Add clickListner for button inside a cardView populated using a recyclerView

前端 未结 4 1426
予麋鹿
予麋鹿 2020-12-25 07:49

I have a cardView

card_contents.xml




        
相关标签:
4条回答
  • 2020-12-25 08:44

    Another approach would be using an Interface

    1) Create an interface

    public interface ItemClickListener{
    
       public void startSecondActivity(int index);
    
       public void startThirdActivity(int index);
    
    }
    

    2) Implement interface in your Activity

    public class MainActivity extends AppcompatActivity implements ItemClickListener {
    
       .........
    
       public void startSecondActivity(int index){
    
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(i);
    
       }
    
       public void startThirdActivity(int index){
    
                Intent i = new Intent(MainActivity.this, ThirdActivity.class);
                startActivity(i);
       }
    
    
    }
    

    3) Use ItemListener in RecyclerViewAdapter

    MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
    
          private ItemListener listener;
    
          public MyAdapter(Context context){
               listener = (ItemListener)context;
          }
    
          Override
          public void onBindViewHolder(MyViewHolder myViewHolder, int position){
    
             myViewHolder.btnButton1.setOnClickListener(new View.OnClickListener(){
    
                  @Override
                  public void onClick(View v) {
                      listener.startSecondActivity(position)
                  }
             });
    
             myViewHolder.btnButton2.setOnClickListener(new View.OnClickListener(){
    
                  @Override
                  public void onClick(View v) {
                       listener.startThirdActivity(position)
                  }
             });
          }
    
    }
    
    0 讨论(0)
  • 2020-12-25 08:45

    Let's say you have:

    <android.support.v7.widget.CardView
        android:id="@+id/cv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:elevation="4dp"
        app:cardCornerRadius="2dp">
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="16dp">
    
            <TextView
                android:id="@+id/listText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:padding="8dp"
                android:text="New Text" />
    
            <ImageButton
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/listText"
                android:layout_alignParentRight="true"
                android:onClick="onClickBotonBorrar"
                android:background="?android:selectableItemBackground"
                android:src="@drawable/ic_action_borrar" />
        </RelativeLayout>
    
    </android.support.v7.widget.CardView>
    

    Note this part: android:onClick="onClickBotonBorrar"

    On your Activity add this:

    public void onClickBotonBorrar (View v) {
    //Do whatever you want when user clicks on your ImageButton
        }
    

    It is recommended not to have the click event inside your adapter but inside your activity.

    Note: It's similar for button

    0 讨论(0)
  • 2020-12-25 08:46

    Define your onClick listener on onBindViewHolder method in CustomAdapter.

    0 讨论(0)
  • 2020-12-25 08:49

    Add click listener for button inside a cardView populated using a recyclerView

    Add click event to buttons which is in RecyclerView as:

    1. Get Button from xml in same way as doing for TextView in MyViewHolder class:

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        Button btnButton1;
        MyViewHolder(View view){
            super(view);
            this.textView= (TextView) view.findViewById(R.id.card_text);
            this.btnButton1= (Button) view.findViewById(R.id.button1);
            ... do same for other Button
        }
    }
    

    2. Add setOnClickListener method for Button in onBindViewHolder method:

    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, int i){
         myViewHolder.textView.setText(list.get(i));
          myViewHolder.btnButton1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                /// button click event
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题