Android 上拉,下拉刷新。RecyclerView的使用(最后附完整代码)

<?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:id="@+id/c1"
    android:layout_height="180dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@mipmap/ss"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@mipmap/ss" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="209dp"
        android:layout_height="40dp"
        android:layout_marginStart="36dp"
        android:layout_marginTop="16dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="62dp"
        android:layout_height="40dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="@+id/textView2"
        app:layout_constraintStart_toStartOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="371dp"
        android:layout_height="79dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 我们可以看出上面的布局类似,只是内容不同。我们在写的时候不可能一个一个写,所以怎么解决?

 

步骤:

一:RecyclerView

 

二:我们新建布局文件:

三:修改布局文件的内容:

方法:我们需要单独创建一个内部类:

1:首先需要找到要修改的部件

2:然后进行修改

   //列表控件里面的内容修改,需要单独创建一个类
    //修改操作Item里面的控件

public class MyViewHolder extends RecyclerView.ViewHolder{//alt+enter显示修补程序
           TextView titleText;
           TextView contentText;
           ConstraintLayout constraintLayout;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            titleText=itemView.findViewById(R.id.textView2);
            contentText=itemView.findViewById(R.id.textView3);
            constraintLayout=itemView.findViewById(R.id.c1);
        }
    }

这些是找到修改的部件。

怎么修改我们需要看下面:

 

  //将列表控件复制n个并且填充到recycleview
    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
        //返回列表布局
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            //加载布局
            View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item_chat,parent,false);
            MyViewHolder myViewHolder=new MyViewHolder(view);
            return myViewHolder;

        }
        //position 位置
       //这里赋值
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
              String title=stringList.get(position);
            holder.titleText.setText(title);
              holder.contentText.setText("20crq");
              holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      Log.i("poss","sss");
                  }
              });
        }
    //列表布局显示个数
        @Override
        public int getItemCount() {
            return stringList.size();
        }
    }

 到这里我们既可以将组件加入了。当我们又很多这样的组件时,我们一个一个修改他的内容很麻烦,我们这里可以用一个数组来存储你需要填充的内容。

列布局显示的个数我们这时也可以用数组大小来显示。

 

怎么把数组的内容一个一个分配给配件属性:

 position 为组建的位置,第一个组件位置为0

这样的话,就可以给很多组件进行赋值。

下面我们来看看上拉,下拉怎么刷新:

一:我们需要加入依赖

代码:

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' //经典加载

二:添加:android.enableJetifier=true

 三:我们需要在布局文件:activity_main文件中加入布局

 四:在MainActivity中加入上拉,下拉

 

  smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
           @Override
           public void onRefresh(@NonNull RefreshLayout refreshLayout) {
             smartRefreshLayout.finishRefresh(2000);
               stringList.clear();
               stringList.add("下拉刷新");
               stringList.add("aaaa");
               stringList.add("bbbb");
               stringList.add("ccc");
               stringList.add("ddd");
               stringList.add("wwww");
               myAdapter.notifyDataSetChanged();
           }
       });
       smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
           @Override
           public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
               smartRefreshLayout.finishLoadMore(2000);
               stringList.add("上拉加载");
               stringList.add("aaaa");
               stringList.add("bbbb");
               stringList.add("ccc");
               stringList.add("ddd");
               stringList.add("wwww");
               myAdapter.notifyDataSetChanged();
           }
       });

最后完整代码:

MainActivity:

package com.example.class52;

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.os.Bundle;
import android.util.Log;
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.BezierRadarHeader;
import com.scwang.smart.refresh.header.ClassicsHeader;
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;
SmartRefreshLayout smartRefreshLayout;

//ArraryList 可变长度数组

List<String> stringList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //添加数据
        stringList.add("text1");
        stringList.add("text2");
        stringList.add("text3");
        stringList.add("text4");
        stringList.add("text5");

       recyclerView= findViewById(R.id.r1);
       myAdapter=new MyAdapter();
       recyclerView.setAdapter(myAdapter);
       smartRefreshLayout=findViewById(R.id.refresh);
       //smartRefreshLayout.setRefreshHeader(new ClassicsHeader(this));
        smartRefreshLayout.setRefreshHeader(new BezierRadarHeader(this));
       smartRefreshLayout.setRefreshFooter(new ClassicsFooter(this));
       smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
           @Override
           public void onRefresh(@NonNull RefreshLayout refreshLayout) {
             smartRefreshLayout.finishRefresh(2000);
               stringList.clear();
               stringList.add("下拉刷新");
               stringList.add("aaaa");
               stringList.add("bbbb");
               stringList.add("ccc");
               stringList.add("ddd");
               stringList.add("wwww");
               myAdapter.notifyDataSetChanged();
           }
       });
       smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
           @Override
           public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
               smartRefreshLayout.finishLoadMore(2000);
               stringList.add("上拉加载");
               stringList.add("aaaa");
               stringList.add("bbbb");
               stringList.add("ccc");
               stringList.add("ddd");
               stringList.add("wwww");
               myAdapter.notifyDataSetChanged();
           }
       });



       //一次显示一行
         recyclerView.setLayoutManager(new LinearLayoutManager(this));

        //recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
       //按钮添加点击事件,添加数据

        findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stringList.add("text11");
                stringList.add("text21");
                stringList.add("text31");
                stringList.add("text41");
                stringList.add("text51");
                //刷新数据
                myAdapter.notifyDataSetChanged();
            }
        });


    }
    //将列表控件复制n个并且填充到recycleview
    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
        //返回列表布局
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            //加载布局
            View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item_chat,parent,false);
            MyViewHolder myViewHolder=new MyViewHolder(view);
            return myViewHolder;

        }
        //position 位置
       //这里赋值
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
              String title=stringList.get(position);
              holder.titleText.setText(title);
              holder.contentText.setText("20crq");
              holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      Log.i("poss","sss");
                  }
              });
        }
    //列表布局显示个数
        @Override
        public int getItemCount() {
            return stringList.size();
        }
    }
    //列表控件里面的内容修改,需要单独创建一个类
    //修改操作Item里面的控件
    public class MyViewHolder extends RecyclerView.ViewHolder{//alt+enter显示修补程序
           TextView titleText;
           TextView contentText;
           ConstraintLayout constraintLayout;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            titleText=itemView.findViewById(R.id.textView2);
            contentText=itemView.findViewById(R.id.textView3);
            constraintLayout=itemView.findViewById(R.id.c1);
        }
    }
}
<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
<com.scwang.smart.refresh.layout.SmartRefreshLayout
    android:id="@+id/refresh"
    android:layout_width="match_parent"
    android:layout_height="354dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    tools:ignore="MissingConstraints">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/r1"
        android:layout_width="match_parent"
        android:layout_height="354dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</com.scwang.smart.refresh.layout.SmartRefreshLayout>

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加数据"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是一个Android RecyclerView实现上拉刷新下拉加载和工具栏的示例代码: 在build.gradle文件中添加以下依赖项: ``` implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.android.support:appcompat-v7:28.0.0' ``` 添加以下布局文件: ``` <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.v4.widget.SwipeRefreshLayout> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> ``` 在Activity或Fragment中添加以下代码: ``` private SwipeRefreshLayout swipeRefreshLayout; private RecyclerView recyclerView; private Toolbar toolbar; private LinearLayoutManager layoutManager; private MyAdapter adapter; // 在onCreate中添加以下代码 swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout); recyclerView = findViewById(R.id.recycler_view); toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); adapter = new MyAdapter(dataList); recyclerView.setAdapter(adapter); swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.colorAccent), getResources().getColor(R.color.colorPrimaryDark)); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { // 下拉刷新操作 // 可以在这里进行网络请求等操作 // 请求完成后,在主线程调用setRefreshing(false)方法结束刷新动画 swipeRefreshLayout.setRefreshing(false); } }); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && !recyclerView.canScrollVertically(1)) { // 上拉加载操作 // 可以在这里进行网络请求等操作 adapter.notifyDataSetChanged(); } } }); ``` 其中,MyAdapter是RecyclerView的适配器,dataList是数据列表。在适配器中,可以定义ViewHolder和绑定数据等方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值