عادة ، لن نلاحظ ListView في ScrollView ، لكن لا بأس إذا أصر القائم على المقابلة على أنني أعشش.
سيؤدي إضافة ListView إلى ScrollView إلى عدم اكتمال التحكم في ListView ، وعادة ما يتم عرض واحد فقط. ما هو السبب؟
ينتج عن حدث التمرير بين عناصر التحكم . لذلك ، من الضروري حساب ارتفاع عرض ListView من خلال عدد العناصر الموجودة في ListView لجعله معروضًا بالكامل. يتم توفير الطريقة التالية للرجوع إليها.
الحل كما يلي:
lv = (listView) findViewById (r.id.lv) ؛ محول = جديد myadapter () ؛ LV.SetAdapter (محول) ؛ setListViewheightBasedonchildren (LV) ؛ ------------------------------------------------- public void setListViewheightBasedOnchildren (ListView ListView) {listadapter listadapter = listView.getAdapter () ؛ if (listadapter == null) {return ؛ } int totalheight = 0 ؛ لـ (int i = 0 ؛ i <listadapter.getCount () ؛ i ++) {view listItem = listadapter.getView (i ، null ، listView) ؛ ListItem.Measure (0 ، 0) ؛ Totalheight += listItem.getMeasureDheight () ؛ } viewGroup.LayoutParams params = listView.getLayoutParams () ؛ params.height = Totalheight + (listView.getDividerHeight () * (listadapter.getCount () - 1)) ؛ params.Height += 5 ؛ listView.setLayoutParams (params) ؛ }أفضل طريقة للتعامل معها في هذه المرحلة هي: Custom ListView ، وتحميل طريقة OnMeasure () ، وتعيين جميع العروض.
استيراد Android.Widget.ListView ؛ / ** * * * @description: تنفيذ بسيط لـ ListView المضمن في ScrollView * * @file: scrollViewWithListView.java * * *// class public class scrollviewwithlistView يمتد listview attrs) ؛ } / ** * integer.max_value >> 2. إذا لم يتم تعيينه ، فإن الإعداد الافتراضي للنظام هو عرض قطعتين * / public void onMeasure (int widthmeasurespec ، int heaveasurespec) {int expenspec = assacurespec.makeasurespec (integer.max_value >> 2 ، exacurespec.ate.ater_maly. super.onmeasure (Widthmeasurespec ، expansspec) ؛ }}المحتوى أعلاه هو الحل لتضمين ListView في ScrollView الذي قدمه لك المحرر. آمل أن يكون ذلك مفيدًا للجميع!