CoordinateorLayout(使用CollapsingToolbar)和ListView实现NestedScrollingChild和GestureDetector / GestureListener

时间:2016-04-05 15:14:06

标签: android listview nestedscrollview gesturelistener

大家好,希望你能帮助我!

我在NestedScroll中读了很多关于unsing ListViews的内容,我知道它不是最好的方法,但我需要一个ListView而不是其他类型的View。 我在GitHub上找到了一个例子,如何在自定义类中实现NestedScrollingChild和GestureDetector.OnGestureListener扩展ListView。

这是我的问题: 如果没有实现 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不处理我的手势?

到目前为止,谢谢!

1 个答案:

答案 0 :(得分:0)

找到第一个答案,但我不确定正确的方法。 我更改了两件事:我在自定义ListView中删除了Listener,在布局中我删除了周围的NestedScrollView。它现在按照我想要的方式工作,但为什么听众没有工作?