Android-滚动浏览和水平回收视图之间的滑动冲突

时间:2018-04-19 07:07:55

标签: android scroll android-recyclerview scrollview conflict

我遇到了scrollview和水平回收视图之间的滚动问题。在Fragment中,有一个包含2个水平回收器视图的scrollview。有时想要滑动回收器视图,但系统也无法检测到,它会触发滚动视图向上或向下滑动。滚动回收站视图对我来说非常困难。 图片下方

enter image description here

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                    let cell = tableView.dequeueReusableCell(withIdentifier: "DepartmentCell", for: indexPath) as! ShowDepttTVCell
        cell.btnView.tag =  indexPath.row
        cell.btnEdit.tag =  indexPath.row
                    //Set action to buttons
                    cell.btnView.addTarget(self, action: #selector(btnView(sender:)), for: .touchUpInside)
                    cell.btnEdit.addTarget(self, action: #selector(btnEdit(sender:)), for: .touchUpInside)

        return cell
   }

@objc func btnView(sender: UIButton)
{
        let index = sender.tag 
        let indexPath = IndexPath(row: index, section: 0)
        let viewDepttVC = self.storyboard?.instantiateViewController(withIdentifier: "Detailed Department") as! DetailedDepttVC
                    self.navigationController?.pushViewController(viewDepttVC, animated: true)
                }

@objc func btnEdit(sender: UIButton)
{
        let index = sender.tag 
        let indexPath = IndexPath(row: index, section: 0)
        //I want to get the indexpath of this cell button.
                    print("Selected iNdex  :  \(r)")
                    print("Edit")
                    let editDeptt = self.storyboard?.instantiateViewController(withIdentifier: "Add Department") as! AddDepartmentVC
                    editDeptt.btnTag = 0
                    //editDeptt.strDName =
                    self.navigationController?.pushViewController(editDeptt, animated: true)

}

2 个答案:

答案 0 :(得分:1)

使用 android.support.v4.widget.NestedScrollView 代替 ScrollView

使用 android:nestedScrollingEnabled="false" 加入 RecyclerView

像这样更改你的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:fitsSystemWindows="true"
    android:descendantFocusability="beforeDescendants">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <ImageView
            android:layout_width="125dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:layout_marginTop="5dp"
            android:src="@drawable/logo" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@color/gray" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="20dp"
            android:layout_weight="1"
            android:fontFamily="@font/roboto_condensed_regular"
            android:text="Journals"
            android:textColor="@color/v2_text_color"
            android:textSize="18sp"
            android:textStyle="bold" />

        <View
            android:layout_width="25dp"
            android:layout_height="1dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="5dp"
            android:background="@color/v2_text_color" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:descendantFocusability="afterDescendants"
            android:nestedScrollingEnabled="false"
            android:scrollbars="horizontal" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="15dp"
            android:fontFamily="@font/roboto_condensed_regular"
            android:text="Editor's Pick"
            android:textColor="@color/v2_text_color"
            android:textSize="18sp"
            android:textStyle="bold" />

        <View
            android:layout_width="25dp"
            android:layout_height="1dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="5dp"
            android:background="@color/v2_text_color" />


        <android.support.v7.widget.RecyclerView
            android:id="@+id/editorRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:descendantFocusability="afterDescendants"
            android:nestedScrollingEnabled="false"
            android:scrollbars="horizontal" />


        <Button
            android:id="@+id/tv_showMore"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="110dp"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="30dp"
            android:background="@drawable/white_rounded_button"
            android:text="Show All"
            android:textAllCaps="false"
            android:textColor="#696969" />


    </LinearLayout>


</android.support.v4.widget.NestedScrollView>

答案 1 :(得分:0)

在scrollview / nestedscrollview中使用Recyclerview时会出现许多问题。视图不会被回收,并会影响应用程序的整体性能(如果您有大量数据) 如果您尝试实现多个水平滑动并且能够垂直滑动布局,最佳做法是使用“嵌套的Recyclerview”

基本上它的作用是“我们将有一个垂直的Recyclerview,垂直Recyclerview的每个项目将是一个水平的Recyclerview” 您可以参考此project

我遵循这种模式并在多个项目中使用它

希望有所帮助