欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RecyclerView(1)实现列表,并添加监听事件

程序员文章站 2022-05-12 08:47:02
...

1实现效果图

RecyclerView(1)实现列表,并添加监听事件

2 学习参考

https://www.jianshu.com/p/69ac902e688c
请仔细看上面的参考文档,多读几遍能够更好的了解Adapter,View,ViewHolder,RecyclerView之间的关系

3 代码实现

1 android studio引入RecyclerView包

找到app文件夹下面的build.gradle
引入implementation ‘com.android.support:recyclerview-v7:26.1.0’
注意下图所示2者版本号一定要一样,
RecyclerView(1)实现列表,并添加监听事件
引入后点击Rebuild Project即可
RecyclerView(1)实现列表,并添加监听事件

2 布局文件

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--显示通讯录里面的信息-->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

recycleritem.xml文件

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <ImageView
        android:id="@+id/item_imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/item_textView1"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            />
        <TextView
            android:id="@+id/item_textView2"
            android:layout_width="wrap_content"
            android:layout_height="20dp" />
    </LinearLayout>



</LinearLayout>

所需图片image.png
RecyclerView(1)实现列表,并添加监听事件
所需对象存储类Information.java

/**
 * Created by Chen on 2020/7/24.
 */
//数据对象类
public class Information {
    private String textView1;
    private String textView2;
    private Object imageView;

    public Information(String textView1, String textView2, Object imageView){
        this.textView1=textView1;
        this.textView2=textView2;
        this.imageView=imageView;
    }
    public String getTextView1() {
        return textView1;
    }

    public void setTextView1(String textView1) {
        this.textView1 = textView1;
    }

    public String getTextView2() {
        return textView2;
    }

    public void setTextView2(String textView2) {
        this.textView2 = textView2;
    }

    public Object getImageView() {
        return imageView;
    }

    public void setImageView(Object imageView) {
        this.imageView = imageView;
    }
}

适配器类MyAdapter

package com.example.chen.myapplication;

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

import java.util.List;

/**
 * Created by Chen on 2020/7/24.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<Information> informationList;
    static class ViewHolder extends RecyclerView.ViewHolder {
        View InformationView;//创建视图对象,用于点击视图的时候实现
        TextView item_textView1,item_textView2;
        ImageView item_imageView;
        public ViewHolder(View view){
            super(view);
            InformationView=view;
            item_textView1=view.findViewById(R.id.item_textView1);
            item_textView2=view.findViewById(R.id.item_textView2);
            item_imageView=view.findViewById(R.id.item_imageView);
        }
    }
    public MyAdapter(List<Information> List){
        informationList =List;
    }
    @Override //创建viewholder
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view=  LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleritem,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        //整个视图添加监听事件
        holder.InformationView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position =holder.getAdapterPosition();
                Information information=informationList.get(position);
                Toast.makeText(view.getContext(),"你点击了"+information.getTextView1(),Toast.LENGTH_SHORT).show();
            }
        });
        //图片添加监听事件
        holder.item_imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position =holder.getAdapterPosition();
                Information information=informationList.get(position);
                Toast.makeText(view.getContext(),"你点击了图片"+information.getTextView1(),Toast.LENGTH_SHORT).show();
            }
        });

        return holder;
    }

    @Override //绑定数据
    public void onBindViewHolder(ViewHolder holder, int position) {
        Information information = informationList.get(position);
        holder.item_textView1.setText(information.getTextView1());
        holder.item_textView2.setText(information.getTextView2());
        holder.item_imageView.setImageResource((int) information.getImageView());

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



}

主类MainActivity

package com.example.chen.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

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

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.recycler_view);

        initView();
    }

    private void initView() {
        //获取数据信息
        List<Information> informationList=getInformatica();
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        MyAdapter myAdapter=new MyAdapter(informationList);
        recyclerView.setAdapter(myAdapter);
    }

    private List<Information>  getInformatica() {
        List<Information> informationList =new ArrayList<>();
        for(int i=0;i<30;i++){
            Information information=new Information("标题"+i,"显示的内容"+i,R.drawable.image);
            informationList.add(information);
        }
        return informationList;
    }
}

由于GitHub还不会上传代码,等我熟悉后,把代码上传.以上代码可以自己复制实现