일반적으로 ScrollView에서 ListView를 중첩하지는 않지만 면접관이 내가 둥지를 틀어야한다고 주장해도 괜찮습니다.
ScrollView에 ListView를 추가하면 ListView 컨트롤이 불완전하게되며 일반적으로 하나만 표시됩니다. 이유는 무엇입니까?
두 스크롤 이벤트는 두 컨트롤 사이의 결과가 충돌합니다 . 따라서 ListView의 항목 수를 통해 ListView의 디스플레이 높이를 계산하여 완전히 표시해야합니다. 참조를 위해 다음 방법이 제공됩니다.
솔루션은 다음과 같습니다.
lv = (listView) findViewById (r.id.lv); 어댑터 = 새로운 myAdapter (); Lv. 세트 레이이프터 (어댑터); setListViewHeightBasedOnchildren (LV); ---------------------------------------------- 공개 void setListViewHeightBasedOnchildren (ListView ListView) {Listadapter listAdapter = listView.getAdapter (); if (listadapter == null) {return; } int totalheight = 0; for (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); }이 단계에서이를 처리하는 가장 좋은 방법은 다음과 같습니다. 사용자 정의 목록보기, OnMeasure () 메소드를 과부하하고 모든 디스플레이를 설정하는 것입니다.
android.widget.listview import; / ** * * @description : scrollview * * @file : scrollviewwithlistview.java * * * @version */ public class scrollviewwithListView instrends ListView {public scrollViewWithListView (android.content.context, and and and atributes tet and and atributetet. 슈퍼 (컨텍스트, attrs); } / ** * integer.max_value >> 2. 설정되지 않으면 시스템 기본 설정은 두 조각을 표시하는 것입니다 * / public void onmeasure (int widthmeasurespec, int heightmeasurespec) {int expandspec = grestpec.makemeasurespec (integer.max_value >> 2, gresecurespec.at_); super.onmeasure (widthmeasurespec, expandspec); }}위의 내용은 편집자가 소개 한 ScrollView에 ListView를 포함시키는 솔루션입니다. 나는 그것이 모두에게 도움이되기를 바랍니다!