基于Android官方Paging Library的RecyclerView分页加载框架

匿名 (未验证) 提交于 2019-12-03 00:37:01
基于Android官方Paging Library的RecyclerView分页加载框架


我之前写了一篇RecyclerView分页加载机制的文章,是基于Android官方的AsyncListUtil实现的,详情见附录文章1。现在再介绍一种RecyclerView分页加载框架:Android Paging Library。
Android Paging Library是Android官方support-v7支持包中专门做的分页框架,详细文档见谷歌官方文档附录2页面。我写这篇文章时候Paging Library的版本是1.0.0-alpha3。
使用Android Paging Library首先需要在gradle添加引用:
implementation 'android.arch.paging:runtime:1.0.0-alpha3'

下面我写示例代码做一个简单的演示:

  1. package
  2. import
  3. import
  4. import
  5. import
  6. import
  7. import
  8. import
  9. import
  10. import
  11. import
  12. import
  13. import
  14. import
  15. import
  16. import
  17. import
  18. import
  19. import
  20. import
  21. import
  22. publicclassextends
  23. private
  24. private
  25. private
  26. private
  27. private
  28. @Override
  29. protectedvoid
  30. super
  31. new
  32. newthis
  33. new
  34. new
  35. privateint
  36. @Override
  37. publicvoidint
  38. super
  39. privatevoid
  40. new
  41. 3//分页数据的数量。在后面的DataSource之loadRange中,count即为每次加载的这个设定值。
  42. 5//初始化时候,预取数据数量。
  43. false
  44. new
  45. new//初始化阶段启用
  46. new//初始化阶段启动
  47. privateclassimplements
  48. public
  49. this.execute(new
  50. @Override
  51. publicvoid
  52. ”BackgroundThreadTask”“run”
  53. @Override
  54. publicvoid@NonNull
  55. privateclassimplements
  56. public
  57. this.execute(new
  58. @Override
  59. publicvoid
  60. ”MainThreadTask”“run”
  61. @Override
  62. publicvoid@NonNull
  63. privateclassextends
  64. @Override
  65. publicint
  66. return
  67. /**
  68. @Override
  69. publicintint
  70. ”MyDataSource”“loadRange:”“,”
  71. return
  72. /**
  73. privateintint
  74. new
  75. forint0
  76. new
  77. ”zhangphil@”
  78. return
  79. privateclassextends
  80. public
  81. public
  82. public
  83. super
  84. privateclassextends
  85. public
  86. super
  87. @Override
  88. publicint
  89. null
  90. new
  91. return
  92. @Override
  93. publicvoidint
  94. privatenew
  95. @Override
  96. publicboolean@NonNull@NonNull
  97. ”DiffCallback”“areItemsTheSame”
  98. return
  99. @Override
  100. publicboolean@NonNull@NonNull
  101. ”DiffCallback”“areContentsTheSame”
  102. return
  103. privateclass
  104. publicint
  105. public
package zhangphil.demo;  import android.arch.paging.PagedList; import android.arch.paging.PagedListAdapter; import android.arch.paging.TiledDataSource; import android.graphics.Color; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.recyclerview.extensions.DiffCallback; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView;  import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor;  public class MainActivity extends AppCompatActivity {     private PagedList<DataBean> mPagedList;     private MyDataSource mDataSource;      private RecyclerView mRecyclerView;     private PagedListAdapter mAdapter;      private LinearLayoutManager mLayoutManager;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          mDataSource = new MyDataSource();         makePageList();          mRecyclerView = findViewById(R.id.recycler_view);          mLayoutManager = new LinearLayoutManager(this);         mLayoutManager.setOrientation(LinearLayout.VERTICAL);         mRecyclerView.setLayoutManager(mLayoutManager);          mAdapter = new MyAdapter();         mRecyclerView.setAdapter(mAdapter);          mAdapter.setList(mPagedList);          mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {             private int lastPos;              @Override             public void onScrollStateChanged(RecyclerView recyclerView, int newState) {                 super.onScrollStateChanged(recyclerView, newState);                  lastPos = mLayoutManager.findLastVisibleItemPosition();                  mPagedList.loadAround(lastPos);//触发Android Paging的加载事务逻辑。             }         });     }      private void makePageList() {         PagedList.Config mPagedListConfig = new PagedList.Config.Builder()                 .setPageSize(3) //分页数据的数量。在后面的DataSource之loadRange中,count即为每次加载的这个设定值。                 .setPrefetchDistance(5) //初始化时候,预取数据数量。                 .setEnablePlaceholders(false)                 .build();          mPagedList = new PagedList.Builder()                 .setConfig(mPagedListConfig)                 .setDataSource(mDataSource)                 .setMainThreadExecutor(new BackgroundThreadTask()) //初始化阶段启用                 .setBackgroundThreadExecutor(new MainThreadTask()) //初始化阶段启动                 .build();     }      private class BackgroundThreadTask implements Executor {         public BackgroundThreadTask() {             this.execute(new Runnable() {                 @Override                 public void run() {                     Log.d("BackgroundThreadTask", "run");                 }             });         }          @Override         public void execute(@NonNull Runnable runnable) {             runnable.run();         }     }      private class MainThreadTask implements Executor {         public MainThreadTask() {             this.execute(new Runnable() {                 @Override                 public void run() {                     Log.d("MainThreadTask", "run");                 }             });         }          @Override         public void execute(@NonNull Runnable runnable) {             runnable.run();         }     }      private class MyDataSource extends TiledDataSource<DataBean> {          @Override         public int countItems() {             return TiledDataSource.COUNT_UNDEFINED;         }          /**          * 注意,这里需要后台线程化。          *          * @param startPosition          * @param count          * @return          */         @Override         public List<DataBean> loadRange(int startPosition, int count) {             Log.d("MyDataSource", "loadRange:" + startPosition + "," + count);             List<DataBean> list = loadData(startPosition, count);             return list;         }     }      /**      * 假设这里需要做一些后台线程的数据加载任务。      *      * @param startPosition      * @param count      * @return      */     private List<DataBean> loadData(int startPosition, int count) {         List<DataBean> list = new ArrayList();          for (int i = 0; i < count; i++) {             DataBean data = new DataBean();             data.id = startPosition + i;             data.content = "zhangphil@" + data.id;             list.add(data);         }          return list;     }      private class MyViewHolder extends RecyclerView.ViewHolder {         public TextView text1;         public TextView text2;          public MyViewHolder(View itemView) {             super(itemView);              text1 = itemView.findViewById(android.R.id.text1);             text1.setTextColor(Color.RED);              text2 = itemView.findViewById(android.R.id.text2);             text2.setTextColor(Color.BLUE);         }     }      private class MyAdapter extends PagedListAdapter<DataBean, MyViewHolder> {         public MyAdapter() {             super(mDiffCallback);         }          @Override         public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {             View view = LayoutInflater.from(getApplicationContext()).inflate(android.R.layout.simple_list_item_2, null);             MyViewHolder holder = new MyViewHolder(view);             return holder;         }          @Override         public void onBindViewHolder(MyViewHolder holder, int position) {             DataBean data = mPagedList.get(position);             holder.text1.setText(String.valueOf(position));             holder.text2.setText(String.valueOf(data.content));         }     }      private DiffCallback<DataBean> mDiffCallback = new DiffCallback<DataBean>() {          @Override         public boolean areItemsTheSame(@NonNull DataBean oldItem, @NonNull DataBean newItem) {             Log.d("DiffCallback", "areItemsTheSame");             return oldItem.id == newItem.id;         }          @Override         public boolean areContentsTheSame(@NonNull DataBean oldItem, @NonNull DataBean newItem) {             Log.d("DiffCallback", "areContentsTheSame");             return TextUtils.equals(oldItem.content, newItem.content);         }     };      private class DataBean {         public int id;         public String content;     } }


代码实现的功能很简单,当RecyclerView不断下滑时,就触发分页加载,把RecyclerView后续使用的数据分页加载显示出来。暂时我将现阶段对Android Paging Library技术的心得记录下来,作为我学习Android Paging Library技术的阶段性备忘录。这部分代码还有待进一步完善,Android Paging Library技术细节后续会继续跟进研究。


附录:

2,谷歌Android官方Android Paging Library技术文档主页:https://developer.android.google.cn/topic/libraries/architecture/paging.html#classes

转自:https://blog.csdn.net/zhangphil/article/details/78627332

基于Android官方Paging Library的RecyclerView分页加载框架


我之前写了一篇RecyclerView分页加载机制的文章,是基于Android官方的AsyncListUtil实现的,详情见附录文章1。现在再介绍一种RecyclerView分页加载框架:Android Paging Library。
Android Paging Library是Android官方support-v7支持包中专门做的分页框架,详细文档见谷歌官方文档附录2页面。我写这篇文章时候Paging Library的版本是1.0.0-alpha3。
使用Android Paging Library首先需要在gradle添加引用:
implementation 'android.arch.paging:runtime:1.0.0-alpha3'

下面我写示例代码做一个简单的演示:

  1. package
  2. import
  3. import
  4. import
  5. import
  6. import
  7. import
  8. import
  9. import
  10. import
  11. import
  12. import
  13. import
  14. import
  15. import
  16. import
  17. import
  18. import
  19. import
  20. import
  21. import
  22. publicclassextends
  23. private
  24. private
  25. private
  26. private
  27. private
  28. @Override
  29. protectedvoid
  30. super
  31. new
  32. newthis
  33. new
  34. new
  35. privateint
  36. @Override
  37. publicvoidint
  38. super
  39. privatevoid
  40. new
  41. 3//分页数据的数量。在后面的DataSource之loadRange中,count即为每次加载的这个设定值。
  42. 5//初始化时候,预取数据数量。
  43. false
  44. new
  45. new//初始化阶段启用
  46. new//初始化阶段启动
  47. privateclassimplements
  48. public
  49. this.execute(new
  50. @Override
  51. publicvoid
  52. ”BackgroundThreadTask”“run”
  53. @Override
  54. publicvoid@NonNull
  55. privateclassimplements
  56. public
  57. this.execute(new
  58. @Override
  59. publicvoid
  60. ”MainThreadTask”“run”
  61. @Override
  62. publicvoid@NonNull
  63. privateclassextends
  64. @Override
  65. publicint
  66. return
  67. /**
  68. @Override
  69. publicintint
  70. ”MyDataSource”“loadRange:”“,”
  71. return
  72. /**
  73. privateintint
  74. new
  75. forint0
  76. new
  77. ”zhangphil@”
  78. return
  79. privateclassextends
  80. public
  81. public
  82. public
  83. super
  84. privateclassextends
  85. public
  86. super
  87. @Override
  88. publicint
  89. null
  90. new
  91. return
  92. @Override
  93. publicvoidint
  94. privatenew
  95. @Override
  96. publicboolean@NonNull@NonNull
  97. ”DiffCallback”“areItemsTheSame”
  98. return
  99. @Override
  100. publicboolean@NonNull@NonNull
  101. ”DiffCallback”“areContentsTheSame”
  102. return
  103. privateclass
  104. publicint
  105. public
package zhangphil.demo;  import android.arch.paging.PagedList; import android.arch.paging.PagedListAdapter; import android.arch.paging.TiledDataSource; import android.graphics.Color; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.recyclerview.extensions.DiffCallback; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView;  import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor;  public class MainActivity extends AppCompatActivity {     private PagedList<DataBean> mPagedList;     private MyDataSource mDataSource;      private RecyclerView mRecyclerView;     private PagedListAdapter mAdapter;      private LinearLayoutManager mLayoutManager;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          mDataSource = new MyDataSource();         makePageList();          mRecyclerView = findViewById(R.id.recycler_view);          mLayoutManager = new LinearLayoutManager(this);         mLayoutManager.setOrientation(LinearLayout.VERTICAL);         mRecyclerView.setLayoutManager(mLayoutManager);          mAdapter = new MyAdapter();         mRecyclerView.setAdapter(mAdapter);          mAdapter.setList(mPagedList);          mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {             private int lastPos;              @Override             public void onScrollStateChanged(RecyclerView recyclerView, int newState) {                 super.onScrollStateChanged(recyclerView, newState);                  lastPos = mLayoutManager.findLastVisibleItemPosition();                  mPagedList.loadAround(lastPos);//触发Android Paging的加载事务逻辑。             }         });     }      private void makePageList() {         PagedList.Config mPagedListConfig = new PagedList.Config.Builder()                 .setPageSize(3) //分页数据的数量。在后面的DataSource之loadRange中,count即为每次加载的这个设定值。                 .setPrefetchDistance(5) //初始化时候,预取数据数量。                 .setEnablePlaceholders(false)                 .build();          mPagedList = new PagedList.Builder()                 .setConfig(mPagedListConfig)                 .setDataSource(mDataSource)                 .setMainThreadExecutor(new BackgroundThreadTask()) //初始化阶段启用                 .setBackgroundThreadExecutor(new MainThreadTask()) //初始化阶段启动                 .build();     }      private class BackgroundThreadTask implements Executor {         public BackgroundThreadTask() {             this.execute(new Runnable() {                 @Override                 public void run() {                     Log.d("BackgroundThreadTask", "run");                 }             });         }          @Override         public void execute(@NonNull Runnable runnable) {             runnable.run();         }     }      private class MainThreadTask implements Executor {         public MainThreadTask() {             this.execute(new Runnable() {                 @Override                 public void run() {                     Log.d("MainThreadTask", "run");                 }             });         }          @Override         public void execute(@NonNull Runnable runnable) {             runnable.run();         }     }      private class MyDataSource extends TiledDataSource<DataBean> {          @Override         public int countItems() {             return TiledDataSource.COUNT_UNDEFINED;         }          /**          * 注意,这里需要后台线程化。          *          * @param startPosition          * @param count          * @return          */         @Override         public List<DataBean> loadRange(int startPosition, int count) {             Log.d("MyDataSource", "loadRange:" + startPosition + "," + count);             List<DataBean> list = loadData(startPosition, count);             return list;         }     }      /**      * 假设这里需要做一些后台线程的数据加载任务。      *      * @param startPosition      * @param count      * @return      */     private List<DataBean> loadData(int startPosition, int count) {         List<DataBean> list = new ArrayList();          for (int i = 0; i < count; i++) {             DataBean data = new DataBean();             data.id = startPosition + i;             data.content = "zhangphil@" + data.id;             list.add(data);         }          return list;     }      private class MyViewHolder extends RecyclerView.ViewHolder {         public TextView text1;         public TextView text2;          public MyViewHolder(View itemView) {             super(itemView);              text1 = itemView.findViewById(android.R.id.text1);             text1.setTextColor(Color.RED);              text2 = itemView.findViewById(android.R.id.text2);             text2.setTextColor(Color.BLUE);         }     }      private class MyAdapter extends PagedListAdapter<DataBean, MyViewHolder> {         public MyAdapter() {             super(mDiffCallback);         }          @Override         public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {             View view = LayoutInflater.from(getApplicationContext()).inflate(android.R.layout.simple_list_item_2, null);             MyViewHolder holder = new MyViewHolder(view);             return holder;         }          @Override         public void onBindViewHolder(MyViewHolder holder, int position) {             DataBean data = mPagedList.get(position);             holder.text1.setText(String.valueOf(position));             holder.text2.setText(String.valueOf(data.content));         }     }      private DiffCallback<DataBean> mDiffCallback = new DiffCallback<DataBean>() {          @Override         public boolean areItemsTheSame(@NonNull DataBean oldItem, @NonNull DataBean newItem) {             Log.d("DiffCallback", "areItemsTheSame");             return oldItem.id == newItem.id;         }          @Override         public boolean areContentsTheSame(@NonNull DataBean oldItem, @NonNull DataBean newItem) {             Log.d("DiffCallback", "areContentsTheSame");             return TextUtils.equals(oldItem.content, newItem.content);         }     };      private class DataBean {         public int id;         public String content;     } }


代码实现的功能很简单,当RecyclerView不断下滑时,就触发分页加载,把RecyclerView后续使用的数据分页加载显示出来。暂时我将现阶段对Android Paging Library技术的心得记录下来,作为我学习Android Paging Library技术的阶段性备忘录。这部分代码还有待进一步完善,Android Paging Library技术细节后续会继续跟进研究。


附录:

2,谷歌Android官方Android Paging Library技术文档主页:https://developer.android.google.cn/topic/libraries/architecture/paging.html#classes
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!