Can not get gridView on item click listener

17,574

Solution 1

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?

Solution 2

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();
        }*/
    }});
}
}
Share:
17,574
AndroidDev
Author by

AndroidDev

Updated on June 29, 2022

Comments

  • AndroidDev
    AndroidDev almost 2 years

    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;
        }
    }`
    
  • Chris Rae
    Chris Rae over 11 years
    Solved my problem too. I'd unchecked Clickable, but not Focusable.
  • Vedant Agarwala
    Vedant Agarwala over 8 years
    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.