Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将项目拖离nestedscrollview中recyclerview的可见空间时-nestedscrollview不';卷轴_Android_Drag And Drop_Android Nestedscrollview - Fatal编程技术网

Android 将项目拖离nestedscrollview中recyclerview的可见空间时-nestedscrollview不';卷轴

Android 将项目拖离nestedscrollview中recyclerview的可见空间时-nestedscrollview不';卷轴,android,drag-and-drop,android-nestedscrollview,Android,Drag And Drop,Android Nestedscrollview,我有两个项目,一个是静态的,另一个是可回收的。这两种类型的视图都由LinearLayout包装。LinearLayout由NestedScrollView包装 XML解释: <NestScrollView> <LinearLayout> <View></View> <View></View> <RecyclerView></RecyclerView> </Line

我有两个项目,一个是静态的,另一个是可回收的。这两种类型的视图都由LinearLayout包装。LinearLayout由NestedScrollView包装

XML解释:

<NestScrollView>
  <LinearLayout>
    <View></View>
    <View></View>
    <RecyclerView></RecyclerView>
  </LinearLayout>
</NestScrollView>

图片说明:

<NestScrollView>
  <LinearLayout>
    <View></View>
    <View></View>
    <RecyclerView></RecyclerView>
  </LinearLayout>
</NestScrollView>

我从本教程中获取的拖动项目的实现:

问题是当我把他的孩子们拖出屏幕时,
RecyclerView
不会滚动。(但是,如果我没有
NestedScrollView
作为父级,
RecyclerView
可以正常工作。)


我搜索了与此相关的所有问题,但没有找到任何解决方案。

recyclerView
放入
NestedScrollView
中不是一个好做法。我建议您使用其他视图作为
RecyclerView
项。我相信这会解决您的问题,正如您所说的“但是,如果我没有NestedScrollView作为家长,RecyclerView将按预期工作。”

使用类似以下内容:

       <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

       <android.support.v4.widget.NestedScrollView
            android:id="@+id/view_noRecord"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:id="@+id/tv_no_record"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:layout_marginBottom="@dimen/_20sdp"
                android:gravity="center_horizontal"
                android:padding="@dimen/_10sdp"
                android:text="@string/no_data_found"
                android:visibility="gone"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </android.support.v4.widget.NestedScrollView>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/common_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fadeScrollbars="true"
            android:scrollbars="vertical"
            android:visibility="gone"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />



    </FrameLayout>

或者尝试将布局中的内容作为RecyclerView的标题

<LinearLayout>
    <View></View>
    <View></View>
  </LinearLayout>

通过向回收器添加
android:fillViewport=“true”
,使NestedScrollView和RecyclerView能够很好地协同工作

当您开始拖动项目或到达屏幕边缘时,您还可以使用
mRecyclerView.setNestedScrollingEnabled(false)
关闭回收者的嵌套行为。这将导致嵌套视图本身滚动,以便用户继续沿着recyclerview向下

完成后,再次将嵌套滚动设置为true

或者你需要考虑授权谁获得触摸事件,就像我在另一个问题中概述的那样。对于您的示例,它可能非常简单:

  • 当拖动开始时,调用nestedScrollView.RequestDisallowWinterCeptTouchEvent(true)
  • 当拖动结束时,请禁用WinterCeptTouchEvent(false)以重新启用scrollview

如果RequestDisallowWinterCeptTouchEvent无法工作,那么您可以创建NestedScrollView的子类,并覆盖InterceptTouchEvent上的
。我使用其他视图作为回收者视图。
首先,我们必须在getItemViewType()中返回两个视图

现在,我们将另一个视图添加为列表中的HeaderView,因此我们将调整列表计数:

@Override public int getItemCount() 
{ 
       return arrayList.size()+1;
}
现在,基于getItemViewType()在onBindViewHolder中正确绑定视图

现在在Item Touch Listener回调中,我们必须调整onMove()方法:

现在,如果所有逻辑都正确,那么我们移动该项并调用onreallyMoved()方法:


如果您想了解更多信息,请查看。

另一种解决方案是使用线性布局,而不是NestedScrollView。在其根目录下,将recyclerView与参数sethasFixedSize(true)放在Java文件中,并与xml中的match_parent height放在一起

概述:

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

    <RecyclerView
        ...
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

 </LinearLayout>


不要忘记设置recyclerView.setHasFixedSize(true);在您的Java文件中

好的,但我必须有scroll和recyclerview@deadfish向导是正确的,您不应该将滚动视图放在滚动视图中,就像您不将listView放在滚动视图中一样,这将导致出现问题并给出下面的答案,这不是一个好的做法。请尝试更改您的方法。您是否在recyclerView中添加了NestedScrollingEnabled=false?我建议您放弃nested scrollview,将文本视图放在父视图中,并将该视图作为标题添加到recyclerView中。下面是如何将标题添加到recyclerview()中的方法。您最终找到解决方案了吗?如果没有NestedScrollView,我的折叠工具栏将无法工作。是否有人尝试覆盖InterpoleoutofBoundsScroll?我可以将标题添加到recyclerview,但我不想将其视为列表的子项。整个版面必须是可滚动的。通过添加页眉,您可以使整个版面也可滚动,我最近也做过同样的事情。不幸的是,
fillViewport
setNestedScrollingEnabled
都没有解决这个问题。您尝试过拦截触摸事件吗?(见编辑)
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { 
// get the viewHolder's and target's positions in your adapter data, swap them
if(viewHolder.getItemViewType() != target.getItemViewType())
{
return false; 
}
int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();
if(dragFrom == -1) 
{ 
dragFrom =  fromPosition; 
}
dragTo = toPosition;
if(dragFrom != -1 && dragTo != -1 && dragFrom != dragTo) 
{
reallyMoved(dragFrom, dragTo);
dragFrom = dragTo = -1;
}
// and notify the adapter that its dataset has changed
adapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());                
return true;
}
private void reallyMoved(int dragFrom, int dragTo) 
{
if(dragFrom == 0 || dragTo == arrayList.size())
{
 return;
}
Collections.swap(arrayList, dragFrom-1, dragTo-1);
}
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RecyclerView
        ...
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

 </LinearLayout>