Java 自定义表格布局指示器-Android

Java 自定义表格布局指示器-Android,java,android,Java,Android,我一直在尝试制作一个定制的表格布局指示器,就像谷歌硬盘的一样,但没有找到它。 我尝试了以下文章: 它工作正常,但不如下面GIF中的效果好 提前谢谢 有两件事是这样的 通过单击选项卡工作 通过水平滚动和单击选项卡也可以工作 我不知道你到底想要什么。因为,您提供的链接是no 1。这是TabHost。我正在为两者添加源代码 ViewPagerAdapter: public class ViewPagerAdapter extends FragmentPagerAdapter { private

我一直在尝试制作一个定制的表格布局指示器,就像谷歌硬盘的一样,但没有找到它。 我尝试了以下文章:

它工作正常,但不如下面GIF中的效果好

提前谢谢


有两件事是这样的

  • 通过单击选项卡工作

  • 通过水平滚动和单击选项卡也可以工作

  • 我不知道你到底想要什么。因为,您提供的链接是
    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);
    }
    }
    
    如果要添加图标:

    //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\u 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>
    
    main活动

    //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);
    

    看到这个,希望你能有一些想法。它接近我想要的,但与GIF中的不一样。我不认为这回答了我的问题,你能看看GIF来了解更多关于我想要的吗。谢谢你。@IlyasseSalama我明白了。它实际上是作为
    ViewPager
    工作的。你可以使用
    no 2
    源代码来理解我在说什么,阅读我答案的描述。它将作为GIF文件使用。但是,
    布局
    设计不是这样的。您必须找到类似UI/UX设计的
    。否则,你必须自己创造。
    <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>
    
    //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);