日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

ScrollView中嵌套ListView

發(fā)布時(shí)間:2025/6/15 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ScrollView中嵌套ListView 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

放置比較少的ListView組件效果圖:

???????????????????????

Item布局文件? list_view_item.xml? 布局文件的內(nèi)容:

<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時(shí),ListView組件比較少時(shí),不需要滑動(dòng),這幾個(gè)ListView組件都可以正常顯示,當(dāng)放置多時(shí)就不能如我們所愿,我們可以使用ScrollView組件來(lái)嵌套ListView組件這樣就可正常顯示了,原理就是? 我們?cè)诖a中動(dòng)態(tài)設(shè)置ListView組件的高度,下面我們來(lái)看srollView視圖中嵌套ListView組件實(shí)現(xiàn)的方法:

/*
?* 動(dòng)態(tài)設(shè)置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()獲取子項(xiàng)間分隔符占用的高度
?? ? ?
?? ?? // params.height最后得到整個(gè)ListView完整顯示需要的高度
?? ? ?
?? ?? listView.setLayoutParams(params);
?? ? ?
?? ?}

在為L(zhǎng)istView組件的實(shí)例設(shè)置適配器時(shí)候,要對(duì)組件高度進(jìn)行動(dòng)態(tài)設(shè)置,調(diào)用上面的?setListViewHeightBasedOnChildren(listView)方法即可:

?????????? l istView.setAdapter(adapter);
?? ??? ?setListViewHeightBasedOnChildren(listView);
?? ??? ?listView1.setAdapter(adapter1);
?? ??? ?setListViewHeightBasedOnChildren(listView1);
?? ??? ?listView2.setAdapter(adapter2);

?? ??? ?setListViewHeightBasedOnChildren(listView2);


動(dòng)態(tài)配置ListView組件高度以后,效果圖上方? 第二張片所示!!!!!!

總結(jié)

以上是生活随笔為你收集整理的ScrollView中嵌套ListView的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。