美文网首页
android-View之TabLayout+ViewPage

android-View之TabLayout+ViewPage

作者: 伊泽瑞额 | 来源:发表于2019-03-20 11:10 被阅读0次
    • 我们在使用TabLayout+ViewPage 的时候经常遇到tab 上更新数字提醒 如图:
    image.png
    • 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"
                  android:orientation="vertical"
                  android:background="@color/white"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
    
    
    
        <cn.bingoogolapple.titlebar.BGATitleBar
            android:id="@+id/title"
            style="@style/TitleBar_Default_YES_Divider"
            app:bgatitlebar_titleText="申诉审批"/>
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="@color/color_f7"
            app:tabIndicatorColor="@color/theme_color"
            app:tabIndicatorHeight="1dp"
            app:tabSelectedTextColor="@color/theme_color"
            app:tabTextColor="@color/text_content_light"/>
    
        <View style="@style/divider_h"/>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    
    </LinearLayout>
    
    • adapter

    public class FragmentPagerAdapter extends FragmentStatePagerAdapter
    {
    
        private List<String> mTitles;
    
        private List<Fragment> mFragments;
    
        public FragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments, String[] titles)
        {
            super(fm);
            mFragments = fragments;
    
            if (titles == null)
            {
                mTitles = new ArrayList<>();
            } else
            {
                mTitles = new ArrayList<String>(Arrays.asList(titles));
            }
        }
    
        public FragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles)
        {
            super(fm);
    
            mFragments = fragments;
    
            if (titles == null)
            {
                mTitles = new ArrayList<>();
            } else
            {
                mTitles = titles;
            }
        }
    
    
        @Override
        public Fragment getItem(int position)
        {
            return mFragments.get(position);
        }
    
    
        @Override
        public CharSequence getPageTitle(int position)
        {
            return mTitles.get(position);
        }
    
        @Override
        public int getCount()
        {
            return mFragments.size();
        }
    
        /**
         * 添加 页面
         *
         * @param title
         * @param fragment
         */
        public void addPager(String title, Fragment fragment)
        {
            this.mTitles.add(title);
            this.mFragments.add(fragment);
            notifyDataSetChanged();
        }
    
    
        /**
         * 更新标题
         * @param position
         * @param context
         * @param count
         * @return
         */
    
        public View getTabItemView(int position,Context context,int count)
        {
            View view = LayoutInflater.from(context).inflate(R.layout.tab_layout_item, null);
            TextView textView = (TextView) view.findViewById(R.id.textview);
            textView.setText(mTitles.get(position)+"("+count+")");
    
            return view;
        }
    }
    
    • activity中使用

       String[] tabs = new String[]{
                    "待我审批的",
                    "审批记录"
            };
    
    
            mFragments = new ArrayList<>();
            mFragments.add(ForMyAppealApprovalFragment.getInstance());
            mFragments.add(AppealApprovalRecordFragment.getInstance());
    
            mAdapter = new FragmentPagerAdapter(getSupportFragmentManager(), mFragments, tabs);
            new ForMyAppealApprovalFragment().setPageAdapter(mAdapter);
            mViewPager.setAdapter(mAdapter);
            mTabLayout.setupWithViewPager(mViewPager);
            ViewUtils.addTabLayoutDivider(getContext(), mTabLayout);
    
    • fragment 中拉去数据 通过eventBus传递数据到activity中 t

      @Override
        public void onSuccessRefresh(List<ForMyAppealApproveBean> msg)
        {
            mDatas.clear();
            mDatas.addAll(msg);
            mAdapter.notifyDataSetChanged();
            EventBus.getDefault().post(new AppealApproveCountEvent(TAG, msg.size()));
    
        }
    
    • activity通过eventBus传递数据更新 tab的数字

      /**
         *更新number
         *
         * @param messageCount
         */
        @Subscribe()
        public void onCountChange(AppealApproveCountEvent messageCount)
        {
            if (messageCount != null)
            {
                number=messageCount.getCount();
    
                TabLayout.Tab tab = mTabLayout.getTabAt(0);
                View customView = tab.getCustomView();
                if (customView != null) {
                    ViewParent parent = customView.getParent();
                    if (parent != null) {
                        ((ViewGroup) parent).removeView(customView);
                    }
                }
    
                tab.setCustomView(mAdapter.getTabItemView(0,getContext(),number));
    
            }
        }
    

    相关文章

      网友评论

          本文标题:android-View之TabLayout+ViewPage

          本文链接:https://www.haomeiwen.com/subject/axfzmqtx.html