ScrollView에 포함된 ListView는 하나의 해결 방법만 표시합니다.

1987 단어 scrollviewlistview
일반적으로 우리는 ScrollView에 ListView를 끼워 넣지 않지만, 면접관이 굳이 끼워 넣지 않아도 된다.
ScrollView에 ListView를 추가하면 listview 컨트롤이 완전하지 않습니다. 보통 하나만 표시됩니다. 도대체 어떤 이유입니까?
두 컨트롤의 스크롤 이벤트 충돌로 인해따라서listview의 item 수량을 통해listview의 디스플레이 높이를 계산하여 완전하게 보여야 한다. 다음은 참고할 수 있는 방법을 제공한다.
해결 방법은 다음과 같습니다.

lv = (ListView) findViewById(R.id.lv); 
adapter = new MyAdapter(); 
lv.setAdapter(adapter); setListViewHeightBasedOnChildren(lv); 
--------------------------------------------------- 
public 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); }
현재 단계에서 가장 좋은 방법은 사용자 정의 ListView, onMeasure () 방법을 다시 불러오고 모든 디스플레이를 설정하는 것입니다.

import android.widget.ListView; 
/** * * @Description: scrollview   listview   
* * @File: ScrollViewWithListView.java 
* * * @Version */ 
public class ScrollViewWithListView extends ListView { 
public ScrollViewWithListView(android.content.Context context, android.util.AttributeSet attrs) { super(context, attrs); } 
/** 
* Integer.MAX_VALUE >> 2, ,  */ 
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); 
} 
}
위의 내용은 여러분에게 소개된 ScrollView에 ListView를 삽입하여 하나만 표시하는 해결 방법입니다. 여러분에게 도움이 되기를 바랍니다!

좋은 웹페이지 즐겨찾기