• ViewPager和ViewPager2


    1.ViewPager简单使用:

       1)定义Layout

    1. <androidx.viewpager.widget.ViewPager
    2. android:id="@+id/viewpager"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"/>

          2)初始化View

    1. private void initViews() {
    2. viewPager = findViewById(R.id.viewpager);
    3. List<String> list = new ArrayList<>();
    4. for (int i = 0; i < 10; i++) {
    5. list.add("pos: " + i);
    6. }
    7. views = new ArrayList<>();
    8. for (int i = 0; i < list.size(); i++) {
    9. View view = LayoutInflater.from(this).inflate(R.layout.layout_item, null);
    10. TextView textView = view.findViewById(R.id.textview);
    11. textView.setText(list.get(i));
    12. views.add(view);
    13. }
    14. MyPageAdapter pageAdapter = new MyPageAdapter(this, views);
    15. viewPager.setAdapter(pageAdapter);
    16. }

        3)PageAdapter

      

    1. public class MyPageAdapter extends PagerAdapter {
    2. private Context context;
    3. private List<View> views;
    4. public MyPageAdapter(Context context, List<View> list) {
    5. this.context = context;
    6. this.views = list;
    7. }
    8. @Override
    9. public int getCount() {
    10. if (views == null) {
    11. return 0;
    12. } else {
    13. return Integer.MAX_VALUE;
    14. }
    15. }
    16. @Override
    17. public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    18. return view == object;
    19. }
    20. @NonNull
    21. @Override
    22. public Object instantiateItem(@NonNull ViewGroup container, int position) {
    23. int index = position % views.size();
    24. Log.e("nyz", "instantiateItem " + position);
    25. Log.e("nyz", "childCount " + container.getChildCount());
    26. container.addView(views.get(index));
    27. return views.get(index);
    28. }
    29. @Override
    30. public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    31. Log.e("nyz", "destroyItem " + position);
    32. Log.e("nyz", "childCount " + container.getChildCount());
    33. container.removeView((View) object);
    34. }
    35. }

    2.ViewPager2的简单使用

     1)layout引入ViewPager2

    1. <androidx.viewpager2.widget.ViewPager2
    2. android:id="@+id/viewpager2"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent" />

       2)Activity中。

    1. List<String> list = new ArrayList<>();
    2. for (int i = 0; i < 10; i++) {
    3. list.add("pos: " + i);
    4. }
    5. viewPager2 = findViewById(R.id.viewpager2);
    6. //设置ViewPager是纵向滚动,还是横向滚动
    7. // viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
    8. viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
    9. viewPager2.setAdapter(new MyPageAdapter2(this, list));

     3)Adapter,定义Adapter和定义RecycleView的Adapter是一样的。因为在ViewPager2内部有一个RecycleView。

    1. public class MyPageAdapter2 extends RecyclerView.Adapter {
    2. private Context context;
    3. private List list;
    4. public MyPageAdapter2(Context context, List list) {
    5. this.context = context;
    6. this.list = list;
    7. }
    8. @NonNull
    9. @Override
    10. public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    11. View view = LayoutInflater.from(context).inflate(R.layout.layout_item, parent,false);
    12. MyViewHolder viewHolder = new MyViewHolder(view);
    13. return viewHolder;
    14. }
    15. @Override
    16. public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    17. ((MyViewHolder) holder).update(list.get(position));
    18. }
    19. @Override
    20. public int getItemCount() {
    21. return list.size();
    22. }
    23. class MyViewHolder extends RecyclerView.ViewHolder {
    24. private TextView textView;
    25. public MyViewHolder(@NonNull View itemView) {
    26. super(itemView);
    27. textView = itemView.findViewById(R.id.textview);
    28. }
    29. public void update(String str) {
    30. textView.setText(str);
    31. }
    32. }
    33. }

    4)java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)

    1. Process: com.example.viewpager, PID: 8447
    2. java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)
    3. at androidx.viewpager2.widget.ViewPager2$4.onChildViewAttachedToWindow(ViewPager2.java:270)
    4. at androidx.recyclerview.widget.RecyclerView.dispatchChildAttached(RecyclerView.java:7561)
    5. at androidx.recyclerview.widget.RecyclerView$5.addView(RecyclerView.java:860)
    6. at androidx.recyclerview.widget.ChildHelper.addView(ChildHelper.java:107)
    7. at androidx.recyclerview.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:8601)
    8. at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:8559)
    9. at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:8547)
    10. at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1641)
    11. at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
    12. at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
    13. at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
    14. at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
    15. at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
    16. at android.view.View.layout(View.java:23750)
    17. at android.view.ViewGroup.layout(ViewGroup.java:6525)
    18. at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:527)
    19. at android.view.View.layout(View.java:23750)
    20. at android.view.ViewGroup.layout(ViewGroup.java:6525)
    21. at androidx.swiperefreshlayout.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:689)
    22. at android.view.View.layout(View.java:23750)
    23. at android.view.ViewGroup.layout(ViewGroup.java:6525)
    24. at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
    25. at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
    26. at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
    27. at android.view.View.layout(View.java:23750)
    28. at android.view.ViewGroup.layout(ViewGroup.java:6525)
    29. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
    30. at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
    31. at android.view.View.layout(View.java:23750)
    32. at android.view.ViewGroup.layout(ViewGroup.java:6525)
    33. at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
    34. at android.view.View.layout(View.java:23750)
    35. at android.view.ViewGroup.layout(ViewGroup.java:6525)
    36. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
    37. at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
    38. at android.view.View.layout(View.java:23750)
    39. at android.view.ViewGroup.layout(ViewGroup.java:6525)
    40. at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
    41. at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
    42. at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
    43. at android.view.View.layout(View.java:23750)
    44. at android.view.ViewGroup.layout(ViewGroup.java:6525)
    45. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
    46. at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
    47. at com.android.internal.policy.DecorView.onLayout(DecorView.java:880)
    48. at android.view.View.layout(View.java:23750)
    49. at android.view.ViewGroup.layout(ViewGroup.java:6525)
    50. at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:4082)
    51. at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3501)
    52. at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2410)
    53. at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9402)
    54. at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1193)
    55. at android.view.Choreographer.doCallbacks(Choreographer.java:945)
    56. at android.view.Choreographer.doFrame(Choreographer.java:851)
    57. at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1175)
    58. at android.os.Handler.handleCallback(Handler.java:938)

      出现上面的异常的解决方案:

    1)解决方案1 在onCreateViewHolder时不能使用下面的方式:

         View view = LayoutInflater.from(context).inflate(R.layout.layout_item,null);

         而应该使用下面的方式,就不会出现上面的异常:

        View view = LayoutInflater.from(context).inflate(R.layout.layout_item, parent,false);

    2)解决方案2 动态设置宽高

    1. View view = LayoutInflater.from(context).inflate(R.layout.layout_item,null);
    2. ViewGroup.LayoutParams layoutParams =
    3. new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    4. view.setLayoutParams(layoutParams);

    通过上面的异常信息,可以看出来Choreographer的调用流程:

    FrameDisplayEventReceiver.run--doFrame---doCallbacks--CallbackRecord.run--TraversalRunnable.run--doTraversal--performTraversals--performLayout--layout--

    然后就是熟悉的onLayout onMeasure onDraw.

    可以参考另外一篇介绍编舞者的文章:

    Choreographer--编舞者源码分析_choreographer 源码分析-CSDN博客

    1. at android.view.View.layout(View.java:23750)
    2. at android.view.ViewGroup.layout(ViewGroup.java:6525)
    3. at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:4082)
    4. at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3501)
    5. at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2410)
    6. at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9402)
    7. at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1193)
    8. at android.view.Choreographer.doCallbacks(Choreographer.java:945)
    9. at android.view.Choreographer.doFrame(Choreographer.java:851)
    10. at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1175)
    11. at android.os.Handler.handleCallback(Handler.java:938)

    代码下载:

    https://download.csdn.net/download/niuyongzhi/88408862

  • 相关阅读:
    MT3905替代方案 (NDP23511KC)完全替代MT3905
    循环神经网络-简洁实现
    Multitor:一款带有负载均衡功能的多Tor实例创建工具
    数字货币中短线策略(数据+回测+实盘)
    部署kafka后启动报错(坑):无法指定被请求的地址
    Kubernetes调度器:资源分配与优化之道
    macOS快捷键
    数百个模型放在面前,金融机构要如何高效管理
    x265参数-early-skip
    java基于微信小程序的培训机构报名作业管理系统 uniapp 小程序
  • 原文地址:https://blog.csdn.net/niuyongzhi/article/details/133702302