电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android 使用RecycleView列表實現加載更多的示例代碼

瀏覽:141日期:2022-09-18 18:52:25
1.界面布局

<?xml version='1.0' encoding='utf-8'?><FrameLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:background='#f0f3f5' tools:context='.MainActivity'> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='match_parent'android:orientation='vertical'tools:context='.MainActivity'><ImageView android:layout_width='match_parent' android:layout_height='wrap_content' android:src='http://www.hdgsjgj.cn/bcjs/@mipmap/logo'/><LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_marginTop='10dp' android:orientation='vertical'> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:orientation='horizontal'android:gravity='center'><TextView android:layout_width='0dp' android:layout_weight='1' android:layout_height='wrap_content' android:gravity='center' android:text='電影名'/><LinearLayout android:layout_width='0dp' android:layout_weight='1' android:gravity='center' android:layout_height='wrap_content' android:orientation='horizontal'> <TextViewandroid:layout_width='wrap_content'android:layout_height='match_parent'android:text='電影評分' /></LinearLayout><TextView android:layout_width='0dp' android:layout_weight='1' android:gravity='center' android:layout_height='wrap_content' android:text='電影圖片'/> </LinearLayout></LinearLayout><androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:layout_height='wrap_content' android:layout_width='wrap_content' android:id='@+id/s1'> <androidx.recyclerview.widget.RecyclerViewandroid: android:layout_width='match_parent'android:layout_height='wrap_content' /></androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </LinearLayout></FrameLayout>

列表布局list.xml

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:orientation='horizontal' android:layout_width='match_parent' android:layout_height='160dp'> <TextViewandroid: android:layout_width='0dp'android:layout_weight='1.5'android:gravity='center'android:layout_height='wrap_content'android:layout_gravity='center'android:text='我不是藥神'/> <TextViewandroid: android:layout_width='0dp'android:layout_weight='1'android:layout_height='wrap_content'android:layout_gravity='center'android:gravity='center'android:text='9.0'/> <ImageViewandroid: android:layout_width='0dp'android:layout_weight='1.5'android:layout_height='150dp'android:padding='20dp'android:src='http://www.hdgsjgj.cn/bcjs/@mipmap/ic_launcher'/></LinearLayout>

加載更多布局foot_view.xml

<?xml version='1.0' encoding='utf-8'?><TextView xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android: android:layout_width='match_parent' android:layout_height='wrap_content' android:padding='10dp' android:gravity='center' tools:text='下拉刷新' android:orientation='vertical'/>

Android 使用RecycleView列表實現加載更多的示例代碼

2.功能實現

(1)添加網絡權限

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

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

implementation ’com.android.support:design:28.0.0’ implementation ’com.android.support:support-v4:28.0.0’ implementation ’com.android.support:appcompat-v7:28.0.0’ implementation ’com.squareup.okhttp3:okhttp:3.12.1’ debugImplementation ’com.squareup.okhttp3:logging-interceptor:3.12.1’ implementation ’com.google.code.gson:gson:2.8.5’ implementation ’com.github.bumptech.glide:glide:4.9.0’ annotationProcessor ’com.github.bumptech.glide:compiler:4.9.0’

(3)數據解析使用GsonFormat插件,快速將json字符串轉換成一個Java Bean,免去我們根據json字符串手寫對應Java Bean的過程。定義一個類OneModel.class

public class OneModel implements Serializable {}

使用快捷鍵(Alt+s)粘貼全部過去數據,之后一直點擊OK

Android 使用RecycleView列表實現加載更多的示例代碼

(4)綁定控件ID

private RecyclerView r1;private SwipeRefreshLayout s1;private LinearLayoutManager linearLayoutManager;private Adapter adapter;

Android 使用RecycleView列表實現加載更多的示例代碼

(5)定義一個Adapter類

package com.example.note4;import android.content.Context;import android.graphics.Color;import android.os.Handler;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import androidx.recyclerview.widget.RecyclerView;import com.bumptech.glide.Glide;import java.util.List;public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> { private Context mContext; private List<DateModel.SubjectsBean> mData;//數據 private int max_count = 6;//最大顯示數 private Boolean isFootView = false;//是否添加了FootView private String footViewText = '';//FootView的內容 //兩個final int類型表示ViewType的兩種類型 private final int NORMAL_TYPE = 0; private final int FOOT_TYPE = 1111; public Adapter(Context context, List<DateModel.SubjectsBean> data) {this.mContext = context;this.mData = data; } public class ViewHolder extends RecyclerView.ViewHolder {public TextView t3,t2;public ImageView i1;private TextView tvFootView;//初始化viewHolder,此處綁定后在onBindViewHolder中可以直接使用public ViewHolder(View itemView, int viewType) { super(itemView); if (viewType == NORMAL_TYPE) {t3 = (TextView) itemView.findViewById(R.id.t3);t2 = (TextView) itemView.findViewById(R.id.t2);i1=(ImageView)itemView.findViewById(R.id.i1); } else if (viewType == FOOT_TYPE) {tvFootView = (TextView) itemView.findViewById(R.id.tv_foot); }} } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View normal_views = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);View foot_view = LayoutInflater.from(parent.getContext()).inflate(R.layout.foot_view, parent, false);if (viewType == FOOT_TYPE) return new ViewHolder(foot_view, FOOT_TYPE);return new ViewHolder(normal_views, NORMAL_TYPE); } @Override public int getItemViewType(int position) {if (position == max_count - 1) { return FOOT_TYPE;}return NORMAL_TYPE; } @Override public void onBindViewHolder(ViewHolder holder, int position) {DateModel.SubjectsBean subjectsBean=mData.get(position);//如果footview存在,并且當前位置ViewType是FOOT_TYPEif (isFootView && (getItemViewType(position) == FOOT_TYPE)) { holder.tvFootView.setText(footViewText); // 刷新太快 所以使用Hanlder延遲兩秒 Handler handler = new Handler(); handler.postDelayed(new Runnable() {@Overridepublic void run() { max_count += 5; notifyDataSetChanged();} }, 1000);} else { holder.t2.setText(subjectsBean.getTitle()); holder.t3.setText(subjectsBean.getRate()); Glide.with(mContext).load(subjectsBean.getCover()).into(holder.i1);} } @Override public int getItemCount() {if (mData.size() <= max_count) { return mData.size();}return max_count; } //創建一個方法來設置footView中的文字 public void setFootViewText(String footViewText) {isFootView = true;this.footViewText = footViewText; }}

(6)網絡請求

public void getDate(DateModel dateModel) {if(dateModel==null||dateModel.getSubjects()==null){ Toast.makeText(MainActivity.this,'失敗',Toast.LENGTH_SHORT).show(); return;}Toast.makeText(MainActivity.this,'成功',Toast.LENGTH_SHORT).show();adapter=new Adapter(MainActivity.this,dateModel.getSubjects());adapter.setFootViewText('加載中...');r1.setAdapter(adapter);s1.setRefreshing(false); } public void requestDate() {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';OkHttpClient okHttpClient = new OkHttpClient();final Request request = new Request.Builder().url(url).get().build();Call call = okHttpClient.newCall(request);call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) {runOnUiThread(new Runnable() { @Override public void run() {Toast.makeText(MainActivity.this, '網絡連接失敗', Toast.LENGTH_SHORT).show(); }}); } @Override public void onResponse(Call call, Response response) throws IOException {String result = response.body().string();Gson gson = new Gson();final DateModel dateModel = gson.fromJson(result, DateModel.class);runOnUiThread(new Runnable() { @Override public void run() {Toast.makeText(MainActivity.this, '網絡連接成功', Toast.LENGTH_SHORT).show();getDate(dateModel); }}); }}); }

(7)功能實現

Android 使用RecycleView列表實現加載更多的示例代碼

linearLayoutManager=new LinearLayoutManager(MainActivity.this);linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);r1.setLayoutManager(linearLayoutManager);requestDate();s1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() {new Handler().postDelayed(new Runnable() { @Override public void run() {requestDate(); }},1000); }});

(8)源代碼點擊下載

到此這篇關于Android 使用RecycleView列表實現加載更多的文章就介紹到這了,更多相關Android加載更多內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 综合管廊模具_生态,阶梯护坡模具_检查井模具制造-致宏模具厂家 | 浇注料-高铝砖耐火砖-郑州凯瑞得窑炉耐火材料有限公司 | 仓储笼_仓储货架_南京货架_仓储货架厂家_南京货架价格低-南京一品仓储设备制造公司 | 胃口福饺子加盟官网_新鲜现包饺子云吞加盟 - 【胃口福唯一官网】 | 冷藏车-东风吸污车-纯电动环卫车-污水净化车-应急特勤保障车-程力专汽厂家-程力专用汽车股份有限公司销售二十一分公司 | 美甲贴片-指甲贴片-穿戴美甲-假指甲厂家--薇丝黛拉 | 磁粉制动器|张力控制器|气胀轴|伺服纠偏控制器整套厂家--台灵机电官网 | 新材料分散-高速均质搅拌机-超声波分散混合-上海化烁智能设备有限公司 | 上海网站建设-上海网站制作-上海网站设计-上海做网站公司-咏熠软件 | 药品冷藏箱厂家_低温冰箱_洁净工作台-济南欧莱博电子商务有限公司官网 | 微信小程序定制,广州app公众号商城网站开发公司-广东锋火 | 苏州柯瑞德货架-仓库自动化改造解决方案 | 反渗透水处理设备|工业零排放|水厂设备|软化水设备|海南净水设备--海南水处理设备厂家 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | 脉冲除尘器,除尘器厂家-淄博机械 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 浙江筋膜枪-按摩仪厂家-制造商-肩颈按摩仪哪家好-温州市合喜电子科技有限公司 | 送料机_高速冲床送料机_NC伺服滚轮送料机厂家-东莞市久谐自动化设备有限公司 | SOUNDWELL 编码器|电位器|旋转编码器|可调电位器|编码开关厂家-广东升威电子制品有限公司 | 压接机|高精度压接机|手动压接机|昆明可耐特科技有限公司[官网] 胶泥瓷砖胶,轻质粉刷石膏,嵌缝石膏厂家,腻子粉批发,永康家德兴,永康市家德兴建材厂 | 袋式过滤器,自清洗过滤器,保安过滤器,篮式过滤器,气体过滤器,全自动过滤器,反冲洗过滤器,管道过滤器,无锡驰业环保科技有限公司 | 越南专线物流_东莞国际物流_东南亚专线物流_行通物流 | 塑钢件_塑钢门窗配件_塑钢配件厂家-文安县启泰金属制品有限公司 深圳南财多媒体有限公司介绍 | 铁艺,仿竹,竹节,护栏,围栏,篱笆,栅栏,栏杆,护栏网,网围栏,厂家 - 河北稳重金属丝网制品有限公司 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 瓶盖扭矩测试仪-瓶盖扭力仪-全自动扭矩仪-济南三泉中石单品站 | 吉祥新世纪铝塑板_生产铝塑板厂家_铝塑板生产厂家_临沂市兴达铝塑装饰材料有限公司 | 【官网】博莱特空压机,永磁变频空压机,螺杆空压机-欧能优 | ET3000双钳形接地电阻测试仪_ZSR10A直流_SXJS-IV智能_SX-9000全自动油介质损耗测试仪-上海康登 | 赛默飞Thermo veritiproPCR仪|ProFlex3 x 32PCR系统|Countess3细胞计数仪|371|3111二氧化碳培养箱|Mirco17R|Mirco21R离心机|仟诺生物 | 考勤系统_人事考勤管理系统_本地部署BS考勤系统_考勤软件_天时考勤管理专家 | 节流截止放空阀-不锈钢阀门-气动|电动截止阀-鸿华阀门有限公司 | 有源电力滤波装置-电力有源滤波器-低压穿排电流互感器|安科瑞 | 2025黄道吉日查询、吉时查询、老黄历查询平台- 黄道吉日查询网 | ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | 许昌奥仕达自动化设备有限公司 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 安全阀_弹簧式安全阀_美标安全阀_工业冷冻安全阀厂家-中国·阿司米阀门有限公司 | 淘趣英语网 - 在线英语学习,零基础英语学习网站 | 橡胶电子拉力机-塑料-微电脑电子拉力试验机厂家-江苏天源 | 高通量组织研磨仪-多样品组织研磨仪-全自动组织研磨仪-研磨者科技(广州)有限公司 | 膏剂灌装旋盖机-眼药水灌装生产线-西林瓶粉剂分装机-南通博琅机械科技 |