亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android 使用RecycleView列表實(shí)現(xiàn)加載更多的示例代碼

瀏覽:138日期: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://m.lshqa.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='電影評(píng)分' /></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://m.lshqa.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列表實(shí)現(xiàn)加載更多的示例代碼

2.功能實(shí)現(xiàn)

(1)添加網(wǎng)絡(luò)權(quán)限

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

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

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)數(shù)據(jù)解析使用GsonFormat插件,快速將json字符串轉(zhuǎn)換成一個(gè)Java Bean,免去我們根據(jù)json字符串手寫對(duì)應(yīng)Java Bean的過程。定義一個(gè)類OneModel.class

public class OneModel implements Serializable {}

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

Android 使用RecycleView列表實(shí)現(xiàn)加載更多的示例代碼

(4)綁定控件ID

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

Android 使用RecycleView列表實(shí)現(xiàn)加載更多的示例代碼

(5)定義一個(gè)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;//數(shù)據(jù) private int max_count = 6;//最大顯示數(shù) private Boolean isFootView = false;//是否添加了FootView private String footViewText = '';//FootView的內(nèi)容 //兩個(gè)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存在,并且當(dāng)前位置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; } //創(chuàng)建一個(gè)方法來設(shè)置footView中的文字 public void setFootViewText(String footViewText) {isFootView = true;this.footViewText = footViewText; }}

(6)網(wǎng)絡(luò)請(qǐng)求

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, '網(wǎng)絡(luò)連接失敗', 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, '網(wǎng)絡(luò)連接成功', Toast.LENGTH_SHORT).show();getDate(dateModel); }}); }}); }

(7)功能實(shí)現(xiàn)

Android 使用RecycleView列表實(shí)現(xiàn)加載更多的示例代碼

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)源代碼點(diǎn)擊下載

到此這篇關(guān)于Android 使用RecycleView列表實(shí)現(xiàn)加載更多的文章就介紹到這了,更多相關(guān)Android加載更多內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 新婚第一次一级毛片 | 成人在线免费小视频 | 日韩欧美中文字幕在线播放 | 亚洲在成人网在线看 | 免费欧洲毛片a级视频 | www.日本在线 | 亚洲一区二区三区在线网站 | 欧美一级毛片特黄黄 | 久草视频国产 | 成人国内精品久久久久影院 | 精品欧美成人高清在线观看2021 | 在线久草视频 | 日本aaaa特级毛片 | 国产欧美日韩免费一区二区 | 在线观看国产日本 | 久久99久久99 | 韩国一级黄色大片 | 国产中文在线视频 | 国产视频中文字幕 | 精品一区二区三区视频在线观看 | 国产欧美综合在线一区二区三区 | 中文字幕成人在线 | 国产日韩欧美视频在线 | 特级a级毛片 | 国产成人午夜精品免费视频 | 亚洲第一视频在线观看 | 日本一级~片免费永久 | 韩日毛片| 一级毛片免费观看久 | 99视频精品在线 | 日本加勒比网站 | 国产高清精品一区 | 亚洲成人免费网站 | 黄色天堂 | 精品视频一区二区三区四区 | 日本阿v精品视频在线观看 日本阿v视频在线观看高清 | 免费看一级欧美激情毛片 | 一区二区三区亚洲视频 | 亚洲激情视频网 | 国产成人18黄网站免费网站 | 国产精品久久国产精品99盘 |