Android 使用RecycleView列表实现加载更多的示例代码

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">

    <LinearLayout
        android: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="@mipmap/logo"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical">
            <LinearLayout
                android: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">

                    <TextView
                        android: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.RecyclerView
                android:id="@+id/r1"
                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">
    <TextView
        android:id="@+id/t2"
        android:layout_width="0dp"
        android:layout_weight="1.5"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="我不是药神"/>
    <TextView
        android:id="@+id/t3"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="9.0"/>
    <ImageView
        android:id="@+id/i1"
        android:layout_width="0dp"
        android:layout_weight="1.5"
        android:layout_height="150dp"
        android:padding="20dp"
        android:src="@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:id="@+id/tv_foot"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="10dp"
          android:gravity="center"
          tools:text="下拉刷新"
          android:orientation="vertical"/>

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

(4)绑定控件ID

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

(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_TYPE
        if (isFootView && (getItemViewType(position) == FOOT_TYPE)) {
            holder.tvFootView.setText(footViewText);
            // 刷新太快 所以使用Hanlder延迟两秒
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public 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)功能实现

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加载更多内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • android中RecycleView添加下滑到底部的监听示例

    我们的日常开发中经常用到下拉刷新,而网上评价最好的开源下拉刷新组件当然还是android-Ultra-Pull-To-Refresh此组件可以给任何的控件添加下拉刷新功能.当然也包括recycleview了. 可惜android-Ultra-Pull-To-Refresh只是提供了下拉刷新的功能,但是对于列表类的组件,我们日常开发中更多的会用到其上拉加载或者滑到底部自动加载的功能,当然目前来看用户更喜欢滑到底部自动加载的功能.就比如今天说的recycleview我们只能自己给其添加滑到底部加载更

  • Android ExpandableListView实现下拉刷新和加载更多效果

    支持下拉刷新和加载更多的ExpandableListView,供大家参考,具体内容如下 模拟器有点卡,滑动的时候鼠标不方便 怎么用: XML中声明 <com.xingyi.elonggradletaskdemo.widget.SExpandableListView android:listSelector="@android:color/transparent" android:id="@+id/elv_coupon" android:layout_width

  • Android中使用ScrollView实现滑动到底部显示加载更多

    这是效果 主要是onTouchListener监听事件,监视什么时候滑到底部 同时要理解getMeasuredHeight和getHeight的区别 getMeasuredHeight:全部的长度 包括隐藏的 getHeight:在布局中展示出来的长度 布局文件: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_paren

  • Android中RecycleView与ViewPager冲突的解决方法及原理

    1.概述 在实际开发中,我们经常遇到需要在ListView或RecycleView头部添加ViewPager实现Banner轮播效果,并需要添加下拉刷新,上拉加载功能. 但,横向滑动ViewPager时,稍微偏移就会失去焦点. 2.解决方案 我们知道事件拦截的顺序,父ViewGroup先接收到拦截,再传递给子ViewGroup 或子View.事件的处理顺序是,子ViewGroup 或子View先处理,若子ViewGroup处理了,父ViewGroup就不用处理,若子ViewGroup未处理,则传

  • Android实践之带加载效果的下拉刷新上拉加载更多

    前言 之前写的一个LoadingBar,这次把LoadingBar加到下拉刷新的头部.从头写一个下拉刷新,附赠上拉加载更多.下面话不多说了,来一起看看详细的介绍吧. 效果图: 实现过程 首先是自定义属性,attrs.xml中定义头部的高度和上下的padding. ####attrs.xml#### <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable

  • android使用PullToRefresh框架实现ListView下拉刷新上拉加载更多

    本文实例为大家分享了Android实现ListView下拉刷新上拉加载更多的具体代码,供大家参考,具体内容如下 其实谷歌官方目前已经推出ListView下拉刷新框架SwipeRefreshLayout,想了解的朋友可以点击 android使用SwipeRefreshLayout实现ListView下拉刷新上拉加载了解一下: 大家不难发现当你使用SwipeRefreshLayout下拉的时候布局文件不会跟着手势往下滑,而且想要更改这个缺陷好像非常不容易. 虽然SwipeRefreshLayout非

  • Android RecyclerView添加上拉加载更多效果

    先看一下效果 刷新的时候是这样的: 没有更多的时候是这样的: 既然有刷新的时候有两种状态就要定义两个状态 //普通布局的type static final int TYPE_ITEM = 0; //脚布局 static final int TYPE_FOOTER = 1; 在特定的时候去显示: @Override public int getItemViewType(int position) { //如果position加1正好等于所有item的总和,说明是最后一个item,将它设置为脚布局

  • Android RecycleView使用(CheckBox全选、反选、单选)

    本文实例为大家分享了CheckBox全选.反选.单选的具体代码,供大家参考,具体内容如下 MainActiivity package com.bwie.day06; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.Recyc

  • Android 使用RecycleView列表实现加载更多的示例代码

    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&q

  • Android Flutter实现上拉加载组件的示例代码

    前言 在此之前对列表下拉刷新做了调整方案,具体介绍可以阅读下拉刷新组件交互调整.既然列表有下拉刷新外当然还有上拉加载更多操作了,本次就来介绍如何为列表增加上拉加载更多的交互实现. 实现方案 上拉刷新实现形式上可以有多种实现方式,若不带交互形式可采用NotificationListener组件监听滑动来实现上拉加载更多:如果对操作交互上有一定要求期望上拉刷新带有直观动画可操作性就需要实现一定样式来实现了. 监听NotificationListener实现 NotificationListener(

  • vue loadmore组件上拉加载更多功能示例代码

    最近在做移动端h5页面,所以分页什么的就不能按照传统pc端的分页器的思维去做了,这么小的屏幕去点击也不太方便一般来讲移动端都是上拉加载更多,符合正常使用习惯. 首先简单写一下模板部分的html代码,,很简单清晰的逻辑: <template> <div class="loadmore"> <div class="loadmore__body"> <slot></slot> </div> <d

  • Android ListView下拉刷新上拉自动加载更多DEMO示例

    代码下载地址已经更新.因为代码很久没更新,已经很落伍了,建议大家使用RecyclerView实现. 参考项目: https://github.com/bingoogolapple/BGARefreshLayout-Android https://github.com/baoyongzhang/android-PullRefreshLayout 下拉刷新,Android中非常普遍的功能.为了方便便重写的ListView来实现下拉刷新,同时添加了上拉自动加载更多的功能.设计最初是参考开源中国的And

  • ListView实现下拉刷新加载更多的实例代码(直接拿来用)

    ListView Api bixu 好好看看 mNewsAdapter.notifyDataSetChanged();//刷新ListView 自定义的RefreashListView package com.itguang.dell_pc.myapplication.view; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import and

  • php+jquery+html实现点击不刷新加载更多的实例代码

    基本原理:页面载入时,jQuery向后台请求数据,PHP通过查询数据库将最新的几条记录显示在列表页,在列表页的底部有个"更多"链接,通过触发该链接,向服务端发送Ajax请求,后台PHP程序得到请求参数,并作出相应,获取数据库相应的记录并以JSON的形式返回给前台页面,前台页面jQuery解析JSON数据,并将数据追加到列表页.其实就是Ajax分页效果. HTML 首先要引入jquery库和jquery.more.js插件,jquery.more.js已经将许多功能都封装好了,并提供了参

  • Android仿支付宝笑脸刷新加载动画的实现代码

    看到支付宝的下拉刷新有一个笑脸的动画,因此自己也动手实现一下.效果图如下: 一.总体思路 1.静态部分的笑脸. 这一部分的笑脸就是一个半圆弧,加上两颗眼睛,这部分比较简单,用于一开始的展示. 2.动态笑脸的实现. 2.1.先是从底部有一个圆形在运动,运动在左眼位置时把左眼给绘制,同时圆形继续运动,运动到右眼位置时绘制右眼,圆形继续运动到最右边的位置. 2.2.当上面的圆形运动到最右边时候,开始不断绘制脸,从右向左,脸不断增长,这里脸设置为接近半个圆形的大小. 2.3.当脸画完的时候,开始让脸旋转

  • vue中实现滚动加载更多的示例

    在以前的前端刀耕火种时代要实现滚动加载更多想要大家都是很快实现了,在vue会有一点麻烦,最近自己研究了一下,做了一个简单的demo,供大家参考: <template> <div> <ul> <li v-for="item in articles"> <h2>{{item.title}}</h2> <img :src="item.images" alt=""> &l

  • vue底部加载更多的实例代码

    要实现的效果如下: <template> <div class="newsList"> <div v-for="(items, index) in newsList"> <div class="date">{{showDay(index)}}</div> <div class="list" > <ul> <li class="l

  • Android Recyclerview实现上拉加载更多功能

    在项目中使用列表的下拉刷新和上拉加载更多是很常见的功能,下拉刷新我们可以用Android自带的SwipeRefreshLayout这个很好解决.但是上拉加载更多就要去找一些框架了,刚开始的时候我找到一个Mugen的github开源框架,但是有个问题,当页面能够一次加载全部item的时候,上拉加载的功能就失效了. 这是因为当界面一次能够加载完全部item的时候,继续往上拉,Recyclerview的滑动监听,中的onScrolled方法只会在页面加载的时候调用一次,只后就不会被调用了,并且dy=0

随机推荐