日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ScrollView中使用ListView

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ScrollView中使用ListView 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自?http://blog.csdn.net/fzh0803/article/details/7971391

由于scrollview和listview不能直接共存,在scrollview中直接使用lsitview的話只會顯示一個條目,要使他們共存,

據我所知,有三種方法:

1。如果listview的高度是一定的話,可以重寫一個listview在onmesure方法里設定固定高度,如下代碼:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
?? ?super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, 0) );?
? ? ? ? int childHeight = getMeasuredHeight() - (getListPaddingTop() + getListPaddingBottom() + ?getVerticalFadingEdgeLength() * 2);?
? ? ? ? int fullHeight = getListPaddingTop() + getListPaddingBottom() + childHeight*(getCount());?
? ? ? ? setMeasuredDimension(getMeasuredWidth(), fullHeight);?}

2.如果listview中的高度不是固定的,可以在初始化完listview后設置listview的高度。可以使用以下函數:

public void setListViewHeightBasedOnChildren(ListView listView) {
? ? ? ? SettingListAdapter listAdapter = (SettingListAdapter) listView.getAdapter();?
? ? ? ? if (listAdapter == null) {
? ? ? ? ? ? return;
? ? ? ? } ? ? ??
? ? ? ? int totalHeight = 0;
? ? ? ? for (int i = 0; i < listAdapter.getCount(); i++) {
? ? ? ? ? ? View listItem = listAdapter.getView(i, null, listView);
? ? ? ? ? ? if(listItem != null){
? ? ? ? ? ? ? ? listItem.setLayoutParams(new LayoutParams(
? ? ? ? ? ? ? ? ? ? ? ? LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
? ? ? ? ? ? ? ? listItem.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
? ? ? ? ? ? ? ? totalHeight += listItem.getMeasuredHeight();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ViewGroup.LayoutParams params = listView.getLayoutParams();
? ? ? ? params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))?
? ? ? ? ? ? ? ? + listView.getPaddingTop() + listView.getPaddingBottom();
? ? ? ? listView.setLayoutParams(params);
? ? }

這樣就可以使listview設定為正確高度。

3.用一個linearLayout來代替listview

轉載于:https://www.cnblogs.com/DonkeyTomy/articles/3246149.html

總結

以上是生活随笔為你收集整理的ScrollView中使用ListView的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。