当前位置:天才代写 > tutorial > 安卓教程 > android基础开发之scrollview

android基础开发之scrollview

2021-02-25 13:05 星期四 所属: 安卓教程 浏览:28

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

 

    关键字:

天才代写-代写联系方式