Recyrcleview Android

Asked

Viewed 39 times

0

In my case I will need to put several buttons in a recyrcleview has to recognize on which button was clicked or only the position of an item in the list?

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/nome"
        android:layout_weight="1"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="10dip" />

    <Button
        android:id="@+id/divisao"
        android:layout_weight="1"
        android:background="@drawable/cell_shape"
        android:gravity="center" />

    <Button
        android:layout_weight="1"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="10dip"
        android:text="LANÇAR" />
</TableRow>

1 answer

1


You can make an interface that will have a method that will be implemented in your Activity, pass this System to your Adapter set the onClickListerner to your button inside the Adapter and call the interface method you implemented.

Example

Part 1

public interface OnClickButton{
    void onClick(View view);
}

Part 2

public class SuaActivity extends AppCompatActivity implements OnClickButton{

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.seu_arquivo_xml);

   seuAdapter.OnClickButton(this);
}

@Override
public void onClick(View view) {
   //Aqui você consegue capturar o id da View clicada
   switch(view.getId()){ 
       case R.id.nome:
           //ação
           break;
       default:
           break;
   }
}

Part 3

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter .MyViewHolder> {

    private OnClickButton onClickButton;


     public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public Button btNome;

        public MyViewHolder(View view) {
            super(view);
            btNome= (Button) view.findViewById(R.id.nome);

            btNome.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            onClickButton.onClick(view);
        }
    }
.
.
.

Try this solution, hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.