标签: android-tablayout

TabLayout:如何加载所有选项卡或仅刷卡刷新

我有一个活动,我用recyclerview实现了android TabLayout.我为TabLayout的三个选项卡实现了三个片段.

TabLayout的默认加载行为它将加载并缓存邻居选项卡,但不加载已加载的选项卡.

假设片段A,B和C分别对应于标签1,2和3.

  • 当用户访问选项卡1 --->加载片段A和B.
  • 当用户访问tab 2 ---> Fragment C时
  • 但是当用户再次访问选项卡1时 - >片段A再次加载.

我想要的是当活动开始时,我希望所有三个选项卡只加载一次,然后我可以通过刷新机制(如刷卡刷新)加载选项卡.

有什么办法可以改变TabLayout的加载行为吗?

谢谢,

android android-fragments android-recyclerview android-tablayout

3
推荐指数
1
解决办法
2863
查看次数

文本在 tablayout 选项卡中被截断

我在我的 Android 应用程序中使用一个 tablayout,它会膨胀自定义选项卡并用图标设置它们。由于空间不足,其中一个选项卡的文本被截断。我必须使文本非常小才能完全显示。这是我的选项卡布局的设置方式。

        <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        app:layout_scrollFlags="scroll|enterAlways"
        android:layout_width="match_parent"
        android:layout_height="@dimen/custom_tab_layout_height"
        app:tabMode="fixed"
        app:tabGravity="fill" />
Run Code Online (Sandbox Code Playgroud)

自定义选项卡布局如下所示:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_text"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="?attr/selectableItemBackground"
    android:gravity="center"
    android:textSize="8dip"
    android:textColor="#ffffff"
    android:singleLine="true"/>
Run Code Online (Sandbox Code Playgroud)

这是我设置选项卡文本和图标的代码:

            TextView tabOne = (TextView)LayoutInflater.From(this).Inflate(Resource.Layout.custom_tab, null);
        tabOne.Text = "Home";
        tabOne.SetCompoundDrawablesWithIntrinsicBounds(0, Resource.Drawable.homewhite, 0, 0);
        tabLayout.GetTabAt(0).SetCustomView(tabOne);

        TextView tab2 = (TextView)LayoutInflater.From(this).Inflate(Resource.Layout.custom_tab, null);
        tab2.Text = "Inbox";
        tab2.SetCompoundDrawablesWithIntrinsicBounds(0, Resource.Drawable.messageWhite, 0, 0);
        tabLayout.GetTabAt(1).SetCustomView(tab2);

        TextView tab3 = (TextView)LayoutInflater.From(this).Inflate(Resource.Layout.custom_tab, null);
        tab3.SetCompoundDrawablesWithIntrinsicBounds(0, Resource.Drawable.upload_icon, 0, 0);
        tabLayout.GetTabAt(2).SetCustomView(tab3);

        TextView tab4 = (TextView)LayoutInflater.From(this).Inflate(Resource.Layout.custom_tab, null);
        tab4.Text = "Notification";
        tab4.SetCompoundDrawablesWithIntrinsicBounds(0, Resource.Drawable.notifWhite, 0, 0);
        tabLayout.GetTabAt(3).SetCustomView(tab4);

        TextView tab5 = (TextView)LayoutInflater.From(this).Inflate(Resource.Layout.custom_tab, null);
        tab5.Text = …
Run Code Online (Sandbox Code Playgroud)

tabs android android-tablayout

3
推荐指数
1
解决办法
3599
查看次数

在选定和取消选择状态下更改Tablayout中的图标

对不起,如果之前已经问过这个问题.

我想在选项卡布局的选项卡中选择图标时更改图标.我怎么能用选择器做到这一点?

我的应用程序中有两个选项卡,选中状态图标应该更改.

icons android android-tablayout

3
推荐指数
2
解决办法
7364
查看次数

如何使用工具栏在选项卡布局中设置起始选项卡?

对不起,我的英语不流利。我想更改选项卡布局中的起始片段。另一个有意图的活动有一个价值。所以我只想用我的选择开始选项卡布局的片段。

这是主要活动的 onCreate()

public class MainActivity extends AppCompatActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

    }
Run Code Online (Sandbox Code Playgroud)

这是在片段中更改的代码

    public class SectionsPagerAdapter extends FragmentPagerAdapter …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-tablayout

3
推荐指数
1
解决办法
6360
查看次数

如何意图进入特定的标签片段?

T有两个Activity:MainActivity和LoginActivity。我也有一个 Fragment,名称是 TabFragment(带有 TabLayout),它的父级是 MainActivity。如何从我的登录活动意图进入特定的 TabFragment。

这是我的主要活动:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        id = getIntent().getStringExtra("id");


        if (savedInstanceState == null) {
     getSupportFragmentManager().beginTransaction().replace(R.id.confrag, new FragmentUtama()).commit();
            getSupportFragmentManager().beginTransaction().replace(R.id.confrag, new TabFragment()).commit();
        }
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        sharedPreferences = getSharedPreferences("login.conf", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        String username = sharedPreferences.getString("username", "");
        String password = sharedPreferences.getString("password", "");
        Log.d(TAG, "Username:"+username+"\npassword:"+password);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }
Run Code Online (Sandbox Code Playgroud)

这是我的标签片段:

  public …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-tablayout

3
推荐指数
1
解决办法
8343
查看次数

TabLayout 中选定的自定义选项卡文本颜色

我正在尝试创建自定义选项卡布局,因为我需要在TextView. 我已经将 id 设置@android:id/text1为文档中提到的。

选择我的自定义选项卡时,不会自动更改 TextView 颜色。如何以正确和干净的方式实现它?

正确选择默认选项卡:

在此处输入图片说明

错误选择的自定义选项卡(文本为灰色但应为白色):

在此处输入图片说明

代码

PagerAdapter adapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
TabLayout.Tab tab = tabLayout.getTabAt(2);
if (tab != null) { 
    tab.setCustomView(R.layout.tab_proposed_rewards);
}
Run Code Online (Sandbox Code Playgroud)

布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:gravity="center"
        android:textAppearance="@style/TextAppearance.Design.Tab"/>

    <TextView
        android:id="@+id/indicator"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:background="@drawable/background_indicator"
        android:gravity="center"
        android:lines="1"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

编辑

回答:

tab.setCustomView(R.layout.tab_proposed_rewards);
ColorStateList textColor = tabLayout.getTabTextColors();
TextView textView = (TextView) tab.getCustomView().findViewById(android.R.id.text1);
textView.setTextColor(textColor);
Run Code Online (Sandbox Code Playgroud)

tabs android android-layout android-tablayout

3
推荐指数
1
解决办法
2297
查看次数

错误:在 Fragment 中膨胀 TabLayout 时出现 java.lang.ClassNotFoundException

每当 Android 尝试在我的 Fragment 中扩充 TabLayout 时,我都会收到错误:java.lang.ClassNotFoundException。我正在使用 Kotlin,不确定这是否重要。

我的片段布局

<LinearLayout android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:orientation="vertical"
          xmlns:android="http://schemas.android.com/apk/res/android">

<!-- our tablayout to display tabs  -->
<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

我的 Gradle 依赖

def room_version = "2.1.0-alpha04"

implementation "androidx.room:room-runtime:$room_version"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
annotationProcessor "androidx.room:room-compiler:$room_version"
// For Kotlin use kapt instead of annotationProcessor

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation 'androidx.core:core-ktx:1.1.0-alpha04'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-tablayout

3
推荐指数
1
解决办法
3673
查看次数

使用选择器时,选项卡之间的动画平滑-Android

我想使选项卡的背景平滑地动画到所选位置(就像tabIndicator选项卡之间的默认动画一样)。

这是我的TabLayout样子

<android.support.design.widget.TabLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout"
        app:tabBackground="@drawable/tab_selector"
        app:tabTextColor="@color/white"
        android:background="@drawable/tab_layout_bg"         
    app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
        app:tabIndicator="@null"
        app:tabRippleColor="@null">

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab1"/>

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab2"/>

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab3"/>

    </android.support.design.widget.TabLayout>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在这里,Tab 3被选中。如果我选择Tab 1的背景应该从动画Tab 3Tab 1

我正在使用选择器更改选项卡的背景。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:drawable="@drawable/tab_layout_fg"/>
    <item
        android:drawable="@drawable/tab_layout_bg"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

在这里,我将附加示例动画。仅查看选项卡栏。

在此处输入图片说明

我遵循的方法是否可以实现这种动画?如果没有,请建议我另一种方式。我已经从两天开始从事这项工作。仍然无法弄清楚。

提前致谢。

android android-animation android-tablayout material-components material-components-android

3
推荐指数
2
解决办法
216
查看次数

未找到类 BadgeDrawable。如何在Android中使用Material Design BadgeDrawable

我第一次尝试在材料 TabItem 上使用徽章通知图标。我的目标是在 RecyclerView 中显示带有项目总数的徽章。我正在阅读此文档Tab Layout - Material Components for Android并尝试相应地实现该功能,但在这一行BadgeDrawable badge = tablayout.getTab(0).getOrCreateBadge();我收到了BadgeDrawable该类未找到的错误。

谁能帮我这个?

谢谢

android material-design android-tablayout material-components material-components-android

3
推荐指数
1
解决办法
1675
查看次数

为什么 ViewPager Height match_parent 不起作用?

我正在尝试使用 ViewPager 的 match_parent 高度来替换片段。但layout_height =“match_parent”或“wrap_content”不起作用。如果我使用它们,那么我的观点就会变得不可见。

为什么它仅在我在 dp 中指定时才有效?我如何使其匹配_parent。

我的 XML 布局代码:这是 Fragment Layout 替换为 FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragments.Frag.MyFragment">

        <com.google.android.material.tabs.TabLayout
            android:background="@drawable/toolbar"
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <androidx.viewpager.widget.ViewPager
        android:background="#f1f1f1"
        android:id="@+id/Viewpager"
        android:layout_width="match_parent"
        android:layout_height="600dp"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的 MainActivity将上面的 XML 替换为 FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white_light2"
    android:fitsSystemWindows="true">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                app:layout_scrollFlags="scroll|enterAlways"
                style="@style/customToolBar"
                android:id="@+id/toolbar1"
                app:title="Home">
            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:layout_marginBottom="?attr/actionBarSize"
            android:id="@+id/FragmentHolder"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </androidx.core.widget.NestedScrollView> …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments android-viewpager android-tablayout

3
推荐指数
1
解决办法
1850
查看次数