Skip to content Skip to sidebar Skip to footer

How To Trigger Onlistitemclick In A Listfragment

In the tablet layout of my app, I have three ListFragments and one regular Fragment, let's call them Make, Model, Size, and Details. Initially the Make list is populated, then bas

Solution 1:

To select the first item in the list, first get focus from touch, then select the first item, then trigger the onclick handler

int position = 0;
getListView().requestFocusFromTouch();
getListView().setSelection(position);
getListView().performItemClick(getListView().getAdapter().getView(position, null, null), position, position);

Solution 2:

Solution 3:

1.) In the OnActivityCreated of your list fragment create an interface like so

publicinterfaceListItemSelectedListener {
        publicvoidonListItemSelected(int index);
}

2.) Implement the interface in your activity 3.) Create private ListItemSelectedListener selectedListener in your list fragment 4.) Set selectedListener.onListItemSelected(0) in the OnActivityCreated.

This is how it worked for me using the Android Support Package - should be similar on HC

Solution 4:

Try adding this,, it works in my project:

@OverridepublicvoidonListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);
    Intentintent=newIntent();
    intent.setClass(getActivity(), someActivity.class);
    startActivity(intent);
}

Solution 5:

in The Fragment class in onActivityCreated(Bundle savedInstanceState) method, you can add the following:

@OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //if the parent is in the landscape, make the first item selectedif (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            try {
                if (mItems.size() > 0) {
                    //notify the activity that an item is selected
                    mListener.onMasterItemSelected(mItems.get(0));
                }
            } catch (Exception e) {
                Log.e("exception", e.toString());
            }
        }
    }

Post a Comment for "How To Trigger Onlistitemclick In A Listfragment"