视图分页器上有多个页面

时间:2018-06-28 01:53:26

标签: android android-viewpager viewpagerindicator

我要实现如下所示的页面查看器

viewpager

如您所见,页面在缩放时,

我知道我们可以通过信息来实现这一点,但是它是Android的新手。 我想要得到的实际效果如下

我怎么能达到这种效果?我不知道如何开始,请帮忙,

1 个答案:

答案 0 :(得分:0)

尝试使用这种逻辑,您绝对可以得到想要的

代码实现

List<Fragment> fragments = buildFragments();
ArrayList<String> categories = {"1", "2", "3", ..., "n"};
mPager = (ViewPager) v.findViewById(R.id.pager);
mPageAdapter = new MyFragmentPageAdapter(this,getSupportFragmentManager(), fragments, categories);
mPager.setAdapter(mPageAdapter);

//Add a new Fragment to the list with bundle
Bundle b = new Bundle();
b.putInt("position", i);
String title = "asd";
mPageAdapter.add(MyFragment.class, title, b);
mPageAdapter.notifyDataSetChanged();

MyFragmentPageAdapter.java

public class MyFragmentPageAdapter extends FragmentPagerAdapter {

public static int pos = 0;

private List<Fragment> myFragments;
private ArrayList<String> categories;
private Context context;

public MyFragmentPageAdapter(Context c, FragmentManager fragmentManager, List<Fragment> myFrags, ArrayList<String> cats) {
    super(fragmentManager);
    myFragments = myFrags;
    this.categories = cats;
    this.context = c;
}

@Override
public Fragment getItem(int position) {

    return myFragments.get(position);

}

@Override
public int getCount() {

    return myFragments.size();
}

@Override
public CharSequence getPageTitle(int position) {

    setPos(position);
    return categories.get(position);
}

public static int getPos() {
    return pos;
}

public void add(Class<Fragment> c, String title, Bundle b) {
    myFragments.add(Fragment.instantiate(context,c.getName(),b));
    categories.add(title);
}

public static void setPos(int pos) {
    MyFragmentPageAdapter.pos = pos;
}
}

片段列表生成器

private List<android.support.v4.app.Fragment> buildFragments() {
        List<android.support.v4.app.Fragment> fragments = new ArrayList<android.support.v4.app.Fragment>();
        for(int i = 0; i<categories.size(); i++) {
            Bundle b = new Bundle();
            b.putInt("position", i);
            fragments.add(Fragment.instantiate(this,MyPagerFragment.class.getName(),b));
        }

        return fragments;
}