ScrollView中嵌套ListView
放置比較少的ListView組件效果圖:
???????????????????????
Item布局文件? list_view_item.xml? 布局文件的內容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? xmlns:tools="http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="50dp"
???? >
??? <ImageView
??????? android:id="@+id/list_view_img"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:layout_centerVertical="true"
??????? android:layout_marginLeft="5dip"
??????? android:padding="5dip" />
??? <TextView
??????? android:id="@+id/list_view_text"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:layout_centerVertical="true"
??????? android:gravity="center_vertical|center_horizontal"
??????? android:layout_marginLeft="50dip"
??????? android:text="@string/title_listview_activity"
??????? android:textColor="#000"
??????? android:textSize="16dp"
??????? tools:context=".ListViewActivity" />
?? ?
? <ImageView
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:layout_centerVertical="true"
??????? android:layout_marginLeft="260dip"
??????? android:src="@drawable/img_go"
??????? android:padding="5dip" />
</RelativeLayout>
在Activity界面中放置ListView時,ListView組件比較少時,不需要滑動,這幾個ListView組件都可以正常顯示,當放置多時就不能如我們所愿,我們可以使用ScrollView組件來嵌套ListView組件這樣就可正常顯示了,原理就是? 我們在代碼中動態設置ListView組件的高度,下面我們來看srollView視圖中嵌套ListView組件實現的方法:
/*
?* 動態設置ListView組建的高度
?*?
?* */
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;// if without this statement,the listview will be
?? ? ?
?? ?? // a
?? ? ?
?? ?? // little short
?? ? ?
?? ?? // listView.getDividerHeight()獲取子項間分隔符占用的高度
?? ? ?
?? ?? // params.height最后得到整個ListView完整顯示需要的高度
?? ? ?
?? ?? listView.setLayoutParams(params);
?? ? ?
?? ?}
在為ListView組件的實例設置適配器時候,要對組件高度進行動態設置,調用上面的?setListViewHeightBasedOnChildren(listView)方法即可:
?????????? l istView.setAdapter(adapter);?? ??? ?setListViewHeightBasedOnChildren(listView);
?? ??? ?listView1.setAdapter(adapter1);
?? ??? ?setListViewHeightBasedOnChildren(listView1);
?? ??? ?listView2.setAdapter(adapter2);
?? ??? ?setListViewHeightBasedOnChildren(listView2);
動態配置ListView組件高度以后,效果圖上方? 第二張片所示!!!!!!
總結
以上是生活随笔為你收集整理的ScrollView中嵌套ListView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为ListView每个Item上面的按钮
- 下一篇: 对ListView滚动状态的监听