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

后端 未结 3 1531
清酒与你
清酒与你 2021-01-11 23:59

I created an Tab based application using viewpager and Tablayout. When i click a button new tabs are created with a child fragment.

What I need is to assign differe

相关标签:
3条回答
  • 2021-01-12 00:17

    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.

    0 讨论(0)
  • 2021-01-12 00:26

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

    0 讨论(0)
  • 2021-01-12 00:34

    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;
        }
    
    0 讨论(0)
提交回复
热议问题