为什么使用接口回调?
在recyclerView中,我们是没法像ListView直接添加setOnItemClick()方法的,所以我们需要用其他方法来实现点击事件
目前有两种实习的方法:
第一种是在RecyclerView中给控件直接加点击事件,这种方法虽然可以,但是有一定的局限性。
所以我今天来介绍通过在Adapter中通过接口来实现点击事件。
话不多说,直接实现。
首先先导入RecyclerView的依赖
implementation 'com.android.support:recyclerview-v7:27.1.1'
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/item_layout"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"/>
</LinearLayout>
只写一个recyclerView,不做解释
RecyclerView的适配器
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private List<String> list;
private Context context;
public RecyclerAdapter(List<String> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.item_layout,null);
MyViewHolder viewHolder = new MyViewHolder(view);
viewHolder.textView = view.findViewById(R.id.text_view);
viewHolder.linearLayout = view.findViewById(R.id.relativelayout);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
holder.textView.setText(list.get(position));
//给条目布局设置点击事件
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用接口方法,传递下标
listener.onItemClick(position);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(View itemView) {
super(itemView);
}
TextView textView;
RelativeLayout linearLayout;
}
//声明接口
private ItemClickListener listener;
//set方法
public void setListener(ItemClickListener listener) {
this.listener = listener;
}
//定义接口
public interface ItemClickListener{
//实现点击的方法,传递条目下标
void onItemClick(int position);
}
}
适配器里只要定义一个接口,给条目的布局加点击即可。
最后MainActivity中调用即可
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
List<String> list = new ArrayList<>();
for (int i = 0; i <100 ; i++) {
list.add("模拟数据"+i);
}
RecyclerAdapter adapter = new RecyclerAdapter(list,this);
recyclerView.setAdapter(adapter);
//调用点击的方法
adapter.setListener(new RecyclerAdapter.ItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(MainActivity.this,position+"===",Toast.LENGTH_SHORT).show();
}
});
}
}