十七、RecyclerView

一、导入依赖包

dependencies {
//添加 RecyclerView的依赖包
implementation 'androidx.recyclerview:recyclerview:1.2.1'
}

二、代码示例

1.布局页面

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
 
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
 
    <TextView
        android:id="@+id/txt"
        android:textSize="15sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 
</LinearLayout>

  

2.后台代码

2.1 创建Bean类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.example.myrecyclerview;
 
public class Bean {
 
    private  String name;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}

  2.2  创建MyAdapter,继承 RecyclerView.Adapter ,需要手动创建实现 item 的点击监听事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.example.myrecyclerview;
 
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
import java.util.List;
 
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
 
    private List<Bean> data;
    private Context context;
 
    public  MyAdapter(List<Bean> data1,Context context1) {
        this.data=data1;
        this.context=context1;
    }
 
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取布局
        View view = View.inflate(context,R.layout.recycleview_list,null);
        return new MyViewHolder(view);
    }
 
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.tv.setText(data.get(position).getName());
    }
 
    /**
     * 显示多少个
     * */
    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }
 
    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv=itemView.findViewById(R.id.txt);
 
            //实现监听方法的回调
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener!=null)
                    {
                        mOnItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }
 
 
    //创建接口
    public interface  OnRecyclerItemClickListener{
        void onRecyclerItemClick(int position);
    }
 
    private OnRecyclerItemClickListener mOnItemClickListener;
 
    //创建设置监听的方法
    public void  setRecyclerItemClickListener(OnRecyclerItemClickListener listener){
        mOnItemClickListener = listener;
    }
}

  2.3 主程序调用,布局设置(注:瀑布流的显示效果会填充 没有使用的区域),最后实现 setRecyclerItemClickListener 的监听信息,进行item点击的回调显示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.example.myrecyclerview;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
 
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
 
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends AppCompatActivity {
 
    private List<Bean> data=new ArrayList<>();
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        for (int i=0;i<1000;i++){
            Bean bean=new Bean();
            bean.setName("享学"+i);
            data.add(bean);
        }
 
        //实现 RecyclerView
        RecyclerView recyclerView = findViewById(R.id.rv);
        //设置布局
        //垂直
        /*LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);*/
        //网格  整齐网格布局
        /*GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        recyclerView.setLayoutManager(gridLayoutManager);*/
        //瀑布流  自动填充剩余空间
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(3,
                        LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
 
        MyAdapter myAdapter = new MyAdapter(data,this);
        recyclerView.setAdapter(myAdapter);
 
        myAdapter.setRecyclerItemClickListener(new MyAdapter.OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(int position) {
                Log.e("tag", "onRecyclerItemClick: "+position );
            }
        });
    }
}

  

posted @   搬砖工具人  阅读(61)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示