盒子
盒子
文章目录
  1. 步骤
  2. 适配器
    1. FragmentPagerAdapter
    2. FragmentStatePagerAdapter
    3. ViewPagerAdapter
  3. ViewPager的优化和封装
  4. 疑问

探索PagerView

步骤

在主activity布局添加android.support.v4.view.ViewPager控件

创建几个页面布局

将页面布局与Fragment绑定,将Fragment添加进容器

也可以不绑定,类似于以下PagerAdapter上编写方式,直接获取View

创建适配器,在容器中取出Fragment,然后将其显示在ViewPager中

适配器

FragmentPagerAdapter

该适配器会将生成的每一个Fragment保存在内存中,因此适用于PagerView页面不多的情况

eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
private class ViewPagerAdapter extends FragmentPagerAdapter{
private ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return pagers.size();
}
@Override
public Fragment getItem(int position) {
return pagers.get(position);
}
}

只需重写

  1. 带FragmentManager参数的构造方法

  2. getCount

  3. getItem

Fragment页面生命周期:

进入第一个页面:

1
2
3
4
5
6
7
8
9
10
11
12
PagerFragmentOne: onAttach
onCreate
PagerFragmentTwo: onAttach
PagerFragmentTwo: onCreate
PagerFragmentOne: onCreateView
PagerFragmentOne: onActivityCreated
onStart
onResume
PagerFragmentTwo: onCreateView
PagerFragmentTwo: onActivityCreated
onStart
onResume

进入第二个页面:

1
2
3
4
5
6
PagerFragmentThird: onAttach
onCreate
PagerFragmentThird: onCreateView
PagerFragmentThird: onActivityCreated
PagerFragmentThird: onStart
onResume

进入第三个页面:

1
2
3
PagerFragmentOne: onPause
PagerFragmentOne: onStop
onDestroyView

由上可知ViewPagerAdapter会将左右两个页面预先加载(执行到onResume),当翻页到某页不在该页左右,则该页视图会被销毁(执行到onDestroyView),但不会销毁Fragment对象

FragmentStatePagerAdapter

该适配器不会将Fragment保存在内存中,适合较多页面的PagerView

eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
private class ViewPagerAdapterr extends android.support.v4.app.FragmentStatePagerAdapter{
public ViewPagerAdapterr(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return pagers.size();
}
@Override
public Fragment getItem(int position) {
return pagers.get(position);
}
}

与FragmentPagerAdapter一样需要重写三个方法

Fragment的生命周期:

与FragmentPagerAdapter不同之处在于当翻页到某页不在该页左右,则该页Fragment销毁(执行到onDetch

ViewPagerAdapter

至少需要重写

  1. instantiateItem

  2. isViewFromObjec

  3. getCount

    eg:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
      private class PagerAdapterr extends PagerAdapter{
    public PagerAdapterr() {
    super();
    }
    @Override
    public int getCount() {
    return pagers.size();
    }
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
    container.addView(views.get(position));
    Log.i(TAG,"instantiateItem"+position);
    return views.get(position);
    }
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view==object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView(views.get(position));
    Log.i(TAG,"destroyItem"+position);
    }
    }

    instantiateItem创建显示的视图

    isViewFromObject判断当前视图是否与object对象相同

    destroyItem销毁指定视图

    生命周期:

    位于第一页:

    1
    2
    ViewPagerActivity: instantiateItem0
    instantiateItem1

    滑向第二页:

    1
    ViewPagerActivity: instantiateItem2

    滑向第三页:

    1
    ViewPagerActivity: destroyItem0

ViewPager的优化和封装

lller库的使用文章中的ViewPageRer的使用

疑问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
List<Fragment> fragments=new ArrayList<>();
fragments.add(new FirstFragment());
fragments.add(new SecondFragment());

public void setPages(List<Fragment> fragments, List<View> views){
this.fragments=fragments;
this.views=views;
}
private class FragmentPagerAdapterr extends FragmentPagerAdapter {
private FragmentPagerAdapterr(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
Log.i(TAG,"fragments.size():"+fragments.size());
return fragments.size();
}
@Override
public Fragment getItem(int position) {
Log.i(TAG,"position:"+position);
return fragments.get(position);
}
}

//FirstFragment.class
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.i(TAG,"onCreateView");
//若传入container会出错
return inflater.inflate(R.layout.fragment_first,null);
}

07-31 15:00:13.732 13036-13036/? E/AndroidRuntime: at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6505)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6505)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6505)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6505)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6505)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6505)
…………..

支持一下
扫一扫,支持Grooter
  • 微信扫一扫
  • 支付宝扫一扫