android基础开发之scrollview

濮阳君浩
2023-12-01

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里面了。

 类似资料: