Java 单击每个recyclerView列表

Java 单击每个recyclerView列表,java,android-studio,android-recyclerview,onclick,Java,Android Studio,Android Recyclerview,Onclick,我在布局中设置了2个recyclerView,并使用arraylist在该recyclerView中传递数据。我试图在不同recyclerview的每个数组列表中设置clickListener 这是适配器 公共类RecyclerAdapter扩展了RecyclerView.Adapter{ 语境; 国际布局; 列出数据; 私有OnNoteClickListener mOnNoteClickListener; 公共回收器适配器(上下文上下文、int-layoutId、列表数据、OnNoteClic

我在布局中设置了2个recyclerView,并使用arraylist在该recyclerView中传递数据。我试图在不同recyclerview的每个数组列表中设置clickListener

这是适配器

公共类RecyclerAdapter扩展了RecyclerView.Adapter{ 语境; 国际布局; 列出数据; 私有OnNoteClickListener mOnNoteClickListener; 公共回收器适配器(上下文上下文、int-layoutId、列表数据、OnNoteClickListener OnNoteClickListener){ this.context=上下文; LayoutId=LayoutId; 这个数据=数据; this.mOnNoteClickListener=onNoteClickListener; } 公共回收器适配器(主活动主活动,内部项目\布局\垂直,列表顶部回收数据){ } @非空 @凌驾 public RecycleServiceWholder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){ LayoutInflater充气器=LayoutInflater.from(parent.getContext()); 查看myView=充气机。充气(LayoutId,null); 返回新的RecycleServiceWholder(myView、mOnNoteClickListener); } @凌驾 BindViewHolder上的公共void(@NonNull RecycleServiceWholder holder,int位置){ final ItemModel singleItem=data.get(位置); holder.imgTitle.setText(singleItem.getImgTitle()); holder.img.setImageDrawable(context.getResources().getDrawable(singleItem.getImgId()); } @凌驾 public int getItemCount(){ 返回data.size(); } 公共类RecyclerViewWholder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{ OnNoteClickListener OnNoteClickListener; 文本视图imgTitle; 图像视图img; public RecycleServiceWholder(@NonNull View itemView,OnNoteClickListener OnNoteClickListener){ 超级(项目视图); imgTitle=itemView.findviewbyd(R.id.imgTitle); img=itemView.findviewbyd(R.id.img); this.onNoteClickListener=onNoteClickListener; setOnClickListener(这个); } @凌驾 公共void onClick(视图){ onNoteClickListener.onNoteClick(getAdapterPosition()); } } 公共接口OnNoteClickListener{ void onNoteClick(整数位置); } } 这是显示recyclerview的活动:

@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//从XMl获取视图
topRecyclerView=findViewById(R.id.topRecyclerView);
bottomRecyclerView=findViewById(R.id.bottomRecyclerView);
//顶部回收器视图的数据
List topRecycleData=new ArrayList();
添加(新的ItemModel(R.mipmap.ic_启动器,“Img 1”);
添加(新的ItemModel(R.mipmap.ic_启动器,“IMG2”);
//底部回收器视图的数据
List bottomRecycleData=new ArrayList();
添加(新的ItemModel(R.mipmap.ic_启动器,“img1”);
添加(新的ItemModel(R.mipmap.ic_启动器,“IMG2”);
添加(新的ItemModel(R.mipmap.ic_启动器,“img3”);
添加(新的ItemModel(R.mipmap.ic_启动器,“img4”);
//将布局设置为回收器视图
setLayoutManager(新的GridLayoutManager(this,2));
bottomRecyclerView.setLayoutManager(新的LinearLayoutManager(这个,LinearLayout.HORIZONTAL,false));
//创建适配器
RecyclerAdapter topAdapter=新的RecyclerAdapter(主活动、右布局、项目、垂直布局、topRecycleData);
RecyclerAdapter bottomAdapter=新的RecyclerAdapter(主活动。此,右布局。项目\u布局\u水平,底部循环数据);
//将适配器设置为布局
topRecyclerView.setAdapter(topAdapter);
bottomRecyclerView.setAdapter(bottomAdapter);
}
}
如果只有一个recyclerview,那么我希望我能做到这一点,但这次在一个家里有两个recyclerview和两个不同的arrylist。所以我很困惑如何调用recyclerview的两个不同arraylist的侦听器


我想在第一个回收器视图中单击“例如”位置2时生成toast消息,在第二个回收器视图中单击位置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);
}
}
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);

        }

        } 
}
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.GridLayoutManager;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入java.util.ArrayList;
导入java.util.List;
公共类MainActivity扩展了AppCompatActivity{
RecyclerView topRecyclerView,bottomRecyclerView;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u test\u main);
//从XMl获取视图
topRecyclerView=findViewById(R.id.topRecyclerView);
bottomRecyclerView=findViewById(R.id.bottomRecyclerView);
//顶部回收器视图的数据
List topRecycleData=new ArrayList();
添加(新的ItemModel(R.mipmap.ic_启动器,“Img 1”);
添加(新的ItemModel(R.mipmap.ic_启动器,“IMG2”);
添加(新的ItemModel(R.mipmap.ic_启动器,“Img 3”);
添加(新的ItemModel(R.mipmap.ic_启动器,“Img 4”);
添加(新的ItemModel(R.mipmap.ic_启动器,“Img 5”);
添加(新的ItemModel(R.mipmap.ic_启动器,“Img 6”);
添加(新的ItemModel(R.mipmap.ic_启动器,“Img 7”);
添加(新的ItemModel(R.mipmap.ic_启动器,“img8”);
添加(新的ItemModel(R.mipmap.ic_启动器,“img9”);
添加(新的ItemModel(R.mipmap.ic_启动器,“img10”);
topRecycleData.add(新项目M
<?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>
<?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>
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);

        }

        } 
}
   ...
   this.onNoteClickListener = onNoteClickListener;

        itemView.setOnClickListener(this);
itemView.setOnClickListener(new View.OnclickListener){
    @Override
    public void onClick(View v){
        if(onNoteClickListener != null){
           int position = getAdapterPosition();//This will provide you the items 
           //position
           //This is to make sure the 
               //position is valid
           if(position != RecyclerView.NO_POSITION){
                 //finally call the onNoteClickListener.onNoteClick
                  onNoteClickListener.onNoteClick(position)
           }
        }
    }
}
adapter.setOnItemClickListener(new OnNoteClickListener ...)
adapter.setOnItemClickListener(new OnNoteClickListener(){
     @Override
     public void onItemClick(int position){
         mListItem.get(positio);
         Toast.make(getApplicationContext(), "Item Clicked ", 
                   Toast.LENGTH_SHORT).show();
     }
});