在ViewPager中捕获幻灯片的更改

时间:2016-12-24 16:18:51

标签: android android-viewpager

我已经实施了ViewPager,以便在两个Fragments之间滑动,如下所示:

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    view =  inflater.inflate(R.layout.statistics_general_vp, container, false);

    mViewPager = (ViewPager) view.findViewById(R.id.pager);
    mStatisticsPagerAdapter = new StatisticsPagerAdapter(getActivity().getSupportFragmentManager());
    mViewPager.setAdapter(mStatisticsPagerAdapter);

    return view;
}

public class StatisticsPagerAdapter extends FragmentStatePagerAdapter {
    public StatisticsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        return new GeneralStatistics();
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }   
}

但是现在我的两张幻灯片显示相同的Fragment

我如何才能展示幻灯片1 Fragment 1和幻灯片2 Fragment 2

2 个答案:

答案 0 :(得分:0)

你是否单独创建了fragment1和fragment 2?如果是,则执行此操作 - 在持有这两个片段的活动中。

private void createViewPager(ViewPager viewPager) {
        Log.d("activity", "viewpager");
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new Fragment1(), "");
        adapter.addFrag(new Fragment2(), "");

        viewPager.setAdapter(adapter);
    }

像这样创建片段 -

    public class Fragment1 extends Fragment {


        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            View v = inflater.inflate(R.layout.transaction_layout, container, false);



            return v;

        }

}

像这样创建viewpager -

class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }

    }

或者如果您想使用图标制作标签,请使用此方法 -

private void createTabIcons() {
        Log.d("activity", "createtabicons");
        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.tab_item2, null);
        tabOne.setText("QR Scan");
        tabOne.setTypeface(MRR);
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image1, 0, 0);
        tabOne.setCompoundDrawablePadding(5);
        tabLayout.getTabAt(0).setCustomView(tabOne);
        tabLayout.getTabAt(0).getCustomView().setSelected(true);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.tab_item2, null);
        tabTwo.setText("Mobile No.");
        tabTwo.setTypeface(MRR);
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image3, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);
        tabTwo.setCompoundDrawablePadding(5);
    }

答案 1 :(得分:0)

您好,我告诉您我在做什么,我在mainActivity中有2个片段

 private void addFragments() {
        List<Fragment> fragments = new ArrayList<>();
        chatFragment = new ChatFragment();
        remoteFragment = new RemoteFragment();
        fragments.add(remoteFragment);
        fragments.add(chatFragment);
        MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(TAB_REMOTE_POSITION)
                .setIcon(getDrawable(R.drawable.ic_settings_remote_white_24dp));
        tabLayout.getTabAt(TAB_SOCIAL_POSITION)
                .setIcon(getDrawable(R.drawable.ic_group_white_24dp));
    }

这是我的适配器:

class MyPageAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

并且OnCreate你应该

addFragments();

我也用ButterKnife绑定视图

 @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.tab_layout)
    TabLayout tabLayout;

但你可以用android

静态实现这个

请记住,你有听众:

private void pageChangeListener() {
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
            View view = getCurrentFocus();
            if (view != null) {
                InputMethodManager imm = (InputMethodManager) getSystemService
                        (Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    });
}