2016-04-05 9 views
0

Hep birlikte, umarım bana yardımcı olabilirsiniz!CoordinateorLayout (CollapsingToolbar ile birlikte) ve ListView uygular NestedScrollingChild ve GestureDetector/GestureListener

Ben NestedScroll içinde listviews unsing hakkında çok şey okudum ve ben bu en iyi yol olmadığını biliyorum ama yerine Görünüm'deki diğer türleri bir ListView gerekir. GitHub'da NestedScrollingChild ve GestureDetector.OnGestureListener uygulamasının özel bir sınıfta nasıl uygulanacağı konusunda bir örnek buldum. ListView. Kaydırma GestureDetektor uygulayan değilse i ihtiyaç yoludur çalışmıyor:

İşte benim Sorun olduğunu. ListView, Toolbar'ın daraltılmasından önce ilk önce aşağı kaydırılır, ancak başka bir şekilde olmasını istiyorum ... ilk önce Araç Çubuğu'nu daralt ve ListView'e ilerleyin, böylece Gesture Detektor'ı uygulayın. uygulandıktan sonra ListView kaydırılamadı. Kodumdaki sorun nedir?

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; 
    } 
} 

Onlar ViewPager bir parçası olarak şişirildiği düzeni:

İşte
<?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> 

özel NestedScrolling ListView: İşte

Düzeni olduğunu.

Peki neden uygulanan GestureListener jestlerimi işlemiyor?

Çok teşekkürler!

cevap

0

İlk cevabı buldum ama doğru yoldan emin değilim. 2 şey değiştiririm: Listener'ı özel ListView'ümde kaldırırım ve düzende, NestedScrollView'ı kaldırın. Çalışmak istediğim gibi, ama dinleyici neden çalışmıyordu?

İlgili konular