最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ScrollView嵌套ListView滑動沖突的解決方法

 更新時間:2016年11月24日 10:12:14   作者:Lindroy  
這篇文章主要介紹了ScrollView嵌套ListView滑動沖突的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

ScrollView和ListView這兩個控件想必大家都不會陌生,但是這兩者嵌套使用的時候就會出現(xiàn)麻煩。比如,我們?nèi)绻朐贚istView下面添加其他的布局或者控件,然后想讓它們作為一個整體都可以滑動的話,最常想到的就是用一個ScrollView把它們包裹起來。想法似乎很美好,但是現(xiàn)實就有點殘酷了。我們可以寫一個小例子體驗一下。

首先創(chuàng)建一個Activity,在它的布局文件上放置一個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));
  }
}

確保你當前的Activity為啟動Activity,然后運行App,可以看到如下的效果:

好,看起來沒有問題,但是如果這時我們需要在這個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">

    <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的頭部和底部加了幾個控件,然后把所有的控件都用一個線性布局包裹起來,再把最外層的布局改為ScrollView,再次運行,麻煩出現(xiàn)了:

天!我們的ListView只剩下小小的一行了!試著滑動一下,發(fā)現(xiàn)滑動是沒有問題的,就是只能顯示一行。那我們該怎么辦呢?

別著急,有一個簡單的方法可以起死回生。我們可以自定義一個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,//右移運算符,相當于除于4
        MeasureSpec.AT_MOST);//測量模式取最大值
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);//重新測量高度
  }
}

在這個ListView中我們重寫了onMeasure方法,然后重新定義heightMeasureSpec參數(shù),它的大小取最大值的四分之一(一般的做法),測量模式取最大值,然后調(diào)用父類的構(gòu)造方法重新傳入heightMeasureSpec參數(shù)。這些步驟是為了保證ListView的高度不出現(xiàn)問題。完成后,我們在布局文件中使用自定義的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>

運行之后,發(fā)現(xiàn)問題解決了!ListView可以完整地顯示,而且也可以滑動到頭部和頂部的布局。

其實要想顯示ListView的頭部或者底部布局或者控件的話不一定要用ScrollView,我們也可以將頭部和底部作為一個整體的布局,即頭布局或者腳布局,然后調(diào)用ListView的addHeaderView方法或者addFooterView方法就可以將它添加到ListView的頭部或者底部了。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android TextView加下劃線的方法

    android TextView加下劃線的方法

    這篇文章主要介紹了android TextView加下劃線的方法,大家可以在項目代碼里試試
    2013-11-11
  • Android SQLite數(shù)據(jù)庫中的表詳解

    Android SQLite數(shù)據(jù)庫中的表詳解

    這篇文章主要介紹了Android SQLite數(shù)據(jù)庫中的表詳解的相關(guān)資料,這里附有實例代碼,需要的朋友可以參考下
    2017-01-01
  • Android自定義折線圖控件的完整步驟

    Android自定義折線圖控件的完整步驟

    折線圖是常用的圖表之一,最近的工作中又遇到了相關(guān)的需求,所以下面這篇文章主要給大家介紹了關(guān)于Android自定義折線圖控件的完整步驟,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • Android 實例開發(fā)基于ArcSoft實現(xiàn)人臉識別

    Android 實例開發(fā)基于ArcSoft實現(xiàn)人臉識別

    人臉識別,是基于人的臉部特征信息進行身份識別的一種生物識別技術(shù)。用攝像機或攝像頭采集含有人臉的圖像或視頻流,并自動在圖像中檢測和跟蹤人臉,進而對檢測到的人臉進行識別的一系列相關(guān)技術(shù),通常也叫做人像識別、面部識別
    2021-11-11
  • Android 10 啟動之servicemanager源碼解析

    Android 10 啟動之servicemanager源碼解析

    這篇文章主要為大家介紹了Android 10 啟動之servicemanager源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Android 5.0及以上編程實現(xiàn)屏幕截圖功能的方法

    Android 5.0及以上編程實現(xiàn)屏幕截圖功能的方法

    這篇文章主要介紹了Android 5.0及以上編程實現(xiàn)屏幕截圖功能的方法,結(jié)合實例形式分析了Android5.0以上實現(xiàn)截圖功能的相關(guān)類、函數(shù)及權(quán)限控制等操作技巧,需要的朋友可以參考下
    2018-01-01
  • Android掃描二維碼時出現(xiàn)用戶禁止權(quán)限報錯問題解決辦法

    Android掃描二維碼時出現(xiàn)用戶禁止權(quán)限報錯問題解決辦法

    這篇文章主要介紹了Android掃描二維碼時出現(xiàn)用戶禁止權(quán)限報錯問題解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android實現(xiàn)日歷控件示例代碼

    Android實現(xiàn)日歷控件示例代碼

    本篇文章主要介紹了Android實現(xiàn)日歷控件示例代碼,實例講解了Android日期與時間相關(guān)控件的相關(guān)使用技巧,具有一定參考價值,需要的朋友可以參考下
    2017-03-03
  • Flutter時間軸Timeline的實現(xiàn)

    Flutter時間軸Timeline的實現(xiàn)

    時間軸在很多地方都可以用的到,本文介紹了Flutter時間軸Timeline的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android讀取properties配置文件的實例詳解

    Android讀取properties配置文件的實例詳解

    這篇文章主要介紹了Android讀取properties配置文件的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09

最新評論

通化县| 广河县| 罗山县| 崇明县| 信阳市| 福清市| 饶阳县| 固镇县| 竹北市| 印江| 桂林市| 鹤壁市| 邹平县| 天峻县| 西乡县| 金阳县| 荆门市| 土默特左旗| 丹巴县| 海南省| 定日县| 桐城市| 濮阳市| 临武县| 日喀则市| 宜宾市| 渝中区| 望奎县| 逊克县| 孝义市| 承德县| 盐池县| 华阴市| 恩施市| 闵行区| 南漳县| 武山县| 陆丰市| 辽中县| 抚州市| 榆树市|