RecyclerView与SwipeRefreshLayout 刷新微件的组合使用(附demo源码)

什么是RecyclerView?

RecyclerView本身就是一个视图,与listView的使用方法大同小异,与listView相比更加灵活,能够动态的创建元素。Google现在推荐使用Recyclerview。listView,GirdView已都不被推荐使用了

如何使用RecyclerView?

添加相关依赖

implementation'androidx.recyclerview:recyclerview:1.1.0'

定义布局管理器

你可以使用RecyclerView库的三种布局管理器之一,LinerLayoutManager,GridLayoutManager,StaggeredGridLayoutManager

例如:

LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);

定义Adapter,ViewHolder

定义Adapter,继承 RecyclerView.Adapter,一定要复写三个方法onCreateViewHolder(),onBindViewHolder(),getItemCount()。

定义ViewHolder需要继承 RecyclerView.ViewHolder

Adapter需要时会创建ViewHolder对象,创建时都会调用onCreateViewHolder()方法,

例如:

@NonNull
@Override
public CustomAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
    
    View view=View.inflate(context,R.layout.simple_item,null);//R.layout.simple_item为视图项的布局
    return new ViewHolder(view);
}

通过onBindViewHolder()将 ViewHolder 与数据相关联

例如:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
    
    holder.tv.setText(dataname[position]);//tv为TextView,dataneme[position]为要绑定的数据
    holder.iv.setImageResource(dataimage[position]);//iv为ImageView,dataImage[position]为要绑定的数据
}

SwipeRefreshLayout 下拉刷新微件

SwipeRefreshLayout的作用

灵活的向现有应用添加滑动刷新微件,检测下拉的滑动,显示一个独特的进度条,在应用里触发回调方法以实现刷新效果

如何使用SwipeRefreshLayout

添加依赖

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

将SwipeRefreshLayout微件添加到包含视图 的现有布局文件中

例如:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

定义SwipeRefreshLayout.setOnRefreshListener监听刷新手势

例如:

swipeRefreshLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
    
    
    @Override
    public void onRefresh() {
    
    
        Toast toast=Toast.makeText(MainActivity.this,"欢迎来到动物世界",Toast.LENGTH_SHORT);
        toast.show();
        swipeRefreshLayout.setRefreshing(false);
    }
});

demo

实现效果:下拉刷新时重新加载,并显示提示信息框

在这里插入图片描述

代码:

MainActivity.java

package com.example.test3;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    
    
    private  String[] names =new String[]{
    
    "Lion","Tiger","Monkey","Dog","Cat","Elephant"};
    private  int[] imageIds =new int[]{
    
    R.drawable.lion,R.drawable.tiger,R.drawable.monkey,R.drawable.dog,R.drawable.cat,R.drawable.elephant};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView=findViewById(R.id.rv);
        SwipeRefreshLayout swipeRefreshLayout=findViewById(R.id.swiperefresh);
        swipeRefreshLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
    
    
            @Override
            public void onRefresh() {
    
    
                Toast toast=Toast.makeText(MainActivity.this,"欢迎来到动物世界",Toast.LENGTH_SHORT);
                toast.show();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        CustomAdapter myAdapter=new CustomAdapter(names,imageIds,this);
        recyclerView.setAdapter(myAdapter);
    }
}

CustomerAdapter.java

package com.example.test3;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder>{
    
    
    private final String[] dataname;
    private final int[] dataimage;
    private Context context;
    public CustomAdapter(String[] datanameset,int[] dataimageset,Context context) {
    
    
        this.dataname = datanameset;
        this.dataimage=dataimageset;
        this.context=context;
    }
    @NonNull
    @Override
    public CustomAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
    
        View view=View.inflate(context,R.layout.simple_item,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
    
        holder.tv.setText(dataname[position]);//tv为TextView,dataneme[position]为要绑定的数据
        holder.iv.setImageResource(dataimage[position]);//iv为ImageView,dataImage[position]为要绑定的数据
    }

    @Override
    public int getItemCount() {
    
    
        return dataname.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
    
    
        private TextView tv;
        private ImageView iv;
        public ViewHolder(@NonNull View itemView) {
    
    
            super(itemView);
            tv=itemView.findViewById(R.id.name);
            iv=itemView.findViewById(R.id.header);
        }
    }
}

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"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

simple_item.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">
    <TextView
        android:id="@+id/name"
        android:layout_width="350dp"
        android:layout_height="60dp"
        android:gravity="center|left"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@drawable/textview_border"
        />
    <ImageView
        android:id="@+id/header"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="@+id/name"
        app:layout_constraintTop_toTopOf="parent"
         />
</androidx.constraintlayout.widget.ConstraintLayout>

textview_border.xml(设置文本边框的drawable文件)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

作者:许宇鹏
原文连接

猜你喜欢

转载自blog.csdn.net/fjnu_se/article/details/121879720
今日推荐