我在布局中设置了 2 个 recyclerView,并使用 arraylist 在该 recyclerView 中传递数据。我试图在不同 recyclerview 的每个数组列表中设置 clickListener。
这是适配器
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>{
Context context;
int LayoutId;
List<ItemModel> data;
private OnNoteClickListener mOnNoteClickListener;
public RecyclerAdapter(Context context, int layoutId, List<ItemModel> data, OnNoteClickListener onNoteClickListener) {
this.context = context;
LayoutId = layoutId;
this.data = data;
this.mOnNoteClickListener = onNoteClickListener;
}
public RecyclerAdapter(MainActivity mainActivity, int item_layout_vertical, List<ItemModel> topRecycleData) {
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View myView = inflater.inflate(LayoutId,null);
return new RecyclerViewHolder(myView, mOnNoteClickListener);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
final ItemModel singleItem = data.get(position);
holder.imgTitle.setText(singleItem.getImgTitle());
holder.img.setImageDrawable(context.getResources().getDrawable(singleItem.getImgId()));
}
@Override
public int getItemCount() {
return data.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
OnNoteClickListener onNoteClickListener;
TextView imgTitle;
ImageView img;
public RecyclerViewHolder(@NonNull View itemView, OnNoteClickListener onNoteClickListener) {
super(itemView);
imgTitle = itemView.findViewById(R.id.imgTitle);
img = itemView.findViewById(R.id.img);
this.onNoteClickListener = onNoteClickListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
onNoteClickListener.onNoteClick(getAdapterPosition());
}
}
public interface OnNoteClickListener{
void onNoteClick(int positon);
}
}
这是显示 recyclerview 的 Activity :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Fetching View From XMl
topRecyclerView = findViewById(R.id.topRecyclerView);
bottomRecyclerView = findViewById(R.id.bottomRecyclerView);
// Data For Top Recycler Views
List<ItemModel> topRecycleData = new ArrayList<ItemModel>();
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 1"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 2"));
// Data For Bottom Recycler Views
List<ItemModel> bottomRecycleData = new ArrayList<ItemModel>();
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 1"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 2"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 3"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 4"));
// Setting Layouts To Recycler Views
topRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
bottomRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL,false));
// Creating Adapters
RecyclerAdapter topAdapter = new RecyclerAdapter(MainActivity.this,R.layout.item_layout_vertical,topRecycleData);
RecyclerAdapter bottomAdapter = new RecyclerAdapter(MainActivity.this,R.layout.item_layout_horizontal,bottomRecycleData);
// Setting Adapters To Layouts
topRecyclerView.setAdapter(topAdapter);
bottomRecyclerView.setAdapter(bottomAdapter);
}
}
如果有一个回收器 View ,那么我希望我能做到这一点,但这次家里有 2 个回收器 View ,有 2 个不同的 arrylist。所以我很困惑如何为recyclerview的两个不同的数组列表调用监听器。
我想在单击第一个回收器 View 上的“例如”位置 2 时生成 toast 消息,并在单击第二个回收器 View 的位置 1 时生成 toast 消息。
最佳答案
这是包含所有更改的完整代码。我已经测试了你的代码,它运行良好。
MainActivity Java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity{
RecyclerView topRecyclerView, bottomRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_main);
// Fetching View From XMl
topRecyclerView = findViewById(R.id.topRecyclerView);
bottomRecyclerView = findViewById(R.id.bottomRecyclerView);
// Data For Top Recycler Views
List<ItemModel> topRecycleData = new ArrayList<ItemModel>();
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 1"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 2"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 3"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 4"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 5"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 6"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 7"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 8"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 9"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 10"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 11"));
topRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 12"));
// Data For Bottom Recycler Views
List<ItemModel> bottomRecycleData = new ArrayList<ItemModel>();
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 1"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 2"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 3"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 4"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 5"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 6"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 7"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 8"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 9"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 10"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 11"));
bottomRecycleData.add(new ItemModel(R.mipmap.ic_launcher,"Img 12"));
// Setting Layouts To Recycler Views
topRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
bottomRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// Creating Adapters (No need to send onNote)
RecyclerAdapter topAdapter = new RecyclerAdapter
(this,R.layout.item_layout_vertical,topRecycleData);
RecyclerAdapter bottomAdapter = new RecyclerAdapter
(this,R.layout.item_layout_horizontal,bottomRecycleData);
// Setting Adapters To Layouts
topRecyclerView.setAdapter(topAdapter);
bottomRecyclerView.setAdapter(bottomAdapter);
}
}
MainActivity XML 使用 Scoll View 和线性布局,并在内部线性布局上使用权重,以在屏幕高度上均匀缩放它们。在您的代码中,顶部布局被底部布局隐藏。 ScrollView 必须包含一个 subview ,这就是为什么添加额外的线性布局作为 ScrollView 的 subview 。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:scrollbars="none"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_margin="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4CE9D9"
android:text="Header No 1"
android:textSize="18sp"
android:textColor="#000"
android:padding="5dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/topRecyclerView"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottomRecyclerViewLayout"
android:layout_margin="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4CE9D9"
android:text="Header No 2"
android:textSize="18sp"
android:textColor="#000"
android:padding="5dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/bottomRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
item_layout_vertical xml 添加一个 id 到顶部布局,稍后将用于设置 onclick 监听器。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/verticalContainer"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/imgTitle"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
android:text="Hello Dear Dibas"/>
</LinearLayout>
item_layout_horizontal xml 添加一个 id 到顶部布局,稍后将用于设置 onclick 监听器。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontalContainer"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/imgTitle"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
android:text="Hello Dear Dibas"/>
</LinearLayout>
RecyclerAdapter Java
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>{
Context context;
int LayoutId;
List<ItemModel> data;
public RecyclerAdapter(Context context, int layoutId, List<ItemModel> data) {
this.context = context;
LayoutId = layoutId;
this.data = data;
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View myView = inflater.inflate(LayoutId,null);
return new RecyclerViewHolder(myView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, final int
position) {
final ItemModel singleItem = data.get(position);
holder.imgTitle.setText(singleItem.getImgTitle());
holder.img.setImageDrawable(context.getResources()
.getDrawable(singleItem.getImgId()));
//checking which layout is in layoutId and adding onclick listener
if (LayoutId == R.layout.item_layout_horizontal)
{
holder.horizontalLayout.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
//check which item is clicked here and proceed
if(position==0)
{
//do something
}
else if(position==1)
{
//do something
}
else if(position==2)
{
//do something
}
//and so on to the list end
}
});
}
else {
holder.verticalLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//check which item is clicked here and proceed
if(position==0)
{
//do something
}
else if(position==1)
{
//do something
}
else if(position==2)
{
//do something
}
//and so on to the list end
}
});
}
}
@Override
public int getItemCount() {
return data.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView imgTitle;
ImageView img;
LinearLayout horizontalLayout,verticalLayout;
public RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
imgTitle = itemView.findViewById(R.id.imgTitle);
img = itemView.findViewById(R.id.img);
//fetching the container views that are to be attached to the
//recycler view and for adding onclick listeners
//(because of multiple layouts for the same adapter)
if(LayoutId==R.layout.item_layout_horizontal)
horizontalLayout=itemView.findViewById(R.id.horizontalContainer);
else
verticalLayout=itemView.findViewById(R.id.verticalContainer);
}
}
}
希望这有帮助。它运行良好。在点击监听器条件中添加 toast 以验证它们是否适用于确切位置。需要注意的是,位置索引从 0(第一项)开始,依此类推。
关于java - recyclerView每个列表中的onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58434166/