RecyclerView的基础使用 +点击添加列表数据 +下拉刷新、上拉加载更多

一、RecyclerView的基础使用。

第一步:添加recyclerview控件。

第二步:创建布局文件(xml) + 单独的类控制布局里面的控件(MyViewHolder)。

第三步:创建一个适配器 MyAdapter,将布局文件复制n次,并将布局文件填充到recyclerview。

第四步:修改MainActivity代码。给recyclerview设置adapter和显示布局。

        recyclerView=findViewById(R.id.recycle);
        myAdapter=new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        //recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局文件的显示方式
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));//设置为水平显示
        //recyclerView.setLayoutManager(new GridLayoutManager(this,2));设置为两列显示

 二、添加按钮,点击添加数据。

        //往recyclerview里添加数据
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stringList.add("test7");
                stringList.add("test8");
                stringList.add("test9");
                stringList.add("test10");
                stringList.add("test11");
                stringList.add("test12");
                //重新刷新一下列表控件的数据
                myAdapter.notifyDataSetChanged();
            }
        });

三、第三方开源控件,下拉刷新、上拉加载更多。

1、添加依赖。bulid.gradle。

    implementation  'io.github.scwang90:refresh-layout-kernel:2.0.5'//核心必须依赖
    implementation  'io.github.scwang90:refresh-header-classics:2.0.5'//经典刷新头
    implementation  'io.github.scwang90:refresh-header-radar:2.0.5'//雷达刷新头
    implementation  'io.github.scwang90:refresh-header-falsify:2.0.5'//虚拟刷新头
    implementation  'io.github.scwang90:refresh-header-material:2.0.5' //谷歌刷新头
    implementation  'io.github.scwang90:refresh-header-two-level:2.0.5' //二级刷新头
    implementation  'io.github.scwang90:refresh-footer-ball:2.0.5' //球脉冲加载
    implementation  'io.github.scwang90:refresh-footer-classics:2.0.5' //经典加载

在gradle.properties文件里添加。

android.enableJetifier=true

2、在布局文件里加入SmartRefreshLayout布局。将recyclerview嵌入该布局。

<com.scwang.smart.refresh.layout.SmartRefreshLayout
    android:id="@+id/refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</com.scwang.smart.refresh.layout.SmartRefreshLayout>

3.在activity 中使用。

 四、源代码。

1、MainActivity

package com.hnucm.recyclerview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.header.TwoLevelHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    MyAdapter myAdapter;
    List<String> stringList=new ArrayList<>();//长度可变数组
    SmartRefreshLayout refresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.recycle);
        myAdapter=new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局文件的显示方式
        //recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));//设置为水平
        //recyclerView.setLayoutManager(new GridLayoutManager(this,2));设置为两列显示

        stringList.add("test1");
        stringList.add("test2");
        stringList.add("test3");
        stringList.add("test4");
        stringList.add("test5");
        stringList.add("test6");

        //往recyclerview里添加数据
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stringList.add("new test7");
                stringList.add("new test8");
                stringList.add("new test9");
                stringList.add("new test10");
                stringList.add("new test11");
                stringList.add("new test12");
                //重新刷新一下列表控件的数据
                myAdapter.notifyDataSetChanged();
            }
        });
        refresh=findViewById(R.id.refresh_layout);
        //设置刷新头部效果  TwoLevelHeader下拉可以进入二楼,类似QQ微信下拉
        refresh.setRefreshHeader(new TwoLevelHeader(this));
        //设置刷新尾部效果
        refresh.setRefreshFooter(new ClassicsFooter(this));
        //设置下拉刷新动作
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                stringList.add("Refresh test7");
                stringList.add("Refresh test8");
                myAdapter.notifyDataSetChanged();
                refresh.finishRefresh(2000);
            }
        });
        //设置上拉加载更多动作
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                stringList.add("add test7");
                stringList.add("add test8");
                myAdapter.notifyDataSetChanged();
                refresh.finishLoadMore(2000);
            }
        });

    }
    //将布局文件复制n次
    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
        //加载并返回布局文件
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {//设置显示
            //content 在内部类中传外部类。类名  不在内部类this
            View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item_chat,parent,false);
            MyViewHolder myViewHolder =new MyViewHolder(view);
            return myViewHolder;
        }
        //填充内容
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.textView.setText(stringList.get(position));
         //滚动控件的点击事件
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                    startActivity(intent);
                }
            });

        }
        //将布局复制的次数返回  记录有多少个recyclerview子项
        @Override
        public int getItemCount() {
            return stringList.size();
        }
    }

    //控制布局文件的控件
    public class MyViewHolder extends RecyclerView.ViewHolder{//拿控件

        TextView textView;
        ConstraintLayout rootView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.textView);
            rootView=itemView.findViewById(R.id.rootview);
        }
    }
}

2、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<com.scwang.smart.refresh.layout.SmartRefreshLayout
    android:id="@+id/refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</com.scwang.smart.refresh.layout.SmartRefreshLayout>



    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="30dp"
        android:layout_marginBottom="16dp"
        android:text="添加数据"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

3、item_chat.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootview"
    android:layout_width="match_parent"
    android:layout_height="80dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_marginStart="14dp"
        android:layout_marginTop="14dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="@+id/imageView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="@+id/imageView"
        app:layout_constraintStart_toStartOf="@+id/textView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的示例代码: 首先,在XML布局文件中添加一个RecyclerView和SwipeRefreshLayout控件: ```xml <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> ``` 然后,在Activity或Fragment中初始化RecyclerView和SwipeRefreshLayout,并设置相应的监听器: ```java // 初始化RecyclerView和SwipeRefreshLayout mRecyclerView = findViewById(R.id.recycler_view); mSwipeRefreshLayout = findViewById(R.id.swipe_refresh_layout); // 设置布局管理器和适配器 mLayoutManager = new LinearLayoutManager(this); mAdapter = new MyAdapter(mDataList); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.setAdapter(mAdapter); // 设置下拉刷新监听器 mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { // 进行下拉刷新操作 refreshData(); } }); // 设置上拉加载更多监听器 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && isSlideToBottom(recyclerView)) { // 滑动到底部,触发加载更多操作 loadMoreData(); } } }); // 判断RecyclerView是否滑动到底部 private boolean isSlideToBottom(RecyclerView recyclerView) { LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition(); int itemCount = layoutManager.getItemCount(); return lastVisibleItemPosition == (itemCount - 1); } // 进行下拉刷新操作 private void refreshData() { // 在这里进行刷新数据的操作 // 刷新完成后调用mSwipeRefreshLayout.setRefreshing(false)停止刷新动画 } // 进行上拉加载更多操作 private void loadMoreData() { // 在这里进行加载更多数据的操作 // 加载完成后调用mAdapter.notifyDataSetChanged()更新列表 } ``` 需要注意的是,上述代码中的MyAdapter是自定义的RecyclerView适配器,需要根据实际情况进行修改。另外,刷新和加载更多操作需要在子线程中进行,可以使用AsyncTask等方式进行异步处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千丘星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值