Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android編程入門 >> android基礎開發之scrollview

android基礎開發之scrollview

編輯:Android編程入門

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 luyuanchun032@****.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裡面了。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved