setOnItemClickListener does not run in the fragment

0

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>
    
asked by Néstor 13.07.2018 в 00:18
source

2 answers

1

It does not work because you should implement an interface to be able to do onClickListener in the elements of gridView , unlike button, you are clicking on views that are inflated, therefore you should implement the part of onClick in the gridView adapter, another thing that I would suggest, is to use a RecyclerView with a GridLayoutManager , since you will be able to have a higher performance in your app.

For that use setOnClickListener() within your adapter

You can follow this video to implement the GridLayoutManager in the video use LinearLayoutManager , but only change it by the grid and ready

link

    
answered by 13.07.2018 в 02:56
0

Do not be if I understand it you want that from the framgment / activity you can intercept the Onclick of each element of the grid?

Searching the web Quora I have found the following, test if it is what you need

public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {

public interface OnItemClickListener {
    void onItemClick(ContentItem item);
}

private final List<ContentItem> items;
private final OnItemClickListener listener;

public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
    this.items = items;
    this.listener = listener;
}

@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false);
    return new ViewHolder(v);
}

@Override public void onBindViewHolder(ViewHolder holder, int position) {
    holder.bind(items.get(position), listener);
}

@Override public int getItemCount() {
    return items.size();
}

static class ViewHolder extends RecyclerView.ViewHolder {

    private TextView name;
    private ImageView image;

    public ViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.name);
        image = (ImageView) itemView.findViewById(R.id.image);
    }

    public void bind(final ContentItem item, final OnItemClickListener listener) {
        name.setText(item.name);
        Picasso.with(itemView.getContext()).load(item.imageUrl).into(image);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                listener.onItemClick(item);
            }
        });
    }
}
}

Then to intercept the OnItemClilckListener() from the adapter.

recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
@Override public void onItemClick(ContentItem item) {
    Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show();
}
}));
    
answered by 17.07.2018 в 12:47