Androidx RecyclerView 基本使用

Posted Gatsby

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Androidx RecyclerView 基本使用相关的知识,希望对你有一定的参考价值。

_iamjerry  https://www.jianshu.com/p/b4bb52cdbeb7

一、用法:
RecyclerView和其他用于界面数据滑动展示的控件(GridView,ListView,Spinner等)一样,都少不了数据源,适配器,以及监听逻辑处理这三块。
下面就来讲解写RecyclerView的使用方法:
1.引用(导包)
2.布局文件引用
3.构造适配器
4.主程序,包括数据源以及逻辑处理等

 

上两个简单的效果图:
StaggeredGridLayoutManager 以瀑布流方式展示Item

 LinerLayoutManager 以垂直或者水平列表方式展示Item

 

二、贴代码时间: 

Step1:引用(导包)

2.1 直接在build.gradle(Module:app)的dependencies添加

implementation \'androidx.recyclerview:recyclerview:1.2.0-alpha03\'

 Step2:布局

2.2 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

2.3 crush_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/crush_image"
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:layout_gravity="left"
        tools:background="@drawable/image0" />

    <TextView
        android:id="@+id/crush_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="40dp"
        tools:text="CrushText" />

</LinearLayout>

 Step3:构造适配器

封装 2.4 Crush.java

package com.gatsby.recyclertest;

public class Crush {

    private  String name;

    private int imageId;

    public Crush(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    }

    public String getName(){
        return  name;
    }

    public int getImageId(){
        return imageId;
    }

}

2.5 CrushAdapter.java
2.5.1 为RecyclerView新增适配器CrshAdapter,并让其继承于RecyclerView.Adapter,把泛型指定为CrushAdapter.ViewHolder。
2.5.2 定义内部类ViewHolder,并继承RecyclerView.ViewHolder。传入的View参数通常是RecyclerView子项的最外层布局。
2.5.3 CrushAdapter构造函数,用于把要展示的数据源传入,并赋予值给全局变量mCrushList
2.5.4继承RecyclerView.Adapter必须要重写的三个方法。 onCreateViewHolder(), onBindViewHolder, getItemCoun

方法 作用
onCreateViewHolder() 用于创建ViewHolder实例。这个方法将子项布局crush_item 加载进来并传入ViewHolder的构造函数,最后返回ViewHolder对象
onBindViewHolder()  此方法用于对子项的数据进行赋值,在每个子项滚动到屏幕内的时候执行
getItemCount()    用于返回RecyclerView的长度

 


  


 

 

 

 

package com.gatsby.recyclertest;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class CrushAdapter extends RecyclerView.Adapter<CrushAdapter.ViewHolder> {

    private List<Crush> mCrushList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View crushView;
        ImageView crushImage;
        TextView crushName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            crushView = itemView;
            crushImage = (ImageView) itemView.findViewById(R.id.crush_image);
            crushName = (TextView) itemView.findViewById(R.id.crush_name);
        }
    }

    public CrushAdapter(List<Crush> crushList) {
        mCrushList = crushList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.crush_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.crushView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Crush crush = mCrushList.get(position);
                Toast.makeText(v.getContext(), "you clicked view " + crush.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        holder.crushImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAbsoluteAdapterPosition();
                Crush crush = mCrushList.get(position);
                Toast.makeText(v.getContext(), "you clicked image " + crush.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Crush crush = mCrushList.get(position);
        holder.crushImage.setImageResource(crush.getImageId());
        holder.crushName.setText(crush.getName());
    }

    @Override
    public int getItemCount() {
        return mCrushList.size();
    }


}

 

 2.7 MainActivity.java

package com.gatsby.recyclertest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;

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

public class MainActivity extends AppCompatActivity {

    private List<Crush> crushList = new ArrayList<Crush>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initCrush();
        CrushRecyclerView();
    }

    private  void  CrushRecyclerView(){
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        /*StaggeredGridLayoutManager layoutManager = new
                StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);*/
        recyclerView.setLayoutManager(layoutManager);
        CrushAdapter adapter = new CrushAdapter(crushList);
        recyclerView.setAdapter(adapter);
    }

    private void initCrush() {
        for (int i = 0; i < 2; i++) {
            Crush image0 = new Crush("image0", R.drawable.image0);
            crushList.add(image0);
            Crush image01 = new Crush("image01", R.drawable.image01);
            crushList.add(image01);
            Crush image02 = new Crush("image02", R.drawable.image02);
            crushList.add(image02);
            Crush image03 = new Crush("image03", R.drawable.image03);
            crushList.add(image03);
            Crush image04 = new Crush("image04", R.drawable.image04);
            crushList.add(image04);
            Crush image05 = new Crush("image05", R.drawable.image05);
            crushList.add(image05);
            Crush image06 = new Crush("image06", R.drawable.image06);
            crushList.add(image06);
            Crush image07 = new Crush("image07", R.drawable.image07);
            crushList.add(image07);
            Crush image08 = new Crush("image08", R.drawable.image08);
            crushList.add(image08);
            Crush image09 = new Crush("image09", R.drawable.image09);
            crushList.add(image09);
            Crush image10 = new Crush("image10", R.drawable.image10);
            crushList.add(image10);
            Crush image11 = new Crush("image11", R.drawable.image11);
            crushList.add(image11);
            Crush image12 = new Crush("image12", R.drawable.image12);
            crushList.add(image12);
            Crush image13 = new Crush("image13", R.drawable.image13);
            crushList.add(image13);
        }
    }

}

 

 

以上是关于Androidx RecyclerView 基本使用的主要内容,如果未能解决你的问题,请参考以下文章

RecyclerView的简单使用

为啥我不能将数据从 RecyclerView 传递到另一个 RecyclerView - AndroidX

androidx.recyclerview.widget.RecyclerView的使用方法

RecyclerView StaggeredGridLayoutManager FindFirstVisibleItemPositions 始终为 0 (androidx)

使用 Serializable - AndroidX 将数据从 RecyclerView 传递到 RecyclerViewMore

尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setLayoutManager()”