RecyclerView
一个用于大量数据展示的新控件,用来代替ListView
依赖
使用前需要导入依赖
//添加recyclerView依赖包
implementation 'androidx.recyclerview:recyclerview:1.1.0'
实例练习
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"
>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv"
android:textSize="30sp"/>
</LinearLayout>
创建一个recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:textSize="30sp"
/>
</LinearLayout>
创建两个类 Bean
package com.example.mylistview;
public class Bean {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MyAdapter 该类中每个方法有着相应的注释 并且在此类中绑定了项点击事件
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> 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) {
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.tv);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onitemClickListener!=null){
onitemClickListener.onRecyclerItemclick(getAdapterPosition());
}
}
});
}
}
private OnRecyclerItemClickListener onitemClickListener;
public void setRecyclerItemClickListener(OnRecyclerItemClickListener onitemlistener){
onitemClickListener=onitemlistener;
}
public interface OnRecyclerItemClickListener{
void onRecyclerItemclick(int position);
}
}
MainActivity
package com.example.myrecyclerview;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
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++){
if(i%2!=0){
continue;
}
Bean bean=new Bean();
bean.setName("博客CSDN碰磕"+i);
data.add(bean);
}
RecyclerView recyclerView=findViewById(R.id.rv);
StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
MyAdapter adapter=new MyAdapter(data,this);
recyclerView.setAdapter(adapter);
adapter.setRecyclerItemClickListener(new MyAdapter.OnRecyclerItemClickListener() {
@Override
public void onRecyclerItemclick(int position) {
Log.e("CSDN", "点击成功" + position);
}
});
}
}
RecyclerView可作多个布局显示,此处练习了 线性布局,网格布局, 最后以瀑布流结尾 最终实例效果: item点击事件触发效果:
|