3

I can not get setOnItemClickListener of gridView in Fragment. What can be the problem?

Here is my code::

 public class MainMenuFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.main_menu_fragment, container, false);

        itemsGridViewObj = (GridView) view.findViewById(R.id.itemsGridView);



        itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {

                Log.d(TAG, "--> onItemClick listener...");      // Can not getting this method.
                /*if(position == 1) {
                    FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
                    fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
                    fragmentTransaction.commit();
                }*/
            }
        });

        return view;
    }
}`
King of Masses
  • 18,405
  • 4
  • 60
  • 77
AndroidDev
  • 2,627
  • 6
  • 29
  • 41
  • It is actually custom GridView containing only ButtonView. I think it is causing problem to get onItemClickListener! – AndroidDev Apr 12 '12 at 09:49
  • Do you mean there is a button in each item in the GridView? I.e. in the layout the LisAdapter uses. – Richard Lewin Apr 12 '12 at 10:33
  • yes. In this case im not getting that onItemClickListener! – AndroidDev Apr 12 '12 at 10:39
  • I believe that the button is overriding the OnItemClickListener. I have had a similar issue with a checkbox in a list view. In the adapter add a new OnClickListener to the button inline and see if the OnItemClickListener comes to life. – Richard Lewin Apr 12 '12 at 11:30
  • I have an imageview and a checkbox in my linear layout that I'm populating the grid view with. I made the image view and checkbox not clickable and not focusable and the linear layout clickable and focusable but I'm still not receiving click events. – Namratha Feb 06 '13 at 04:37
  • @Namratha: Please have a look at the link [here](http://xjaphx.wordpress.com/2011/07/14/listview-doesnt-respond-to-onitemclicklistener/) it contains all the combinations that should be taken into account while handling such issues. – AndroidDev Feb 06 '13 at 13:37

2 Answers2

9

You may need to set the following in your ButtonView. android:focusable="false" android:focusableInTouchMode="false"

see adding CheckBox to list row loses my onItemClick events?

Community
  • 1
  • 1
esse
  • 5,429
  • 3
  • 19
  • 20
3

When using Fragments the initialisation of the view occurs over two stages.

The view is only inflated (and therefore accessible) after the onCreateView method. This method is only for inflating a view and returning it to the Fragment.

Therefore, any logic to do with finding views and setting up onClickListeners should be done in the onActivityCreated() function as this is the first point at which you can access the inflated view.

Have a look at the Google docs at http://developer.android.com/reference/android/app/Fragment.html#Lifecycle

Below is you code adjusted to comply to what I have described above:

public class MainMenuFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment

    return inflater.inflate(R.layout.main_menu_fragment, container, false);     
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState); 

    GridView itemsGridViewObj = (GridView) findViewById(R.id.itemsGridView);

    itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {

        Log.d(TAG, "--> onItemClick listener..."); // You should see this now
        /*if(position == 1) {
            FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
            fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
            fragmentTransaction.commit();
        }*/
    }});
}
}
Richard Lewin
  • 1,870
  • 1
  • 19
  • 26
  • Does no work for me. Firstly there is no method `findViewById` in `Fragment` and doing `getView().findViewById` doesn't work either. I don't know how this answer got 3 votes. – Vedant Agarwala Sep 24 '15 at 07:38