在这篇文章中,我们想看看几个滚动视图的变体或子类以及它们是如何使用的。以下是我们迄今为止涵盖的变体:
ScrollView - 超类
NestedScrollView - 子类
HorizontalScrollView - 子类
StickyScrollView - 子类和第三方库。
ParallaxScrollView - 子类和第三方库
1.滚动视图
Android ScrollView 教程和示例
一个 ScrollView 是一个 android 布局,它允许它的子视图垂直滚动。这很重要,因为在许多情况下您需要滚动内容。通常,诸如ListView和recyclerview之类的adapterviews具有滚动功能,但视图数量并不多。因此我们不使用滚动视图,否则我们会降低性能。
使用滚动视图,您只能向上或向下滚动。但是,如果您想要水平滚动,则可以使用 HorizontalScrollView。
此外, ScrollView 应该只有一个直接子级。这意味着,如果您希望添加多个子项,请使用诸如relativelayout或LinearLayout 之类的 ViewGroup 。然后将这些孩子添加到视图组,将视图组添加到 Scrollview。
Android 工程师建议您使用 NestedScrollView 而不是 ScrollView 进行垂直滚动。
这是因为后者提供了更大的用户界面灵活性和对材料设计滚动模式的支持。
ScrollView API 定义
滚动视图作为一个类驻留在android.widget包中并从 FrameLayout 派生:
public class ScrollView extends FrameLayout
下面是 FrameLayout 的继承层次结构:
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.FrameLayout
↳ android.widget.ScrollView
重要的 ScrollView 方法
(a).scrollTo(int x, int y)
在此方法中,您传递视图的滚动位置。这个版本还将滚动限制在我们孩子的边界上。
public static void scrollToUp(ScrollView scrollView) {
scrollView.scrollTo(0, 0);
}
(b).smoothScrollTo(int x, int y)
像scrollTo(int, int),但平滑滚动而不是立即滚动。
public static void smoothScrollToUp(ScrollView scrollView) {
scrollView.smoothScrollTo(0, 0);
}
(C).getChildAt(int index)
此方法将返回组中指定位置的视图。
View child = scrollView.getChildAt(0);
(d).getChildCount()
此方法返回组中孩子的数量。
int count = scrollView.getChildCount();
快速滚动视图示例
- ScrollView - 如何滚动到顶部
让我们看看如何滚动到滚动视图的顶部。
public static void scrollToUp(ScrollView scrollView) {
scrollView.scrollTo(0, 0);
}
2.如何在ScrollView中向下滚动
然后,我们还想了解如何以编程方式向下滚动到滚动视图的底部。
这是一个静态方法,我们将ScrollView实例作为参数传递。首先,我们获得height滚动视图的 ,因为它是子项计数。
在一天结束时,我们仍然使用该scrollTo()方法,传入x和y位置。
public static void scrollToDown(ScrollView scrollView) {
int y = scrollView.getHeight();
int count = scrollView.getChildCount();
if (count > 0) {
View view = scrollView.getChildAt(count - 1);
y = view.getBottom() + scrollView.getPaddingBottom();
}
scrollView.scrollTo(0, y);
}
3.如何平滑向上滚动
我们使用smoothScrollTo()方法并传递位置。
public static void smoothScrollToUp(ScrollView scrollView) {
scrollView.smoothScrollTo(0, 0);
}
4.如何平滑向下滚动
以下是我们如何以编程方式向下滚动。
public static void smoothScrollToDown(ScrollView scrollView) {
int y = scrollView.getHeight();
int count = scrollView.getChildCount();
if (count > 0) {
View view = scrollView.getChildAt(count - 1);
y = view.getBottom() + scrollView.getPaddingBottom();
}
scrollView.smoothScrollTo(0, y);
}
- 如何通过子视图计算ScrollView的高度
public static int calculaetHeightByView(ScrollView scrollView, View viewin) {
int topAll = viewin.getTop();
ViewParent p = viewin.getParent();
while (p != null) {
topAll += ((View) p).getTop();
if (p instanceof NestedScrollView) {
break;
}
p = p.getParent();
}
return topAll;
}
6.如何创建具有最大高度限制的滚动视图
如果我们希望为我们的滚动视图设置最大高度限制怎么办。好吧,我们可以简单地通过从android.widget.ScrollView类派生来创建自定义滚动视图,覆盖我们的构造函数以及onMeasure()方法。
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
/**
* ScrollView with maximum height limit
*/
public class MaxHeightScrollView extends ScrollView {
private final MaxSizeHelper mHelper = new MaxSizeHelper();
public MaxHeightScrollView(Context context) {
this(context, null);
}
public MaxHeightScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
if (isInEditMode()) {
return;
}
mHelper.init(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mHelper.getMaxHeight(), MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void setMaxHeight(int maxHeight) {
mHelper.setMaxHeight(maxHeight);
}
}
- 完整的 ScrollView 示例
让我们看一个完整的滚动视图示例。
(a).MainActivity.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private static final String TAG = "Main";
private ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Final Text View textview = ( Text View ) findViewById ( R . id . txt );
textView.setText(R.string.content);
Button btnUp = (Button) findViewById(R.id.btn_up);
Button btnDown = (Button) findViewById(R.id.btn_down);
btnUp.setOnClickListener(this);
btnDown.setOnClickListener(this);
scrollView = (ScrollView) findViewById(R.id.scroll);
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log . d ( TAG , "Finger press, Y coordinate:" + scrollView . getScrollY ());
break;
case MotionEvent.ACTION_UP:
Log . d ( TAG , "Finger raised, Y coordinate:" + scrollView . getScrollY ());
break;
case MotionEvent.ACTION_MOVE:
/**
* scrollView.getScrollY(): vertical distance scrolled by ScrollView
* scrollView.getHeight(): the height of one screen
* scrollView.getChildAt(0).getMeasuredHeight(): the total height of the child tags inside the ScrollView
* Here is the total height of the TextView
*
*/
Log . d ( TAG , "Finger swipe, Y coordinate:" + scrollView . getScrollY () +
",scrollView.getHeight():" +
scrollView.getHeight() +
",scrollView.getChildAt(0).getMeasuredHeight():" +
scrollView.getChildAt(0).getMeasuredHeight() +
",scrollView.getMeasuredHeight():" +
scrollView.getMeasuredHeight());
// Whether to slide to the bottom of the scroll bar: The total height of the TextView <= the height of a screen +
// The vertical distance of the ScrollView scroll
if (scrollView.getChildAt(0).getMeasuredHeight() <= scrollView.getHeight() +
scrollView.getScrollY()) {
Log . d ( TAG , "Already to the bottom bird!" );
textView.append(getResources().getString(R.string.content));
}
}
return false;
}
});
}
@Override
public void onClick(View v) {
/**
* scrollTo: each time the relative distance is scrolled from the starting (X or Y axis) position
* scrollBy: scroll from the last relative position
*/
switch (v.getId()) {
case R.id.btn_up:
scrollView.scrollBy(0, -30);
break;
case R.id.btn_down:
scrollView.scrollBy(0, 30);
}
}
}
(b).activity_main.xml
<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=".MainActivity">
<Button
android:id="@+id/btn_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/up"/>
<Button
android:id="@+id/btn_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/down"/>
<ScrollView
android:id="@+id/scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txt"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
- NestedScrollView
Android NestedScrollView 教程和示例。
NestedScrollView 是一个 ScrollView,它能够在新旧版本的 Android 上充当嵌套滚动父项和子项。
NestedScrollView 中默认启用嵌套滚动。
在许多情况下,建议使用 NestedScrollView 而不是 Scrol