简体   繁体   English

同一片段中的多个ViewPager

[英]Multiple ViewPager in same Fragment

I have created two customViews classes,Having view pager in each customview class. 我创建了两个customViews类,每个customview类中都有视图寻呼机。 Now I want to include these two custom view classes in same activity. 现在,我想在同一活动中包括这两个自定义视图类。 The problem is one pagerview working fine, but the other pager is not showing any view? 问题是一个pagerview工作正常,但另一个pager没有显示任何视图?

Anyone knowing how to add multiple viewpager in same activity.?
Please help.

My customeViews are 我的customeViews是

public class TrainingResultsTopView extends LinearLayout {

    Context context;
    View rootView;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private PieChartFragment monthFragment;
    private PieChartFragment annualFragment;
    private PieChartFragment allTimeFragment;

    public TrainingResultsTopView(Context context) {
        super(context);
        initView(context);
    }

    public TrainingResultsTopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TrainingResultsTopView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public TrainingResultsTopView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        rootView = inflater.inflate(R.layout.dashboard_top_view, this, true);
        viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(3);
        tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

        setFragmentsToPager();
        customizeTabs(inflater);
    }


//    private void bindDataToFragments() {
//        monthFragment.bindLeaderboardData(leaderboardModelMonthly);
//        annualFragment.bindLeaderboardData(leaderboardModelAnnually);
//        allTimeFragment.bindLeaderboardData(leaderboardModelAllTime);
//    }

    private void setFragmentsToPager() {
        ViewPagerAdapter adapter = new ViewPagerAdapter(((AppCompatActivity)context).getSupportFragmentManager());
        monthFragment = new PieChartFragment();
        annualFragment = new PieChartFragment();
        allTimeFragment = new PieChartFragment();
        adapter.addFragment(monthFragment, "Last 30 Days");
        adapter.addFragment(annualFragment, "This Year");
        adapter.addFragment(allTimeFragment, "All-Time");
        viewPager.setAdapter(adapter);
    }

    private void customizeTabs(LayoutInflater inflater) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0,0,0,0);

        TextView tv1 = (TextView) inflater.inflate(R.layout.textview_dashboard_tab, null);
        tv1.setTypeface(Constants.appTypeface);
        tv1.setText("Last 30 Days");
        tv1.setGravity(Gravity.CENTER);
        tv1.setLayoutParams(lp);
        TextView tv2 = (TextView) inflater.inflate(R.layout.textview_dashboard_tab, null);
        tv2.setTypeface(Constants.appTypeface);
        tv2.setText("This Year");
        tv2.setGravity(Gravity.CENTER);
        tv2.setLayoutParams(lp);
        TextView tv3 = (TextView) inflater.inflate(R.layout.textview_dashboard_tab, null);
        tv3.setTypeface(Constants.appTypeface);
        tv3.setText("All-Time");
        tv3.setGravity(Gravity.CENTER);
        tv3.setLayoutParams(lp);

        tabLayout.getTabAt(0).setCustomView(tv1);
        tabLayout.getTabAt(1).setCustomView(tv2);
        tabLayout.getTabAt(2).setCustomView(tv3);
    }


}






And

public class ActionBeaconRatingTopView extends LinearLayout {

    Context context;
    View rootView;
    private CustomViewPager viewPager;
    private TabLayout tabLayout;
    private ActionBeaconRatingFragment monthFragment;
    private ActionBeaconRatingFragment annualFragment;
    private ActionBeaconRatingFragment allTimeFragment;

    public ActionBeaconRatingTopView(Context context) {
        super(context);
        initView(context);
    }

    public ActionBeaconRatingTopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ActionBeaconRatingTopView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public ActionBeaconRatingTopView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        rootView = inflater.inflate(R.layout.dashboard_top_view, this, true);

        viewPager = (CustomViewPager) rootView.findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setPagingEnabled(false);
        tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

        setFragmentsToPager();
        customizeTabs(inflater);
    }


//    private void bindDataToFragments() {
//        monthFragment.bindLeaderboardData(leaderboardModelMonthly);
//        annualFragment.bindLeaderboardData(leaderboardModelAnnually);
//        allTimeFragment.bindLeaderboardData(leaderboardModelAllTime);
//    }

    private void setFragmentsToPager() {
        ViewPagerAdapter adapter = new ViewPagerAdapter(((AppCompatActivity)context).getSupportFragmentManager());
        monthFragment = new ActionBeaconRatingFragment();
        annualFragment = new ActionBeaconRatingFragment();
        allTimeFragment = new ActionBeaconRatingFragment();
        adapter.addFragment(monthFragment, "Last 30 Days");
        adapter.addFragment(annualFragment, "This Year");
        adapter.addFragment(allTimeFragment, "All-Time");
        viewPager.setAdapter(adapter);
    }

    private void customizeTabs(LayoutInflater inflater) {
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0,0,0,0);

        TextView tv1 = (TextView) inflater.inflate(R.layout.textview_dashboard_tab, null);
        tv1.setTypeface(Constants.appTypeface);
        tv1.setText("Last 30 Days");
        tv1.setGravity(Gravity.CENTER);
        tv1.setLayoutParams(lp);
        TextView tv2 = (TextView) inflater.inflate(R.layout.textview_dashboard_tab, null);
        tv2.setTypeface(Constants.appTypeface);
        tv2.setText("This Year");
        tv2.setGravity(Gravity.CENTER);
        tv2.setLayoutParams(lp);
        TextView tv3 = (TextView) inflater.inflate(R.layout.textview_dashboard_tab, null);
        tv3.setTypeface(Constants.appTypeface);
        tv3.setText("All-Time");
        tv3.setGravity(Gravity.CENTER);
        tv3.setLayoutParams(lp);

        tabLayout.getTabAt(0).setCustomView(tv1);
        tabLayout.getTabAt(1).setCustomView(tv2);
        tabLayout.getTabAt(2).setCustomView(tv3);
    }

I want to add these views in in same fragment. 我想在同一片段中添加这些视图。 At a time one will be visible. 一次将可见。

试试本教程:< 在此处输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM