ScrollView嵌套ListView滑動(dòng)沖突的解決方法
ScrollView和ListView這兩個(gè)控件想必大家都不會(huì)陌生,但是這兩者嵌套使用的時(shí)候就會(huì)出現(xiàn)麻煩。比如,我們?nèi)绻朐贚istView下面添加其他的布局或者控件,然后想讓它們作為一個(gè)整體都可以滑動(dòng)的話,最常想到的就是用一個(gè)ScrollView把它們包裹起來。想法似乎很美好,但是現(xiàn)實(shí)就有點(diǎn)殘酷了。我們可以寫一個(gè)小例子體驗(yàn)一下。
首先創(chuàng)建一個(gè)Activity,在它的布局文件上放置一個(gè)ListView:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.lin.mr.mystudy.scrollview.TestActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
然后在代碼中使用for循環(huán)生成一些數(shù)據(jù),并使用ArrayAdapter適配數(shù)據(jù)。這里允許我偷一下懶,ListView的item布局直接使用Android提供的R.layout.simple_list_item_1,而沒有自己去自定義。
public class TestActivity extends Activity { private ListView listView; private ArrayList<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); listView = (ListView) findViewById(R.id.listView); findViewById(R.id.ll_container); list = new ArrayList<>(); //生成需要顯示到ListView中的數(shù)據(jù) for (int i = 0; i < 30; i++) { list.add("這是數(shù)據(jù)"+i); } //使用ArrayAdapter適配數(shù)據(jù) listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list)); } }
確保你當(dāng)前的Activity為啟動(dòng)Activity,然后運(yùn)行App,可以看到如下的效果:
好,看起來沒有問題,但是如果這時(shí)我們需要在這個(gè)ListView的頭部或者底部添加一些控件,然后讓它們整體都可以滑動(dòng)呢?我們可以先這樣試試:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.lin.mr.mystudy.scrollview.TestActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕一" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕二" /> </LinearLayout> </ScrollView>
在ListView的頭部和底部加了幾個(gè)控件,然后把所有的控件都用一個(gè)線性布局包裹起來,再把最外層的布局改為ScrollView,再次運(yùn)行,麻煩出現(xiàn)了:
天!我們的ListView只剩下小小的一行了!試著滑動(dòng)一下,發(fā)現(xiàn)滑動(dòng)是沒有問題的,就是只能顯示一行。那我們?cè)撛趺崔k呢?
別著急,有一個(gè)簡單的方法可以起死回生。我們可以自定義一個(gè)ListView:
/** * 自定義ListView */ public class MyListView extends ListView { public MyListView(Context context) { super(context); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,//右移運(yùn)算符,相當(dāng)于除于4 MeasureSpec.AT_MOST);//測量模式取最大值 super.onMeasure(widthMeasureSpec,heightMeasureSpec);//重新測量高度 } }
在這個(gè)ListView中我們重寫了onMeasure方法,然后重新定義heightMeasureSpec參數(shù),它的大小取最大值的四分之一(一般的做法),測量模式取最大值,然后調(diào)用父類的構(gòu)造方法重新傳入heightMeasureSpec參數(shù)。這些步驟是為了保證ListView的高度不出現(xiàn)問題。完成后,我們?cè)诓季治募惺褂米远x的ListView:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.lin.mr.mystudy.scrollview.TestActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <com.lin.mr.mystudy.scrollview.MyListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"> </com.lin.mr.mystudy.scrollview.MyListView> <Button android:layout_margin="4dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕一" /> <Button android:layout_margin="4dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕二" /> </LinearLayout> </ScrollView>
運(yùn)行之后,發(fā)現(xiàn)問題解決了!ListView可以完整地顯示,而且也可以滑動(dòng)到頭部和頂部的布局。
其實(shí)要想顯示ListView的頭部或者底部布局或者控件的話不一定要用ScrollView,我們也可以將頭部和底部作為一個(gè)整體的布局,即頭布局或者腳布局,然后調(diào)用ListView的addHeaderView方法或者addFooterView方法就可以將它添加到ListView的頭部或者底部了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
flutter 實(shí)現(xiàn)多布局列表的示例代碼
這篇文章主要介紹了flutter 實(shí)現(xiàn)多布局列表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02android實(shí)現(xiàn)簡易登錄注冊(cè)界面及邏輯設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡易登錄注冊(cè)界面及邏輯設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁效果
這篇文章主要介紹了Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁效果,本文通過實(shí)例代碼結(jié)合文字說明的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-03-03android打開應(yīng)用所在的市場頁面進(jìn)行評(píng)分操作的方法
這篇文章主要介紹了android打開應(yīng)用所在的市場頁面進(jìn)行評(píng)分操作的方法,涉及Android操作市場頁面評(píng)分效果的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04anroid開發(fā)教程之spinner下拉列表的使用示例
這篇文章主要介紹了anroid的spinner下拉列表的使用示例,需要的朋友可以參考下2014-04-04Android獲取點(diǎn)擊屏幕的位置坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Android獲取點(diǎn)擊屏幕的位置坐標(biāo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android開發(fā)實(shí)現(xiàn)按鈕點(diǎn)擊切換背景并修改文字顏色的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)按鈕點(diǎn)擊切換背景并修改文字顏色的方法,涉及Android界面布局與相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下2018-01-01Android自定義View實(shí)現(xiàn)圓弧進(jìn)度效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓弧進(jìn)度效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11