आपको सूची दृश्य की ऊंचाई को एक निश्चित मान पर सेट करना होगा। अपनी सामग्री गतिशील है वहाँ एडाप्टर रीसेट करने के बाद वास्तविक listsize को मापने के लिए एक अच्छा समारोह है:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
आप अपने अनुकूलक सेट करने के बाद पैरामीटर के रूप में अपने सूचीदृश्य के साथ इस स्थिर विधि कॉल करने के लिए है। अब आप एक स्क्रॉलव्यू जोड़ सकते हैं (अंदर आपकी सूचीदृश्य और अन्य विचार हैं)। मैं यह व्यवहार 2.3.3 में पहले कहूंगा कि एक छोटी सी बग है ... मैंने जिस तरीके से वर्णन किया है, उसे स्क्रॉलव्यू के अंदर सूचीदृश्य शामिल करने का कोई आसान तरीका नहीं है। इसी कारण से उन्होंने OVERSCROLL_NEVER मोड प्रस्तुत किया :)
कोड डौगड से है!
स्रोत
2012-04-22 17:11:45
शानदार !!! यह वास्तव में मुझे बहुत परेशानी बचाई। आप कैसे जानते थे कि किस विधि का नाम देखना है, एस 2 एपीआई सार्वजनिक है? – npace