Cannot get gridView while listening for clicks

I cannot get setOnItemClickListener gridView in Fragment. What could 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;
    }
}`
+3
source share
2 answers

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

see adding CheckBox to the list bar lose my onItemClick events?

+9
source

When using fragments, the presentation is initialized in two stages.

The view is only overpriced (and therefore available) after the onCreateView method. This method is intended only to inflate the view and return it to the Fragment.

, onClickListeners, onActivityCreated(), , .

Google http://developer.android.com/reference/android/app/Fragment.html#Lifecycle

, , :

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();
        }*/
    }});
}
}
+3

All Articles