安卓开发中RecyclerView比ListView和GridView更优
介绍
RecyclerView是ListView和GridView的继承者,有它俩的优点,也弥补了它们的不足;RecyclerView使用起来更灵活、简洁、更漂亮,更能实现我们想要的效果。
RecyclerView使用的很广泛,如下面的都能用RecyclerView实现
淘宝
网易云
下面我们就大概做这样的一个效果
使用
1、创建2个布局文件
一个放有RecyclerView组件
一个有是实现的样式xml
activity_mian.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_test" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
myrecycler.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="110dp" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView
android:id="@+id/img_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_rubbish01" android:layout_gravity="center_horizontal" /> <TextView
android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="题目" android:textSize="20dp" /> </LinearLayout>
如图:
2、创建bean类用来保存myrecycler.xml中的组件的数据,创建get和set方法
infobean.java
package com.example.xianyu; public class infoBean { private int picture; private String title; infoBean(int picture,String title){ this.picture = picture; this.title = title; } public int getPicture() { return picture; } public void setPicture(int picture) { this.picture = picture; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
3、创建适配器,并设置item的点击事件
要点:
(1)、适配器继承自RecyclerView.Adapter
(2)、适配器里的泛型需自定义,并且继承自RecyclerView.ViewHolder,主要是实例化组件
(3)、onBindViewHolder方法,利用传进来的ArrayList,进行绑定数据
(4)、执行顺序:
onCreateViewHolder(找到布局)—>MyViewHolder(找到组件) —>onBindViewHolder(绑定数据)
myadapter.java
package com.example.xianyu; import android.content.Context; 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.ArrayList; import java.util.List; public class myadapter extends RecyclerView.Adapter<myadapter.MyViewHolder>{ private final Context context; List<infoBean> data = new ArrayList<infoBean>(); /*
自定义的构造方法 ,传入上下文对象context 和data数据
*/ myadapter(Context context, List<infoBean> data){ this.context = context; this.data = data; } /*
getview
*/ @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(context,R.layout.myrecycler,null); return new MyViewHolder(view); } /*
绑定数据
*/ @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { infoBean bean = data.get(position); holder.picture.setImageResource(bean.getPicture()); holder.title.setText(bean.getTitle()); //设置item的点击事件 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context,bean.getTitle(), Toast.LENGTH_LONG).show(); } }); } /*
*得到总条数,返回data.size() 就可以了
*/ @Override public int getItemCount() { return data.size(); } /*
也是getview
*/ class MyViewHolder extends RecyclerView.ViewHolder{ private ImageView picture; private TextView title; public MyViewHolder(@NonNull View itemView) { super(itemView); picture = itemView.findViewById(R.id.img_picture); title = itemView.findViewById(R.id.tv_title); } } }
4、在主Activity设置RecyclerView
homeActivity.java
package com.example.xianyu; import android.app.Activity; import android.os.Bundle; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class homeActivity extends Activity { private RecyclerView mrecycler; List<infoBean> data = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mian); initDate(); mrecycler = findViewById(R.id.recycler_test); //设置适配器 mrecycler.setAdapter(new myadapter(homeActivity.this,data)); //设置样式 ,有如下: // LinearLayoutManager(Context context, @RecyclerView.Orientation int orientation,boolean reverseLayout) //GridLayoutManager(Context context, int spanCount) //StaggeredGridLayoutManager(int spanCount, int orientation) mrecycler.setLayoutManager(new GridLayoutManager(homeActivity.this,3)); } //数据 private void initDate() { data.add(new infoBean(R.drawable.ic_rubbish02,"干")); data.add(new infoBean(R.drawable.ic_rubbish03,"湿")); data.add(new infoBean(R.drawable.ic_rubbish01,"有害")); data.add(new infoBean(R.drawable.ic_rubbish04,"其他")); data.add(new infoBean(R.drawable.ic_rubbish05,"回收")); data.add(new infoBean(R.drawable.ic_rubbish06,"厨余")); } }
实现图:
总结
非常有用,大家快快收藏叭!
Blog如有不对,敬请斧正
本文地址:https://blog.csdn.net/weixin_44758662/article/details/108942044