大家好,希望大家能帮到我!
我读了很多关于在 NestedScroll 中取消 ListViews 的内容,我知道这不是最好的方法,但我需要一个 ListView 而不是其他类型的视图。我在 GitHub 上找到了一个示例,如何在自定义类扩展 ListView 中实现 NestedScrollingChild 和 GestureDetector.OnGestureListener。
这是我的问题:如果不实施GestureDetektor ,滚动工作不是我需要的方式。ListView 在折叠工具栏之前首先向下滚动,但我希望它以另一种方式...首先折叠工具栏然后滚动 ListView,因此实现 Gesture Detektor。实现ListView 后无法滚动。我的代码有什么问题?
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="de.example.nestedScrollTest.MainActivity"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="@+id/pFragmentList_CC_frame"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/pFragmentList_ABL_AppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:background="@drawable/draw_bg_standard_element"
android:fitsSystemWindows="true"
android:layout_gravity="center_vertical"
app:contentScrim="@color/colorPrimary"
android:id="@+id/pFragmentList_CTB_collapseBar">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:background="@drawable/img_theme_blank"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true">
<de.example.nestedScrollTest.pakMainFragments.NestedScrollingListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="de.example.nestedScrollTest.MainActivity"
android:id="@+id/pFragmentList_LV_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="5.0sp"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:scrollbars="vertical" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
这是自定义的 NestedScrolling ListView:
import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChildHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ListView;
public class NestedScrollingListView extends ListView implements NestedScrollingChild, GestureDetector.OnGestureListener {
private final NestedScrollingChildHelper mNestedScrollingChildHelper;
private GestureDetectorCompat mDetector;
public NestedScrollingListView(Context context) {
this(context, null);
}
public NestedScrollingListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public NestedScrollingListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mNestedScrollingChildHelper = new NestedScrollingChildHelper(this);
mDetector = new GestureDetectorCompat(context, this);
setNestedScrollingEnabled(true);
}
@Override
public void setNestedScrollingEnabled(boolean enabled) {
mNestedScrollingChildHelper.setNestedScrollingEnabled(enabled);
}
@Override
public boolean isNestedScrollingEnabled() {
return mNestedScrollingChildHelper.isNestedScrollingEnabled();
}
@Override
public boolean startNestedScroll(int axes) {
return mNestedScrollingChildHelper.startNestedScroll(axes);
}
@Override
public void stopNestedScroll() {
mNestedScrollingChildHelper.stopNestedScroll();
}
@Override
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
return mNestedScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
}
@Override
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
return mNestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
}
@Override
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
return mNestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
}
@Override
public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
return mNestedScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mNestedScrollingChildHelper.onDetachedFromWindow();
}
@Override
public boolean hasNestedScrollingParent() {
return mNestedScrollingChildHelper.hasNestedScrollingParent();
}
@Override
public boolean onTouchEvent(MotionEvent event){
final boolean handled = mDetector.onTouchEvent(event);
if (!handled && event.getAction() == MotionEvent.ACTION_UP) {
stopNestedScroll();
}
return true;
}
@Override
public boolean onDown(MotionEvent e) {
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
return true;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
dispatchNestedPreScroll(0, (int) distanceY, null, null);
dispatchNestedScroll(0, 0, 0, 0, null);
return true;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return true;
}
}
它们的布局在 ViewPager 中膨胀为片段。
那么为什么实现的 GestureListener 不处理我的手势呢?
谢谢你!