2017-08-17 97 views
0

我想要实现一个包含垂直ScrollView有几个水平RecyceViews为孩子们的活动!嵌套水平RecycleView中断

但有时我面对奇怪的触摸从内RecycleViews中断,当我尝试在垂直方向滚动并我的触摸的起始点为(的RecycleView)项的内部的ScrollView没有按卷动不能正常工作,它不会在Y方向上滚动!

我尝试实施我自己的ScrollViewRecycleView要处理触摸操作,但问题仍然存在!

,你可以看到底发生了什么在这个视频: video of my applications problem

MyScrollView:

public class MyScrollView extends ScrollView { 
    private GestureDetector mGestureDetector; 

    public MyScrollView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mGestureDetector = new GestureDetector(context, new YScrollDetector()); 
     setFadingEdgeLength(0); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); 
    } 

    @Override 
    public boolean canScrollVertically(int direction) { 
     return true; 
    } 


    @Override 
    public boolean canScrollHorizontally(int direction) { 
     return false; 
    } 

    // Return false if we're scrolling in the x direction 
    class YScrollDetector extends GestureDetector.SimpleOnGestureListener { 
     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
      boolean result=Math.abs(distanceY) > Math.abs(distanceX); 
      Log.d("ADAPTER MYSCR G = ",result+""); 
      return result; 
     } 
    } 

} 

MyRecycleView:

public class MyRecycleView extends RecyclerView { 
    private GestureDetector mGestureDetector; 

    public MyRecycleView(Context context) { 
     super(context); 
     mGestureDetector = new GestureDetector(context, new MyRecycleView.XScrollDetector()); 
    } 

    public MyRecycleView(Context context, @Nullable AttributeSet attrs) { 
     super(context, attrs); 
     mGestureDetector = new GestureDetector(context, new MyRecycleView.XScrollDetector()); 
    } 

    public MyRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     mGestureDetector = new GestureDetector(context, new MyRecycleView.XScrollDetector()); 
    } 


    @Override 
    public boolean onInterceptTouchEvent(MotionEvent e) { 
     boolean result=super.onInterceptTouchEvent(e) && mGestureDetector.onTouchEvent(e); 
     if(!result) { 
      Log.d("ADAPTER MYREC G = ",result+""); 
      View parent=((View) getParent()); 

      while (parent!=null){ 
       if(parent.getClass().getName().equals("class com.android.internal.policy.DecorView")) 
        break; 
       if(parent instanceof MyScrollView) { 
        Log.d("MY SCROLL VIEW","FOUND AS PARENT!"); 
        ((MyScrollView) parent).requestDisallowInterceptTouchEvent(false); 
        break; 
       } 
       parent= (View) parent.getParent(); 
      } 

      this.stopScroll(); 
     } 
     return result; 
    } 

    @Override 
    public boolean canScrollHorizontally(int direction) { 
     return true; 
    } 


    @Override 
    public boolean canScrollVertically(int direction) { 
     return false; 
    } 

    // Return false if we're scrolling in the y direction 
    class XScrollDetector extends GestureDetector.SimpleOnGestureListener { 
     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
      boolean result=Math.abs(distanceY) < Math.abs(distanceX); 
      return result; 
     } 
    } 
} 

,这里是我的RecycleView项目适配器XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/lib/io.github.makbn.graphics" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_margin="2dp" 
    android:background="@android:color/transparent" 
    android:gravity="center" 
    android:orientation="vertical"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/graphics_card_container" 
     android:layout_width="115dp" 
     android:layout_height="180dp" 
     android:layout_marginBottom="3dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="1dp" 
     android:foreground="?android:attr/selectableItemBackground" 
     app:cardBackgroundColor="#fff" 
     android:clickable="true" 
     android:focusableInTouchMode="false" 
     android:filterTouchesWhenObscured="true" 
     android:focusable="false" 
     app:cardElevation="2dp" 
     app:cardPreventCornerOverlap="true"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:orientation="vertical"> 
      <RelativeLayout 
       android:id="@+id/layout_foodview_container" 
       android:layout_width="match_parent" 

       android:layout_height="wrap_content"> 

       <ImageView 
        android:id="@+id/graphics_img_icon" 
        android:layout_width="match_parent" 
        android:layout_height="80dp" 
        android:layout_gravity="center" 
        android:background="#00000000" 
        android:scaleType="centerCrop" /> 

       <RatingBar 
        style="@style/MyRatingBar_Style" 
        android:id="@+id/rating" 
        android:layout_width="match_parent" 
        android:layout_height="20dp" 
        android:layout_alignBottom="@+id/graphics_img_icon" 
        android:layout_centerInParent="true" 
        android:layout_gravity="center_horizontal" 
        android:isIndicator="true" 
        android:background="@color/white_overlay" 
        android:numStars="5" /> 
      </RelativeLayout> 

      <TextView 
       android:id="@+id/txt_name" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/img_logo" 
       android:layout_gravity="center" 
       android:layout_margin="3dp" 
       android:ellipsize="end" 
       android:gravity="center" 
       android:lines="2" 
       android:maxLines="2" 
       android:padding="2dp" 
       android:text="نام محصول" 
       android:textSize="12sp" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1"></LinearLayout> 

      <TextView 
       android:id="@+id/txt_price" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       android:background="@color/colorAccent" 
       android:gravity="center_horizontal" 
       android:padding="3dp" 
       android:text="۷۵۰۰ ت" 
       android:textColor="#fff" 
       android:textSize="12sp" /> 
     </LinearLayout> 
    </android.support.v7.widget.CardView> 
</LinearLayout> 

而在我的activty中,我找到了我的MyScrollView并创建了几个MyRecycleView的实例并添加到它!

+0

而不是使用scrollview ...为什么你不能使用recyclerview本身 – Jeevanandhan

+0

@Jeevanandhan,因为我的主要活动是动态的,不同的视图可以添加到服务器处理的滚动视图,这样做与recycleview是如此复杂和艰难 –

+0

::我认为recycleview不像scrollview那样复杂,因为在recyclerview中我们可以在适配器中使用getItemViewType方法。一些如何在scrollview中动态加载数据,你可能会做一些验证来填充数据。在recyclerview适配器中的getItemViewType方法中执行此操作,并创建独立的视图持有者,这样您也可以实现代码的可重用性。例如我会分享你一个链接,我不会给你确切的解决方案,但可能会帮助你的方法:) https://stackoverflow.com/a/44470106/1501864 – Jeevanandhan

回答

0

如果我已经正确理解你的问题,那么你不需要做这一切。 使用NestedScrollView而不是滚动视图

建议: 我观看了您的视频,您还可以使用嵌套的recyclerview使您的UI完全动态。 RecyclerView的ViewHolders功能非常强大。您可以在Parent recyclerview的Item中添加Horizo​​ntal RecyclerView,然后使用父适配器的onBind方法初始化内部recyclerViews。

+0

NestedRecycleview ??你是什么意思? –

+0

使用NestedScrollView而不是ScrollView并没有帮助! –

+0

你能分享更新的代码吗?你有没有移动手势检测器? –

相关问题