Android --Recyclerview 使用案例
步骤:
(1)添加依赖
(2)编写 Recyclerview子布局页面(xml)和子布局实体类(java)
(3)编写 Adapter 适配器类
(4)页面中调用 Recuclerview (activity和xml)
(1)在 app 下的 build.gradle 中添加 RecyclerView 依赖
//添加 RecyclerView 依赖包 implementation 'androidx.recyclerview:recyclerview:1.1.0'
(2)编写 Recyclerview子布局页面(xml)和子布局实体类(java)
recyclerview_item.xml (一个TextView 和一个Button)
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:textSize="30sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt"/> </LinearLayout>
Bean.java (一个 textView的数据和一个 ButtonText的数据)
package xyz.lushangg.t8.recyclerview;
public class Bean { private String name; private String buttonText;
public String getName(){ return name; } public void setName(String name){ this.name=name; } public String getButtonText() { return buttonText; } public void setButtonText(String buttonText) { this.buttonText = buttonText; } }
(3)编写 Adapter 适配器类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package xyz.lushangg.t8.recyclerview; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; import xyz.lushangg.t8.R; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { //导入数据 private List<Bean> data; //Context private Context context; //构造 public MyAdapter(List<Bean> data, Context context) { this.data = data; this.context = context; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { //获取布局 View view=View.inflate(context, R.layout.recyclerview_item,null); return new MyViewHolder(view); } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { //赋值给 tv holder.tv.setText(data.get(position).getName()); holder.bt.setText("按钮"+data.get(position).getButtonText()); } @Override public int getItemCount() { return data==null?0: data.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { private TextView tv; private Button bt; public MyViewHolder(@NonNull View itemView) { super(itemView); tv=itemView.findViewById(R.id.tv); bt=itemView.findViewById(R.id.bt); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(mOnItemClickListener!=null){ //调用按钮点击接口方法 mOnItemClickListener.onButtonClick(getAdapterPosition()); } } }); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(mOnItemClickListener !=null){ //调用接口的方法 mOnItemClickListener.onRecyclerItemClick(getAdapterPosition()); } } }); } } //声明接口 private OnRecyclerItemClickListener mOnItemClickListener; //赋值监听事件 public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener){ mOnItemClickListener=listener; } //定义接口 public interface OnRecyclerItemClickListener{ void onRecyclerItemClick(int position); void onButtonClick(int position); } }
(4)页面中调用 Recuclerview (activity和xml)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rv"/> </LinearLayout>
MainActivity
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package xyz.lushangg.t8; 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; import xyz.lushangg.t8.recyclerview.Bean; import xyz.lushangg.t8.recyclerview.MyAdapter; 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("RecyclerView --"+i); bean.setButtonText("按钮--"+i); data.add(bean); } //导入 view RecyclerView recyclerView= (RecyclerView) findViewById(R.id.rv); //为recyclerView 添加布局方向 //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("点击Item项监听:","Click:"+position); } @Override public void onButtonClick(int position) { Log.e("点击按钮:","Click:"+position); } }); } }
效果: