Recyrcleview Android [duplicate]

0

In my case I will need to put several buttons in a recyrcleview how can I recognize 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>

    
asked by anonymous 13.12.2017 / 18:25

1 answer

1

You can make an interface that will have a method that will be implemented in your Activity, pass this listener to your adapter, set the onClickListerner to your button inside the adapter and call the method of the interface that you have 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!

    
13.12.2017 / 19:30