Force onSizeChanged for ListView

I have a list that is dynamically assigned with different data sets. It works great. I also made fastScrollEnabled true. To update sectionIndexer I need to call

list.setFastScrollEnabled(false); list.setFastScrollEnabled(true); 

The problem is that the index is displayed in the upper left corner, and this is a problem with the information. Generally, when I change orientation, the index displays correctly again. To test this, I implemented a custom ListView, and observation - if I can get listView to call onSizeChanged (), it will display correctly.

Now the question is:

Is it possible to force listView to call the onSizeChanged () method? If so, how can we do this.

Does anyone know the conditions when onSizeChanged () is called. This is definitely called when orientation changes, any other conditions that we can think of.

+7
source share
3 answers

To force onSizeChanged into a ListView, I do the following:

 myList.getLayoutParams().height = myList.getHeight() + 1; myList.requestLayout(); 

Just remember not to call +1 or -1 every time, as the list can simply overstrain the specified size and therefore use the flag to switch between +1 and -1.

+7
source

Try:

 listView.requestLayout(); 
0
source

This is my decision:
I subclassed the GridView using the constructor (Context, AttributeSet):
(this should be done for me in a separate file class)
and redefined the onSizeChanged method

 MyGrid.java public class MyGrid extends GridView { public void MyGrid(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } protected void onSizeChanged(int w, int h, int oldw, int oldh) { // TODO Auto-generated method stub // Here I call my custom functions to prepare layout of my custom listview super.onSizeChanged(w, h, oldw, oldh); } } 

In the Activity class, than using a GridView,
I redefined the onStart method
(called after OnCreate and after onRestart [when you came from another activity])

 MyActivity.java public class MyActivity extends Activity { onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(...) ... } .... protected void onStart() { // TODO Auto-generated method stub // call db and create cursor // 'mygridview' = findbyid MyGrid // (declared on xml like '<packagepath>'.mygrid // setadapter with my costom adapeter // **HERE THE CALLED ONSIZECHANGED** // I make test proper for my app // (there is a simple test) if ('mygridview'.getMeasuredHeight() > 0) { // I use the same width and height for new and old measures // because for me that is right 'mygridview'.onSizeChanged(gw.getWidth(), gw.getHeight(), gw.getWidth(), gw.getHeight()); } super.onStart(); } } 

With this approach, I can resize the grid at any time. Hope this helps you.

0
source

All Articles