I need to assign a unique tab id to my tabs created using viewpager and tablayout?

不打扰是莪最后的温柔 提交于 2019-12-01 17:34:40

Here is my solution for the question:

First, create an id file under res/values named ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="tab_id_one" type="id"/>
    <item name="tab_id_two" type="id"/>
</resources>

Then, within the code where tabs are created, do the following:

for (int i = 0; i< tabLayout.getTabCount(); i++) {
        TabLayout.Tab mTab = tabLayout.getTabAt(i);
        if (mTab != null) {
            switch (i){
                case 0:
                    View tabViewOne = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(i);
                    tabViewOne.setId(R.id.tab_id_one);
                    //ect..
                    break;
                case 1:
                    View tabViewTwo = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(i);
                    tabViewTwo.setId(R.id.tab_id_two);
                    //ect..
                    break;
                case 2:
                    //etc..
            }
        }
    }

Note: Views tabViewOne and tabViewTwo can be made global variables where it will give access to their ids anywhere within the class/activity/fragment.

In your viewPager Adapter extends FragmentPagerAdapter this and include these methods and it will give new ID to every Fragment and when you add manually data in array and call notifyDataSetChange(), also call this method notifyChangeInPosition(mItems.size() - 1);.

From this the added item get a new ID.

  private long baseId = 0;

    ViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    // Returns total number of pages
    @Override
    public int getCount() {
        return mItems.size();
    }
//this is called when notifyDataSetChanged() is called
    @Override
    public int getItemPosition(Object object) {
        // refresh all fragments when data set changed
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public long getItemId(int position) {
        // give an ID different from position when position has been changed
        return baseId + position;
    }

    /**
     * Notify that the position of a fragment has been changed.
     * Create a new ID for each position to force recreation of the fragment
     *
     * @param n number of items which have been changed
     */
    void notifyChangeInPosition(int n) {
        // shift the ID returned by getItemId outside the range of all previous fragments
        baseId += getCount() + n;
    }

You can use the setTag() method of TabLayout.Tab to use any Object you want as an identifier. You could do this in your setupTabLayout() method, where you are setting your custom view for each Tab.

For example, to use a String:

tabLayout.getTabAt(i).setTag("tab_" + i);

You can then later check the tag of each tab in your parent fragment with tab.getTag();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!