RecyclerView的简单使用
简单讲讲RecyclerView的使用:(这里用的是androidX,而不是之前的supprot-v7,用法基本一样)
1、引入recyclerView的依赖库:
implementation 'androidx.recyclerview:recyclerview:1.0.0'
2、主布局内添加RecyclerView的布局控件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
3、添加一个适配器,继承自RecyclerView.Adapter
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
//这里的要实现ViewHolder,ListView里没有强制使用ViewHolder
Context context;
List<String> list;
public RecyclerAdapter(Context context, List<Data> list) {
//这里添加一个构造方法,方便使用
this.context = context;
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//实例化ViewHolder,并通过inflate来把ItemView传递过去
ViewHolder viewHolder=new ViewHolder(View.inflate(context,R.layout.recycler_item,null));
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.textView.setText(list.get(position).getTiTle());
}
@Override
public int getItemCount() {
return list.size();
//返回列表的大小
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.textView);
}
}
}
4、Data类
public class Data {
String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
5、Activity
public class MainActivity extends AppCompatActivity {
List<Data> list;
RecyclerAdapter adapter;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recyclerView);
list=new ArrayList<>();
adapter=new RecyclerAdapter(this,list); //实例化适配器
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//设置布局管理器,如果实现网格布局,可以用GridLayoutManager
recyclerView.setAdapter(adapter); //设置适配器
}
}
最后讲一点,因为RecyclerView没有实现单击和长按的功能,所以要自己写
在适配器里添加回调接口实现:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.textView.setText(list.get(position).getTitle());
/*********************************/
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(position);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onItemLongClickListener.onItemLongClick(position);
return true;
}
});
/**************************************/
}
/*实现单击和长按*/
OnItemLongClickListener onItemLongClickListener;
OnItemClickListener onItemClickListener;
interface OnItemClickListener{
void onItemClick(int position);
}
interface OnItemLongClickListener{
void onItemLongClick(int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener=onItemClickListener;
}
public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener){
this.onItemLongClickListener=onItemLongClickListener;
}
/*实现单击和长按*/
---Activity
adapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(MainActivity.this, "pos"+position, Toast.LENGTH_SHORT).show();
}
});