底部弹出窗口中的RecyclerView无法工作

11

以下是我的XML文件

MainView

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Your content -->
    <include layout="@layout/content_main"/>

    <!-- Bottom Sheet -->
    <include layout="@layout/main_bottom_sheet"/>

</android.support.design.widget.CoordinatorLayout>

底部弹出视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_navigation_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical"
    app:behavior_peekHeight="56dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <FrameLayout
        android:id="@+id/fragment_bottom_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

可重复使用视图(RecycleView)屏幕

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/white"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        android:background="@color/alabaster"
        android:elevation="@dimen/elevation_normal"
        android:orientation="horizontal"
        tools:targetApi="lollipop">

        <FrameLayout
            android:id="@+id/search_as_you_type_back_button"
            android:layout_width="@dimen/toolbar_height"
            android:layout_height="@dimen/toolbar_height"
            android:foreground="?selectableItemBackground">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@string/accessibility_back"
                app:srcCompat="@drawable/ic_arrow_back"/>

        </FrameLayout>

        <EditText
            android:id="@+id/search_as_you_type_edit_text"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginStart="@dimen/margin_medium"
            android:layout_weight="1"
            android:background="@color/transparent"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center_vertical"
            android:imeOptions="actionSearch"
            android:inputType="text"
            android:textColor="@color/darkest_grey"
            android:textSize="@dimen/text_medium"
            tools:text="Chocolate"/>

        <FrameLayout
            android:id="@+id/search_as_you_type_close_container"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:foreground="?selectableItemBackground"
            android:visibility="invisible">

            <ImageView
                android:id="@+id/search_as_you_type_close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@string/accessibility_close"
                app:srcCompat="@drawable/ic_cancel_grey_24dp"/>

        </FrameLayout>

    </LinearLayout>

    <ViewFlipper
        android:id="@+id/view_flipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inAnimation="@android:anim/fade_in"
        android:outAnimation="@android:anim/fade_out">


      <android.support.v7.widget.RecyclerView
          android:id="@+id/auto_suggest_list"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
          android:nestedScrollingEnabled="true"/>


    </ViewFlipper>

</LinearLayout>

底部片段容器布局将充气具有RecyclerView屏幕的片段。这里的RecyclerView在填充数据后不会滚动。当键盘弹出时,它也不会得到调整。我尝试设置windowSoftInputMode。有人能帮助解决这个问题吗。


你找到解决方案了吗? - A.s.ALI
我们已经解决了这个问题 - 你还遇到这个问题吗? - dazza5000
请参考这个答案,可能会对您有所帮助:https://dev59.com/BLPma4cB1Zd3GeqPurI-#60696081 - Asad Mukhtar
5个回答

15

这是在底部工作的 RecyclerView 的可用代码。

XML:

<layout 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">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewMore"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:focusableInTouchMode="true"
                android:orientation="vertical"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
          
        </RelativeLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

在Java中,在onCreateView方法中

B.recyclerViewMore.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.getParent().requestDisallowInterceptTouchEvent(true);
        v.onTouchEvent(event);
        return true;
    }
});

2
工作得很好,它应该被接受为答案。 - GianhTran

14

lincy的解决方案对我不起作用。事件从未传递到OnTouchListener

我的解决方案是使用自定义布局包装RecyclerView并钩入dispatchTouchEvent

<info.mschmitt.view.TouchEventInterceptorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</info.mschmitt.view.TouchEventInterceptorLayout>

TouchEventInterceptorLayout 的代码(已删除构造函数):

public class TouchEventInterceptorLayout extends FrameLayout {
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean handled = super.dispatchTouchEvent(ev);
        requestDisallowInterceptTouchEvent(true);
        return handled;
    }
}

这将递归地禁用当前触摸交互的所有父级触摸事件处理,仅留下 RecyclerView 作为处理目标。

为什么需要这样做我不知道。也许这是 Google 的 BottomSheet 实现中的一个错误。


太棒了,非常感谢! - Eric B.
我的应用程序总是会出现这个错误:“Caused by: android.view.InflateException: Binary XML file line #10 in com.imincode.earthlings:layout/generic_item_list: Error inflating class com.imincode.earthlings.helpers.TouchEventInterceptorLayout Caused by: java.lang.NoSuchMethodException: com.imincode.earthlings.helpers.TouchEventInterceptorLayout.<init> [class android.content.Context, interface android.util.AttributeSet]” - imin
1
没事了,我已经解决了。需要使用构造函数 public TouchEventInterceptorLayout(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); } - imin
TouchEventInterceptorLayout类 @JvmOverloads构造函数( context: Context, attrs: AttributeSet? = null ) : FrameLayout(context, attrs) - Jayamurugan

1
你需要在用户滚动列表时,阻止触摸事件传递给父级(BottomSheet)。尝试使用这段代码。
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
            rv.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }

        @Override
        public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    });

这个解决方案对我有效。谢谢 @hemdroid。 - undefined

0
rv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    v.getParent().requestDisallowInterceptTouchEvent(true);
    v.onTouchEvent(event);
    return true;
}
});

0
这个是对@Matt解决方案的优化,因为它在我的情况下无法工作。
class TouchEventInterceptorLayout(context: Context, attrs: AttributeSet?) :
FrameLayout(context, attrs) {
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
    val handled = super.dispatchTouchEvent(ev)
    requestDisallowInterceptTouchEvent(true)
    return handled
}}

将RecyclerView包装成这样
<com.demo.view.TouchEventInterceptorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</com.demo.view.TouchEventInterceptorLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接