Handle click item in Recycleview

后端 未结 5 1809
不知归路
不知归路 2021-01-07 08:52

I have a horizontal recycleview inside vertical recycleview.

and i add list cardview inside horizontal rec

相关标签:
5条回答
  • 2021-01-07 09:01

    It would be better to implement OnClickListener on your ViewHolder class and add the listener to your desired views.

    Just in onBindViewHolder(...) add setOnClickListener(...) like this:

    @Override
    public void onBindViewHolder(WordViewHolder holder, final int position) {
    
        //... 
    
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), position + "", Toast.LENGTH_SHORT).show();
            }
        });
    }
    
    0 讨论(0)
  • 2021-01-07 09:04

    Create an inteface

    public interface OnItemClickListener{
        void onItemClick(View view, int position)
    }
    

    Add a parametrized constuctor in your adapter AlbumsVerticalAdapter

    private OnItemClickListener mOnviewItemClickListener;
    public AlbumsVerticalAdapter(Context context, List data, OnItemClickListener mOItemClickListener){
    //...
    this.mOnItemClickListener= mOnItemClickListener;
    

    Now add this line in your in OnBinderViewHolder method inside adapter class

    @Override
    public void onBindViewHolder(final FolderViewHolder holder, final int position) {
      //....
      mholder.mimageview.setOnClickListener(new View.OnItemClickListener){
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v,position);    
            }
        });
    }
    

    Implement this interface in your Activity and use onClick of the item

     public class MainActivity extends AppCombatActivity implements AlbumsVerticalAdapter.OnItemClickListener{
            @Override
            onItemClick(View view, int position){
             // write your code 
          }
    
         }
    
    0 讨论(0)
  • 2021-01-07 09:10
     /Just try to implement the below code Snippet/
    
     First make an Interface Class "RecyclerView_ItemClickListener"
    
     public interface RecyclerView_ItemClickListener
     {
      void onClick(View view, int position);
    
      void onItemClick(View view, int position);
     }
    
     Then use this class in your Activity/Fragment and  inside Adapter.
    
     How to Implement in Adapter class:
    
     private RecyclerView_ItemClickListener clickListener;
    
     public void setClickListener(RecyclerView_ItemClickListener itemClickListener)
     {
        this.clickListener = itemClickListener;
     }
    
     public void SetOnItemClickListener(final RecyclerView_ItemClickListener mItemClickListener)
     {
        this.clickListener = mItemClickListener;
     }
    
    Implement implements View.OnClickListener inside MyViewHolder Method 
    of Adapter
    
     Example like this: class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    
    itemView.setOnClickListener(this);
    
    
    Then put the below code
    
    @Override
        public void onClick(View view)
        {
            if (clickListener != null)
            {
                clickListener.onClick(view, getAdapterPosition());
    
                clickListener.onItemClick(view,getAdapterPosition());
            }
        }
    
    After that implements RecyclerView_ItemClickListener inside your 
    Activity/Fragment
    
    in OnCreate:
    
    your_Adapter.setClickListener(this);
    
    your_Adapter.SetOnItemClickListener(this);
    
    @Override
    public void onClick(View view, int position)
    {}
    
    @Override
    public void onItemClick(View view, int position)
    {
    
    }
    
    0 讨论(0)
  • 2021-01-07 09:12

    Looking at your code the VerticalRecyclerViewAdapter has a List<Albums> and each Album in this list will require a HorizontalRecyclerViewAdapter to hold another List<Albums>.

    In your VerticalRecylerViewAdapter create VerticalClickListener which will be implemented by your activity. Inside HorizontalRecyclerViewAdapter create HorizontalClickListener which will be implemented by your VerticalRecyclerViewAdapter for each Album. When creating the HorizontalRecyclerViewAdapter for each Album, pass in the vertical position as well. Then, when a cardview is clicked, the HorizontalClickListener can return the vertical position of the adapter and also the horizontal position of the cardview. The VerticalRecyclerViewAdapter will return the vertical and horizontal positions to your activity.

    0 讨论(0)
  • 2021-01-07 09:13

    First, you create an interface like :

    public interface OnItemClickListener{
        void onItemClick(View view, int position)
    }
    

    Declare on global :

    private OnItemClickListener mOnItemClickListener;
    

    Next, you need a contructor like :

    public MyAdapter(Context context, List data, OnItemClickListener mOnItemClickListener){
    //...
    this.mOnItemClickListener= mOnItemClickListener;
    

    }

    And in onBindViewHolder

    @Override
    public void onBindViewHolder(final FolderViewHolder holder, final int position) {
      //....
      holder.image.setOnClickListener(new View.OnItemClickListener){
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v,position);    
            }
        });
    

    }

    Finally, you can implement Callback in class use adapter like :

    public class A extends AppCombatActivity implements MyAdapter.OnItemClickListener{
     //exist code
        @Override
        onItemClick(View view, int position){
         // Do something with view + position
      }
    
     }
    
    0 讨论(0)
提交回复
热议问题