0

I have a list with recyclerView in which the CardView is visualized through an adapter and within the CardView I have 8 textView. Up to that point everything is perfect, the list is displayed well.But when implementing the onClick to the cardView I have the following problem, that the cardViews are only clickable in the background below the TextView elements, it is as if they cover the clickleable zone

 ArrayList<Maquina> maquinasList;
 maquinasList=new ArrayList<>();

    listaMaquinas= (RecyclerView) findViewById(R.id.lista_de_maquinas);

    listaMaquinas.setLayoutManager(new LinearLayoutManager(this));

    maquinasAdapter adapter= new maquinasAdapter(maquinasList);

    adapter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Maquina user=maquinasList.get(listaMaquinas.getChildAdapterPosition(v));
            Intent intent=new Intent(ListaMaquinas.this,RegistrarMaquina.class);

            Bundle bundle=new Bundle();
            bundle.putSerializable("maquina",user);

            intent.putExtras(bundle);
            startActivity(intent);

        }
    });
    listaMaquinas.setAdapter(adapter);

public class maquinaAdapter extends RecyclerView.Adapter<maquinaAdapter.MaquinaViewHolder> {

private ArrayList<Maquina> listaMaquinas;
private OnItemClickListener mListener;
public interface OnItemClickListener {
    void onItemClick(int position);
}
public void setOnClickListener(OnItemClickListener listener){
    mListener = listener;
}

public static class MaquinaViewHolder extends RecyclerView.ViewHolder{
    public TextView tipo;
    public TextView marca;
    public TextView modelo;
    public TextView serie;

    public MaquinaViewHolder(View itemView,final OnItemClickListener listener){
        super(itemView);
        tipo= itemView.findViewById(R.id.tipo);
        marca= itemView.findViewById(R.id.marca);
        modelo= itemView.findViewById(R.id.modelo);
        serie= itemView.findViewById(R.id.serie);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    int position=getAdapterPosition();
                    if (position!=RecyclerView.NO_POSITION){
                        listener.onItemClick(position);
                    }
                }
            }
        });
    }
}
public maquinaAdapter(ArrayList<Maquina> listaMaquina){
    listaMaquinas = listaMaquina;
}
@Override
public MaquinaViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista_maquinas,parent,false);
    return new MaquinaViewHolder(v,mListener);
}
@Override
public void onBindViewHolder(MaquinaViewHolder holder,int position){
    Maquina currentItem = listaMaquinas.get(position);

    holder.tipo.setText(currentItem.getTipo());
    holder.marca.setText(currentItem.getMarca());
    holder.modelo.setText(currentItem.getModelo());
    holder.serie.setText(currentItem.getSerie());
}
@Override
public int getItemCount(){
    return listaMaquinas.size();
}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.cardview.widget.CardView
    android:id="@+id/card"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:background="@drawable/cardview"
    android:backgroundTintMode="multiply"
    android:elevation="20dp"
    card_view:cardBackgroundColor="@color/colorAccent"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="24dp"
    card_view:cardMaxElevation="20dp"
    card_view:cardPreventCornerOverlap="true"
    card_view:cardUseCompatPadding="false">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="93dp"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/t1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/btnbordes"
                    android:text="Tipo"
                    android:textAlignment="center"
                    android:textColor="#3dffb8"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/t2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/btnbordes"
                    android:text="Marca"
                    android:textAlignment="center"
                    android:textColor="@android:color/white"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/t3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/btnbordes"
                    android:text="Modelo"
                    android:textAlignment="center"
                    android:textColor="@android:color/white"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/t4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginBottom="10dp"
                    android:background="@drawable/btnbordes"
                    android:text="Serie"
                    android:textAlignment="center"
                    android:textColor="@android:color/white"
                    android:textSize="14sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="227dp"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tipo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/text"
                    android:inputType="textMultiLine"
                    android:isScrollContainer="true"
                    android:maxLines="4"
                    android:textAlignment="center"
                    android:textColor="#3dffb8"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/marca"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/text"
                    android:inputType="textMultiLine"
                    android:isScrollContainer="true"
                    android:maxLines="4"
                    android:textAlignment="center"
                    android:textColor="@android:color/white"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/modelo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/text"
                    android:inputType="textMultiLine"
                    android:isScrollContainer="true"
                    android:maxLines="4"
                    android:textAlignment="center"
                    android:textColor="@android:color/white"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/serie"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginBottom="10dp"
                    android:background="@drawable/text"
                    android:inputType="textMultiLine"
                    android:isScrollContainer="true"
                    android:maxLines="4"
                    android:textAlignment="center"
                    android:textColor="@android:color/white"
                    android:textSize="14sp" />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>


</LinearLayout>

This is the code of the cardViews that are displayed in the list. I have already reviewed the adapter and the other classes and I think the problem is in the CardView Configuration. Thank you very much in advance.

1 Answers1

0

this is sample adapter of RecyclerView.Adapter

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private ListItemClickListener mOnClickListener;
public interface ListItemClickListener{
    void onListItemClick(View view, int postion);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    public TextView title;
    public CardView cardView;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.tvTitle);
        cardView=(CardView)view.findViewById(R.id.card_view);

        title.setOnClickListener(this);
        cardView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mOnClickListener.onListItemClick(v, getAdapterPosition());
    }

}

public void setClickListener(ListItemClickListener itemClickListener) {
    this.mOnClickListener = itemClickListener;
}

Now implement Adapter.ListItemClickListener on your activity class and set the setClickListener of your adapter

Adapter.setClickListener(this);
//the intent where to go inside the implemented method
Abdulkadir Ugas
  • 415
  • 5
  • 13