2016-04-05 4 views
0

Hallo alle zusammen, hoffe, Sie können mir helfen!CoordinatorLayout (mit CollapsingToolbar) und ListView implementiert NestedScrollingChild und GestureDetector/GestureListener

Ich lese viel über unsing ListViews in NestedScroll und ich weiß, es ist nicht der beste Weg, aber ich brauche ein ListView statt andere Arten von View. Ich fand ein Beispiel auf GitHub, wie man NestedScrollingChild und GestureDetector.OnGestureListener in einer benutzerdefinierten Klasse implementiert erweitert ListView.

Hier ist mein Problem: Das Scrolling arbeitet, nicht die Art, wie ich brauchen, wenn nicht GestureDetektor implementieren. Die ListView wird zuerst durchgeblättert, bevor die Toolbar minimiert wird, aber ich möchte sie auf die andere Art öffnen ... zuerst die Symbolleiste ausblenden und dann in ListView blättern, also implementieren Sie Gesture Detektor. Nach der Implementierung konnte die ListView nicht gescrollt werden. Was ist falsch an meinem Code? Hier

ist das Layout:

<?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> 

Hier ist der Brauch 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; 
    } 
} 

Sie Layout wird als ein Fragment in einem ViewPager aufgeblasen.

Warum also der implementierte GestureListener nicht mit meinen Gesten umgehen?

Vielen Dank bis jetzt!

Antwort

0

Eine erste Antwort gefunden, aber ich bin mir nicht sicher über den richtigen Weg. Ich ändere 2 Dinge: Ich entferne den Listener in meiner benutzerdefinierten ListView und im Layout entferne ich die Umgebung NestedScrollView. Es funktioniert jetzt so, wie ich es möchte, aber warum der Hörer nicht funktioniert hat?

Verwandte Themen