Far*_*deh 10 android android-custom-view gesturedetector
我刚开始在Android中编写自定义视图(第一次),我意识到我需要实现滚动功能.
自定义视图还使用包含一些文本的标题(应该保持固定而不是滚动).
我已经阅读了关于GestureDetector.SimpleOnGestureListener和的文档Scroller.我还阅读了关于动画滚动手势的文档,但我发现这些示例很难理解.我还看了Stack Overflow上的其他一些问题.
使用我在Stack Overflow答案的文档中理解的作为参考来指导我,我已将以下内容添加到我的自定义视图中:
变量和字段:
private OverScroller mScroller;
private final GestureDetector mGestureDetector =
new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// Note 0 as the x-distance to prevent horizontal scrolling
scrollBy(0, (int) distanceY);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
final int maxScrollX = 0;
// wholeViewHeight is height of everything that is drawn
int wholeViewHeight = calculateWholeHeight();
int visibleHeight = getHeight();
final int maxScrollY = wholeViewHeight - visibleHeight;
mScroller.forceFinished(true);
mScroller.fling(0, // No startX as there is no horizontal scrolling
getScrollY(),
0, // No velocityX as there is no horizontal scrolling
- (int) velocityY,
0,
maxScrollX,
0,
maxScrollY);
invalidate();
return true;
}
@Override
public boolean onDown(MotionEvent e) {
if (!mScroller.isFinished()) {
mScroller.forceFinished(true);
}
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
初始化mScroller:
// Called from the constructor
private void init() {
mScroller = new OverScroller(getContext(), new FastOutLinearInInterpolator());
...
}
Run Code Online (Sandbox Code Playgroud)
东西onDraw():
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
...
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
}
}
Run Code Online (Sandbox Code Playgroud)
东西onTouchEvent():
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
Run Code Online (Sandbox Code Playgroud)
这些添加的结果是一个可以垂直滚动(而不是水平滚动)的自定义视图,但是有一些问题:
RecyclerView或ScrollView),没有边缘发光效果有人可以解释滚动在自定义视图中的工作原理以及如何使用这些功能正确实现它吗?
我可以提供一个简单的布局,它有一个固定的标题和垂直滚动的内容,听起来像你想要的那样并且不需要复杂的编程?从长远来看,它可以使您不必花费数小时的研究和编程:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true">
<FrameLayout
android:id="@+id/scrolling_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
使用此布局,您可以FrameLayout使用固定的标题视图替换或插入带有 ID 的“标题”。然后,您会将FrameLayout滚动内容放在ID 为“scrolling_content”的内部。该NestedScrollingView会直接定位你的固定头下方,并自动给你你想要的滚动行为没有任何进一步的代码。
| 归档时间: |
|
| 查看次数: |
2239 次 |
| 最近记录: |