android基礎開發之scrollview

來源:互聯網
上載者:User

標籤:

scrollView 是android系統提供的一種 特殊的展示view。

其實我們很早就遇到過scrollview的東東,比如listview。

而google官方文檔也提出,不要混合使用scrollview & listview,應為他們有一定的衝突性。

本文後面會分析和解決這個問題。

1.認識scrollview

 

Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container.ScrollView only supports vertical scrolling. For horizontal scrolling, use HorizontalScrollView.

上面這段就是官方描述,大體講了幾個問題。

  • scrollview本質上就是一個framelayout,所以scrollview內部建議只存放一個view,比如linerlayout。
  • Textview & ListView 本生就有scroll的功能,所以不建議和scrollview一起使用。
  • 水平方向的scrollview ,可以使用HorizontalScrollview

2.使用scrollview。

官方文檔上,第一行屬性就是android:fillViewport 可見這個屬性對scrollview至關重要!

這是屬性什麼作用:

當你的scrollview的子view,比如說linearlayout,在螢幕夠得情況下,想撐滿整個螢幕,但是你發現無論你怎麼設定layout_height = "match_parent".

它就是不會把整個螢幕撐滿。只會按照wrap_content 的方式顯示。這個就是scrollview特殊的地方。使用android:fillViewport就可以解決這個問題。

3.scrollview & listview

在某些特殊情況下scroollview 和 listview需要同時使用,以下是參考網上的例子寫的一個demo:

布局:

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/act_solution_1_sv"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#0dfeef">    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical">        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="\nListView上方資料\n" />        <com.joyfulmath.sample.javanetwork.androidbase.scrollview.view.ScrollListView            android:id="@+id/act_solution_1_lv"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1">        </com.joyfulmath.sample.javanetwork.androidbase.scrollview.view.ScrollListView>        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="\nListView下方資料\n" />    </LinearLayout></ScrollView>

 

 

import android.content.Context;import android.util.AttributeSet;import android.widget.ListView;/** * @author deman.lu email [email protected]****.com * @version on 2016-03-10 11:12 */public class ScrollListView extends ListView {    public ScrollListView(Context context) {        super(context);    }    public ScrollListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);    }}

如此自訂listview以後,listview就可以全部展示在scrollview裡面了。

android基礎開發之scrollview

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.