经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android 使用RecycleView列表实现加载更多的示例代码
来源:jb51  时间:2021/5/7 9:35:37  对本文有异议

1.界面布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="#f0f3f5"
  7. tools:context=".MainActivity">
  8.  
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:orientation="vertical"
  13. tools:context=".MainActivity">
  14.  
  15. <ImageView
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:src="@mipmap/logo"/>
  19. <LinearLayout
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_marginTop="10dp"
  23. android:orientation="vertical">
  24. <LinearLayout
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:orientation="horizontal"
  28. android:gravity="center">
  29.  
  30. <TextView
  31. android:layout_width="0dp"
  32. android:layout_weight="1"
  33. android:layout_height="wrap_content"
  34. android:gravity="center"
  35. android:text="电影名"/>
  36. <LinearLayout
  37. android:layout_width="0dp"
  38. android:layout_weight="1"
  39. android:gravity="center"
  40. android:layout_height="wrap_content"
  41. android:orientation="horizontal">
  42.  
  43. <TextView
  44. android:layout_width="wrap_content"
  45. android:layout_height="match_parent"
  46. android:text="电影评分" />
  47.  
  48. </LinearLayout>
  49. <TextView
  50. android:layout_width="0dp"
  51. android:layout_weight="1"
  52. android:gravity="center"
  53. android:layout_height="wrap_content"
  54. android:text="电影图片"/>
  55. </LinearLayout>
  56.  
  57. </LinearLayout>
  58. <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
  59. android:layout_height="wrap_content"
  60. android:layout_width="wrap_content"
  61. android:id="@+id/s1">
  62.  
  63. <androidx.recyclerview.widget.RecyclerView
  64. android:id="@+id/r1"
  65. android:layout_width="match_parent"
  66. android:layout_height="wrap_content" />
  67.  
  68.  
  69. </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
  70. </LinearLayout>
  71.  
  72. </FrameLayout>

列表布局list.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="match_parent"
  5. android:layout_height="160dp">
  6. <TextView
  7. android:id="@+id/t2"
  8. android:layout_width="0dp"
  9. android:layout_weight="1.5"
  10. android:gravity="center"
  11. android:layout_height="wrap_content"
  12. android:layout_gravity="center"
  13. android:text="我不是药神"/>
  14. <TextView
  15. android:id="@+id/t3"
  16. android:layout_width="0dp"
  17. android:layout_weight="1"
  18. android:layout_height="wrap_content"
  19. android:layout_gravity="center"
  20. android:gravity="center"
  21. android:text="9.0"/>
  22. <ImageView
  23. android:id="@+id/i1"
  24. android:layout_width="0dp"
  25. android:layout_weight="1.5"
  26. android:layout_height="150dp"
  27. android:padding="20dp"
  28. android:src="@mipmap/ic_launcher"/>
  29. </LinearLayout>

加载更多布局foot_view.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/tv_foot"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:padding="10dp"
  8. android:gravity="center"
  9. tools:text="下拉刷新"
  10. android:orientation="vertical"/>

在这里插入图片描述

2.功能实现

(1)添加网络权限

  1. <uses-permission android:name="android.permission.INTERNET"/>

(2)添加使用到的第三方库

  1. implementation 'com.android.support:design:28.0.0'
  2. implementation 'com.android.support:support-v4:28.0.0'
  3. implementation 'com.android.support:appcompat-v7:28.0.0'
  4. implementation 'com.squareup.okhttp3:okhttp:3.12.1'
  5. debugImplementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
  6. implementation 'com.google.code.gson:gson:2.8.5'
  7. implementation 'com.github.bumptech.glide:glide:4.9.0'
  8. annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

(3)数据解析
使用GsonFormat插件,快速将json字符串转换成一个Java Bean,免去我们根据json字符串手写对应Java Bean的过程。
定义一个类OneModel.class

  1. public class OneModel implements Serializable {
  2.  
  3. }

使用快捷键(Alt+s)粘贴全部过去数据,之后一直点击OK

在这里插入图片描述

(4)绑定控件ID

  1. private RecyclerView r1;
  2. private SwipeRefreshLayout s1;
  3. private LinearLayoutManager linearLayoutManager;
  4. private Adapter adapter;

在这里插入图片描述

(5)定义一个Adapter类

  1. package com.example.note4;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Color;
  5. import android.os.Handler;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.ImageView;
  11. import android.widget.TextView;
  12.  
  13. import androidx.recyclerview.widget.RecyclerView;
  14.  
  15. import com.bumptech.glide.Glide;
  16.  
  17. import java.util.List;
  18. public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
  19. private Context mContext;
  20. private List<DateModel.SubjectsBean> mData;//数据
  21. private int max_count = 6;//最大显示数
  22. private Boolean isFootView = false;//是否添加了FootView
  23. private String footViewText = "";//FootView的内容
  24.  
  25. //两个final int类型表示ViewType的两种类型
  26. private final int NORMAL_TYPE = 0;
  27. private final int FOOT_TYPE = 1111;
  28.  
  29.  
  30. public Adapter(Context context, List<DateModel.SubjectsBean> data) {
  31. this.mContext = context;
  32. this.mData = data;
  33. }
  34.  
  35. public class ViewHolder extends RecyclerView.ViewHolder {
  36. public TextView t3,t2;
  37. public ImageView i1;
  38. private TextView tvFootView;
  39.  
  40. //初始化viewHolder,此处绑定后在onBindViewHolder中可以直接使用
  41. public ViewHolder(View itemView, int viewType) {
  42. super(itemView);
  43. if (viewType == NORMAL_TYPE) {
  44. t3 = (TextView) itemView.findViewById(R.id.t3);
  45. t2 = (TextView) itemView.findViewById(R.id.t2);
  46. i1=(ImageView)itemView.findViewById(R.id.i1);
  47. } else if (viewType == FOOT_TYPE) {
  48. tvFootView = (TextView) itemView.findViewById(R.id.tv_foot);
  49. }
  50. }
  51. }
  52.  
  53. @Override
  54. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  55. View normal_views = LayoutInflater.from(parent.getContext()).inflate(
  56. R.layout.list, parent, false);
  57. View foot_view = LayoutInflater.from(parent.getContext()).inflate(
  58. R.layout.foot_view, parent, false);
  59.  
  60. if (viewType == FOOT_TYPE)
  61. return new ViewHolder(foot_view, FOOT_TYPE);
  62. return new ViewHolder(normal_views, NORMAL_TYPE);
  63. }
  64.  
  65. @Override
  66. public int getItemViewType(int position) {
  67. if (position == max_count - 1) {
  68. return FOOT_TYPE;
  69. }
  70. return NORMAL_TYPE;
  71. }
  72.  
  73. @Override
  74. public void onBindViewHolder(ViewHolder holder, int position) {
  75. DateModel.SubjectsBean subjectsBean=mData.get(position);
  76. //如果footview存在,并且当前位置ViewType是FOOT_TYPE
  77. if (isFootView && (getItemViewType(position) == FOOT_TYPE)) {
  78. holder.tvFootView.setText(footViewText);
  79. // 刷新太快 所以使用Hanlder延迟两秒
  80. Handler handler = new Handler();
  81. handler.postDelayed(new Runnable() {
  82. @Override
  83. public void run() {
  84. max_count += 5;
  85. notifyDataSetChanged();
  86. }
  87. }, 1000);
  88.  
  89. } else {
  90. holder.t2.setText(subjectsBean.getTitle());
  91. holder.t3.setText(subjectsBean.getRate());
  92. Glide.with(mContext).load(subjectsBean.getCover()).into(holder.i1);
  93. }
  94. }
  95.  
  96. @Override
  97. public int getItemCount() {
  98. if (mData.size() <= max_count) {
  99. return mData.size();
  100. }
  101. return max_count;
  102. }
  103.  
  104. //创建一个方法来设置footView中的文字
  105. public void setFootViewText(String footViewText) {
  106. isFootView = true;
  107. this.footViewText = footViewText;
  108. }
  109.  
  110. }

(6)网络请求

  1. public void getDate(DateModel dateModel)
  2. {
  3. if(dateModel==null||dateModel.getSubjects()==null)
  4. {
  5. Toast.makeText(MainActivity.this,"失败",Toast.LENGTH_SHORT).show();
  6. return;
  7. }
  8. Toast.makeText(MainActivity.this,"成功",Toast.LENGTH_SHORT).show();
  9. adapter=new Adapter(MainActivity.this,dateModel.getSubjects());
  10. adapter.setFootViewText("加载中...");
  11. r1.setAdapter(adapter);
  12. s1.setRefreshing(false);
  13. }
  14. public void requestDate() {
  15.  
  16. String url = "https://movie.douban.com/j/search_subjects?type=movie&tag=%E8%B1%86%E7%93%A3%E9%AB%98%E5%88%86&sort=recommend&page_limit=200&page_start=0";
  17. OkHttpClient okHttpClient = new OkHttpClient();
  18. final Request request = new Request.Builder()
  19. .url(url)
  20. .get()
  21. .build();
  22. Call call = okHttpClient.newCall(request);
  23. call.enqueue(new Callback() {
  24. @Override
  25. public void onFailure(Call call, IOException e) {
  26. runOnUiThread(new Runnable() {
  27. @Override
  28. public void run() {
  29. Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
  30. }
  31. });
  32. }
  33.  
  34. @Override
  35. public void onResponse(Call call, Response response) throws IOException {
  36. String result = response.body().string();
  37. Gson gson = new Gson();
  38. final DateModel dateModel = gson.fromJson(result, DateModel.class);
  39. runOnUiThread(new Runnable() {
  40. @Override
  41. public void run() {
  42. Toast.makeText(MainActivity.this, "网络连接成功", Toast.LENGTH_SHORT).show();
  43. getDate(dateModel);
  44. }
  45. });
  46. }
  47. });
  48. }

(7)功能实现

在这里插入图片描述

  1. linearLayoutManager=new LinearLayoutManager(MainActivity.this);
  2. linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  3. r1.setLayoutManager(linearLayoutManager);
  4. requestDate();
  5.  
  6. s1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  7. @Override
  8. public void onRefresh() {
  9. new Handler().postDelayed(new Runnable() {
  10. @Override
  11. public void run() {
  12. requestDate();
  13. }
  14. },1000);
  15. }
  16. });

(8)源代码
点击下载

到此这篇关于Android 使用RecycleView列表实现加载更多的文章就介绍到这了,更多相关Android加载更多内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号