在一些初學(xué)者中很多的同學(xué)就會(huì)遇到這樣的問題像標(biāo)題所說的那樣的問題。
什么廢話我就不說了
/*
* listview適應(yīng)Scrollview
*/
public class ListViewForScrollView extends ListView {
public ListViewForScrollView(Context context) {
super(context);
}
public ListViewForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ListViewForScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
/**
* 重寫該方法,達(dá)到使ListView適應(yīng)ScrollView的效果
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
(解釋上面的代碼)
寫個(gè)自定義類繼承l(wèi)istview寫下三個(gè)構(gòu)造方法這個(gè)我就不解釋了,然后重寫onMeasure方法??梢灾苯影焉厦娴拇a復(fù)制過去。
xml里面這樣寫:其中的xxx.xxxx 是你重寫的這個(gè)類的包命 自定義類
<xxx.xxxx.ListViewForScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@color/community_list_gray"
android:dividerHeight="1dp" >
</xxx.xxxx.ListViewForScrollView>
接下來的代碼就是跟寫listview一樣的寫就是了,注意的是如果是要做listview嵌套listview,那么就把這個(gè)類當(dāng)成listview的條目。GridView嵌套也是這個(gè)原理只不過是把繼承的類改成GridView,下面的方法都不用變。
貼上一張效果圖

Screenshot_2017-01-07-12-46-49-1533014422.png
額上傳了發(fā)現(xiàn)似乎有點(diǎn)大,不過不用在意這些細(xì)節(jié)。
下面的就是下載地址(說明一下 我只是把這兩個(gè)類的代碼文件放在下面而不是工程,因?yàn)槲覒胁幌雽?。但是只要你不是傻子一般都是能明白?br> http://pan.baidu.com/s/1dFuYSCD