问题
I have a listview which gets assigned with different datasets dynamically. This works fine. I have also made fastScrollEnabled to be true. For the sectionIndexer to be updated I need to call
list.setFastScrollEnabled(false);
list.setFastScrollEnabled(true);
The problem is the index shows in top left and this is a know issue. As a work-around whenever I change the orientation the index is again correctly shown. To test this I have implemented a custom ListView and the observation is if I can force listView to call onSizeChanged() it will show up properly.
Now the question:
Is it possible to force listView to call onSizeChanged() method? If yes how can we accomplish this.
Does anyone know of conditions when onSizeChanged() is called. It is definitely called when the orientation changes, any other conditions we can think of.
回答1:
To force onSizeChanged on ListView I do the following:
myList.getLayoutParams().height = myList.getHeight() + 1;
myList.requestLayout();
Just be sure to not call +1 or -1 everytime as the list may just overstretch the size specified and hence use a flag to toggle between +1 and -1.
回答2:
Try with:
listView.requestLayout();
回答3:
This is my solution:
I subclassed GridView with constructor (Context, AttributeSet):
(this for me must be done on a separate file class)
and overrided 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);
}
}
On class Activity than use GridView,
I have overrided 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 my grid at any time. I hope this you helpful.
来源:https://stackoverflow.com/questions/6141314/force-onsizechanged-for-listview