首页 > 解决方案 > 自定义 TabLayout 指示器 - Android

问题描述

我一直在尝试制作一个像 Google Drive 一样的自定义 TabLayout 指示器,但没有弄明白。我尝试了以下文章:

https://medium.com/@adrianespi94/apply-new-google-style-to-your-android-material-tabs-4d498c993c51

它工作正常,但不如下面 GIF 中的那个。

提前致谢。

在此处输入图像描述

标签: javaandroid

解决方案


有两件事是这样的。

  1. 通过单击选项卡工作。

  2. 也可以通过水平滚动和单击选项卡来工作。

我不知道你实际上想要什么。因为,你给的链接是no 1。这是TabHost。我正在为两者添加源代码。

ViewPagerAdapter

public class ViewPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragmentList=new ArrayList<>();
private final List<String> fragmentListTitles=new ArrayList<>();


public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    return fragmentList.get(position);
}

@Override
public int getCount() {
    return fragmentListTitles.size();
}


//return page title
@Override
public CharSequence getPageTitle(int position) {
    return fragmentListTitles.get(position);
}

public void AddFragment(Fragment fragment,String title)
{
    fragmentList.add(fragment);
    fragmentListTitles.add(title);
}
}

MainActivity

private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;

tabLayout=(TabLayout)findViewById(R.id.tablayout);
    viewPager=(ViewPager)findViewById(R.id.viewpager);


    //create viewpageradaper class object
    ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager());
    //adding fragments using adapter object
    adapter.AddFragment(new FreeFireFragment(), "Free Fire");
    adapter.AddFragment(new PubgFragment(), "Pubg");
    adapter.AddFragment(new CallOfDutyFragment(), "Call of Duty");

    //set adapter into viewpager
    viewPager.setAdapter(adapter);

    //set viewpager into tablayout
    tabLayout.setupWithViewPager(viewPager);

如果要添加图标:

//for set icon into tab items
final int[] ICONS = new int[]{
        R.drawable.ff,
        R.drawable.pubg,
        R.drawable.cod
};
//enter the following source code below the MainActivity source code
//set icon to tab items
    tabLayout.getTabAt(0).setIcon(ICONS[0]);
    tabLayout.getTabAt(1).setIcon(ICONS[1]);
    tabLayout.getTabAt(2).setIcon(ICONS[2]);

activity_home.xml

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


<com.google.android.material.tabs.TabLayout
    android:id="@+id/tablayout"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    app:tabBackground="@color/homeColor"
    app:tabGravity="fill"
    app:tabIndicatorColor="@color/tabIndicator"
    app:tabMode="fixed"
    app:tabSelectedTextColor="@color/tabIndicator"
    app:tabTextColor="@color/tabTextColor">

</com.google.android.material.tabs.TabLayout>

<androidx.viewpager.widget.ViewPager
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/viewpager">

</androidx.viewpager.widget.ViewPager>



</LinearLayout>

上面的源代码作为没有 2 工作。

现在,没有1:

Layout

<TabHost
    android:id="@+id/tab_host"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/rl_">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/Filters"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <include
                    layout="@layout/filters_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/Adjustments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include
                    layout="@layout/adjustment_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>

        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#40cccccc" />

    </LinearLayout>
</TabHost>

MainActivity

//setup tabHost
    TabHost tabHost = findViewById(R.id.tab_host);
    tabHost.setup();

    //set item 1 in tabhost
    TabHost.TabSpec  Filters= tabHost.newTabSpec("Filters");
    Filters.setContent(R.id.Filters);
    Filters.setIndicator("Filters");
    tabHost.addTab(Filters);

    //set item 2 in tabhost
    TabHost.TabSpec Adjustments= tabHost.newTabSpec("Adjustments");
    Adjustments.setContent(R.id.Adjustments);
    Adjustments.setIndicator("Adjustments");
    tabHost.addTab(Adjustments);

推荐阅读