一、ScrollView中嵌套ListView/GridView:
創(chuàng)新互聯(lián)建站從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元井研做網(wǎng)站,已為上家服務(wù),為井研各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
方法一:計(jì)算ListView的高度
public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition 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)); listView.setLayoutParams(params); } |
處理方式:在將樓上這個(gè)方法拷貝到工具類中,在ListVIew.setAdapter 的下方調(diào)用這個(gè)方法即可,如下所示:
apprAdapter = new ApprovedPersonAdapter(mContext, mListAped); mListView.setAdapter(apprAdapter); ListHeightUtils.setListViewHeightBasedOnChildren(mListView); |
方法二:重寫ListView/GridView
重寫ListView:
public class MyScrollVListView extends ListView { public MyScrollVListView(Context context) { public MyScrollVListView(Context context, AttributeSet attrs) { public MyScrollVListView(Context context, AttributeSet attrs, int defStyle) { @Override } |
重寫GridView:
public class MyScrollGridView extends GridView { private boolean haveScrollbar = false; public MyScrollGridView(Context context) { super(context); } public MyScrollGridView(Context context, AttributeSet attrs) { super(context, attrs); } public MyScrollGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * 設(shè)置是否有ScrollBar,當(dāng)要在ScollView中顯示時(shí),應(yīng)當(dāng)設(shè)置為false。 默認(rèn)為 true * * @param haveScrollbars */ public void setHaveScrollbar(boolean haveScrollbar) { this.haveScrollbar = haveScrollbar; } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } } |
處理方式:和正常的布局ListView/GridView一樣,只是這里需要用全路徑,即可解決,如下所示:
<com.derek.views.MyScrollVListView android:id="@+id/mListView" android:layout_width="match_parent" android:layout_height="wrap_content" > </com.derek.views.MyScrollVListView> |
方法三:LinerLayout代替ListView/GridView
1、先在需要布局ListView/GridView的地方布局上一個(gè)LinerLayout,如下所示:
<LinearLayout android:id="@+id/mLinearLayout" android:layout_width="match_parent" |
2、在FindViewById該對象,如下所示:
mLinearLayout= (LinearLayout) findViewById(R.id.mLinearLayout); |
3、初始化數(shù)據(jù)
/** @Override |
如此將數(shù)據(jù)組裝好后,該問題也基本解決了,mLinearLayout.removeAllViews();
表示先清空所有的View,便于每次添加進(jìn)去的都是新的View。此方法在很多動態(tài)添加數(shù)據(jù)中都會用上,ListView的方法與此一樣。
二、ScrollView嵌套ListView時(shí),嵌套的布局是A布局+ListView+B布局的形式,當(dāng)A布局中的內(nèi)容超過一屏?xí)r,ListView的最后一個(gè)Item總是顯示不全?
解決方案: (1)ListView不能用自定義的,必須用原生的ListView;
(2)ListView的寬高都用 match_parent或fill_parent;
(3)用計(jì)算高度的方式去實(shí)現(xiàn)setListViewHeightBasedOnChildren(ListView);
(4)adapter的item的根布局必須為LinearLayout布局,不然計(jì)算高度時(shí)會有問題;
(5)ScrollView中添加屬性 android:fillViewport="true"
利用以上的5點(diǎn),基本可以解決問題,我是這樣解決,如果有沒有解決的,請注意布局上看看有什么問題,把多余的功能去掉,從最基本的只顯示ListView開始調(diào)節(jié),一個(gè)功能一個(gè)功能的加上去,看看是哪里出問題,就逐步排除并解決。
ListView中嵌套ListView的處理在后續(xù)的文章中單獨(dú)講解,其實(shí)這三種方法網(wǎng)上也基本有的,只是便于android的學(xué)習(xí)記錄一下這個(gè)過程。
分享名稱:總結(jié)ScrollView中嵌套ListView/GridView的方法
URL網(wǎng)址:http://chinadenli.net/article36/joijsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、移動網(wǎng)站建設(shè)、虛擬主機(jī)、網(wǎng)站收錄、響應(yīng)式網(wǎng)站、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)