android-View之TabLayout+ViewPage

2019-03-20  本文已影响0人  伊泽瑞额
image.png
<?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>
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;
    }
}
   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);
  @Override
    public void onSuccessRefresh(List<ForMyAppealApproveBean> msg)
    {
        mDatas.clear();
        mDatas.addAll(msg);
        mAdapter.notifyDataSetChanged();
        EventBus.getDefault().post(new AppealApproveCountEvent(TAG, msg.size()));

    }
  /**
     *更新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));

        }
    }
上一篇下一篇

猜你喜欢

热点阅读