How to call onListItemClick in ListFragment

On the tablet tablets of my application’s tablet, I have three lists and one regular fragment, let’s call them Make, Model, Size and Details. The Make list is initially populated, then, based on the Make selection, the Model list is selected; when a model is selected, the "Size" list is populated; when size is selected, details are displayed. Each of these events (selection of list items) is processed through the onListItemClick handler.

When I start, I want to fill in the Make list, select the first Make in the list and pass it through the onListItemClick handler to populate the Model list (and so on, so that all the lists are filled and details are shown) this should also be the behavior when there is any choice made in any of the lists - select the first item in the following list until we see the details). Please note that I have control over the database, and for each Make there will always be at least one model for each Make / Model, at least one size for each Make / Model / Size in exactly one detail.

So, I want to select the first item in the list and call its onListItemClick handler. I tried the following (with appropriate border checking, etc.), but it does not work.

getListView().setItemChecked(0, true); 

The only changes to the “out of the box” ListFragment is to set CacheColorHint as such

 getListView().setCacheColorHint(R.color.GhostWhite); 

where GhostWhite is set to styles.xml as

 <color name="GhostWhite">#88FFFFFF</color> 

Any ideas?

Thanks in advance.

+6
source share
5 answers

To select the first item in the list, first select the focus from the touch screen, then select the first item, then run the onclick handler

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

1.) In the OnActivityCreated of your list fragment, create such an interface

 public interface ListItemSelectedListener { public void onListItemSelected(int index); } 

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

Here's how it worked for me with the Android Support Package - should look like HC

+1
source

Try adding this, it works in my project:

 @Override public void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); Intent intent = new Intent(); intent.setClass(getActivity(), someActivity.class); startActivity(intent); } 
+1
source

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

 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //if the parent is in the landscape, make the first item selected if (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()); } } } 
+1
source

Source: https://habr.com/ru/post/904363/


All Articles