2016-04-05 5 views
0

こんにちは、一緒に、私を助けることを願って!CoordinateorLayout(CollapsingToolbarを使用)とListViewはNestedScrollingChildとGestureDetector/GestureListenerを実装しています

NestedScrollのunsing ListViewについて多くのことを読んでいますが、それが最良の方法ではないことは分かっていますが、他のタイプのViewではなくListViewが必要です。 カスタムクラスのNestedScrollingChildとGestureDetector.OnGestureListenerを実装する方法をGitHubで見つけると、ListViewが拡張されます。ここで

は私の問題です: は、スクロール作業は GestureDetektorを実装していない場合、私はを必要とする方法ではありません。リストビューは最初にスクロールされてからツールバーを折りたたむ前にスクロールされますが、逆にしたいのですが...ツールバーを最初に折り畳んで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のリストビューである:彼らはレイアウト

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は私のジェスチャーを処理しませんか?

ありがとうございました!

答えて

0

最初の回答が見つかりましたが、正しい方法についてはわかりません。 私は2つのことを変更します:私は私のカスタムListViewでリスナーを削除し、レイアウトでは、NourcedScrollViewの周辺を削除します。それは今、私の望むやり方ですが、なぜリスナーが働いていないのですか?

関連する問題