tablayout 和viewpager的绑定

时间:2021-12-08 16:25:47
chargerContainerAdapter = new ChargerContainerAdapter(getSupportFragmentManager(), this);
chargerContainerAdapter.setChargerId(currStationId);
containerVpMarker.setAdapter(chargerContainerAdapter);
container_tl_mraker.setupWithViewPager(containerVpMarker);
setupTabIcons();
LinearLayout linearLayout = (LinearLayout) container_tl_mraker.getChildAt(0);
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,
        R.drawable.layout_divider_vertical));
linearLayout.setDividerPadding(30);
containerVpMarker.setCurrentItem(mfPoision);

container_tl_mraker.getTabAt(mfPoision).select();

private void setupTabIcons() {
    container_tl_mraker.getTabAt(0).setCustomView(chargerContainerAdapter.getTabView(0));
    container_tl_mraker.getTabAt(1).setCustomView(chargerContainerAdapter.getTabView(1));
    container_tl_mraker.getTabAt(2).setCustomView(chargerContainerAdapter.getTabView(2));
}
public class ChargerContainerAdapter extends FragmentStatePagerAdapter {
    private final Context mContext;
    private String chargerId;
    private String[] mTitles = new String[]{"详情", "车位", "点评"};
    private Integer [] mIcons = new Integer[]{R.drawable.ic_popup_bottom_info_icon,R.drawable.ic_popup_bottom_count_icon,R.drawable.ic_popup_bottom_message_icon};
    private Fragment mFragment = null ;

    public ChargerContainerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.mContext = context ;
    }

    public void setChargerId(String chargerId ){
        this.chargerId =chargerId;
    }
    @Override
    public Fragment getItem(int position) {
        Bundle bundle = new Bundle();
        bundle.putString("chargerId",chargerId);
        if (position==0){
            mFragment = new ChargerInfoFragment();
            mFragment.setArguments(bundle);
        }else if (position==1){
            mFragment = new ChargerCarportFragment();
            mFragment.setArguments(bundle);
        }else if (position==2){
            mFragment = new ChargerCommentFragment();
            mFragment.setArguments(bundle);
        }
        return mFragment;
    }

    @Override
    public int getCount() {
        return mTitles.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }

    public View getTabView(int i) {
        View newtab =  LayoutInflater.from(mContext).inflate(R.layout.item_tab_layout,null);
        TextView tv = (TextView) newtab.findViewById(R.id.tabtext);
        tv.setText(mTitles[i]);
        ImageView im = (ImageView)newtab.findViewById(R.id.tabicon);
        im.setImageResource(mIcons[i]);
        return newtab;
    }
}