I am using a GridView within a RelativeLayout that contains an image, a checkbox and a textview. I use an adapter to select or deselect the checkboxes. I use a fragment to show the GridView. The checkbox is selected / deselected by touching it or touching the image. I have implemented the onClick checkbox and imageview.
In the Fragment, I try to know the selected elements to enable / disable a menu of the fragment itself but the problem is that the setOnItemClickListener method is not running.
First of all, I want to say that I have already consulted the answers to questions similar to this one and I have applied each one but it does not work, so I have to "repeat it".
I have the code for it in the following way:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_residuos, container, false);
final GridView gridview = view.findViewById(R.id.grid_elements);
adapter = new ResiduosAdapter(this.activity);
gridview.setAdapter(adapter);
// creo un objeto listener
AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getContext().getApplicationContext(),
"Click ListItem Number " + i, Toast.LENGTH_LONG)
.show();
}
};
// asigno el objeto listener al gridview
gridview.setOnItemClickListener(listener);
return view;
}
In many of the answers they mention putting android:focusable="false"
in each element, which I already did but it still does not work.
The Fragment:
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="@+id/relativeLayout1"
p1:padding="10dp">
<GridView
p1:id="@+id/grid_elements"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:choiceMode="multipleChoice"
p1:clickable="false"
p1:drawSelectorOnTop="true"
p1:horizontalSpacing="2dp"
p1:minHeight="25px"
p1:minWidth="25px"
p1:numColumns="3"
p1:verticalSpacing="2dp" />
</RelativeLayout>
The custom layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
p1:id="@+id/relativeLayout1"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:background="@color/primary_material_light"
p1:focusable="false"
p1:focusableInTouchMode="false"
p1:minHeight="25px"
p1:minWidth="25px">
<ImageView
p1:id="@+id/elements_img"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:layout_marginLeft="8dp"
p1:layout_marginTop="8dp"
p1:focusable="false"
p1:focusableInTouchMode="false"
p1:src="@drawable/envases_plasticos" />
<TextView
p1:id="@+id/elements_name"
p1:layout_width="wrap_content"
p1:layout_height="wrap_content"
p1:layout_below="@+id/elements_img"
p1:layout_marginLeft="10dp"
p1:layout_marginTop="5dp"
p1:focusable="false"
p1:focusableInTouchMode="false"
p1:text=" "
p1:textColor="@color/primary_dark_material_dark"
p1:textSize="@dimen/abc_edit_text_inset_top_material" />
<CheckBox
p1:id="@+id/elements_check"
p1:layout_width="wrap_content"
p1:layout_height="wrap_content"
p1:layout_alignRight="@id/elements_img"
p1:focusable="false"
p1:focusableInTouchMode="false" />
</RelativeLayout>