代码之家  ›  专栏  ›  技术社区  ›  Michael

RecyclerView未在NavigationView中滚动

  •  -1
  • Michael  · 技术社区  · 6 年前

    我在抽屉布局导航视图(DrawerLayoutNavigationView)中有两个recyclerviews,该视图从右侧滑入。在顶部水平滚动的那一个可以工作,尽管你必须先按住其他地方,这样东西才不会认为你想把视图滑回去。不过,更大的问题是,邮件的第二个回收器视图不允许您滚动。我尝试删除所有额外的属性,如clickable和transcriptmode,并尝试将回收器视图移到XML的底部,但都没有成功。以下是我的主页XML:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <RelativeLayout
            android:id="@+id/activity_user_marker_details"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.test.test.User_Marker_Details"
            android:layout_centerVertical="false"
            android:orientation="vertical">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/userMarkerToolbar"
                style="@style/HeaderBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#279d43"
                android:elevation="4dp"
                android:minHeight="?attr/actionBarSize"
                android:transitionName="actionBar"
                app:popupTheme="@style/ActionBarPopupThemeOverlay"
                app:theme="@style/ActionBarThemeOverlay"
                app:title="Go Back"
                app:titleTextColor="@color/md_white_1000"
                tools:ignore="UnusedAttribute" />
    
            <RelativeLayout
                android:id="@+id/topRL"
                android:layout_below="@id/userMarkerToolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:text="00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:id="@+id/textViewUserMarkerDetCount"
                    android:textSize="60sp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    tools:ignore="HardcodedText" />
    
                <LinearLayout
                    android:id="@+id/peopleInHerdLinearLayout"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@+id/textViewUserMarkerDetCount"
                    android:layout_toEndOf="@+id/textViewUserMarkerDetCount"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp">
    
                    <TextView
                        android:text="people"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView2"
                        android:textSize="24sp"
                        tools:ignore="HardcodedText" />
    
                    <TextView
                        android:text="in the herd"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView4"
                        android:textSize="18sp"
                        tools:ignore="HardcodedText" />
    
                </LinearLayout>
    
                <TextView
                    android:layout_toRightOf="@id/peopleInHerdLinearLayout"
                    android:text="Outdoor Activity"
                    android:lines="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="viewEnd"
                    android:id="@+id/categoryTextView"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginEnd="5dp"
                    android:gravity="end"
                    android:layout_toEndOf="@id/peopleInHerdLinearLayout"
                    tools:ignore="HardcodedText" />
    
                <ImageView
                    android:id="@+id/notifyHerdButtonBackground"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_below="@id/categoryTextView"
                    android:layout_alignParentRight="true"
                    android:layout_margin="5dp"
                    android:background="@drawable/circle_background_green"
                    android:layout_alignParentEnd="true"
                    tools:ignore="ContentDescription" />
    
                <Button
                    android:id="@+id/notifyHerdButton"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:background="@drawable/cow"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_alignTop="@id/notifyHerdButtonBackground"
                    android:layout_alignLeft="@id/notifyHerdButtonBackground"
                    android:layout_alignStart="@id/notifyHerdButtonBackground"
                    android:layout_marginStart="10dp" />
    
            </RelativeLayout>
    
            <TextView
                android:layout_below="@id/topRL"
                android:text="people who joined the herd..."
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView7"
                android:textAlignment="center"
                android:textStyle="normal|italic"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                tools:ignore="HardcodedText" />
    
            <ListView
                android:layout_below="@id/textView7"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/myUserCheckedInListView" />
    
            <ImageButton
                android:id="@+id/herdchatButton"
                android:layout_width="40dp"
                android:layout_height="50dp"
                android:background="@drawable/herdchat_button"
                android:paddingTop="5dp"
                android:src="@drawable/ic_action_chat"
                android:scaleType="fitCenter"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_below="@id/topRL"
                android:layout_marginTop="50dp"
                android:paddingRight="5dp"
                android:paddingLeft="10dp"
                android:paddingStart="10dp"
                android:paddingEnd="5dp"
                tools:ignore="ContentDescription" />
    
        </RelativeLayout>
    
    
        <android.support.design.widget.NavigationView
            android:id="@+id/herdchatSlideInView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="0dp"
            android:layout_gravity="end"
            android:fitsSystemWindows="true"
            android:layout_marginStart="0dp">
    
            <include layout="@layout/activity_herdchat"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </android.support.design.widget.NavigationView>
    </android.support.v4.widget.DrawerLayout>
    

    包含在上面的XML中,其中包含recyclerview:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_user__subscriptions"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.test.test.HerdchatActivity"
        android:background="@color/white"
        android:orientation="vertical">
    
        <android.support.v7.widget.RecyclerView
            android:layout_marginTop="30dp"
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:id="@+id/herdchatHerdsRecyclerView"
            android:orientation="horizontal"
            tools:listitem="@layout/herdchat_herds"/>
    
        <View
            android:id="@+id/separatorView"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/logoGreen"
            android:layout_below="@id/herdchatHerdsRecyclerView"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"/>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/messagesRecylcerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/separatorView"
            android:transcriptMode="alwaysScroll"
            android:layout_above="@+id/divider"
            android:divider="@null"
            android:clickable="false"
            android:dividerHeight="0dp"
            android:stackFromBottom="true"
            tools:listitem="@layout/chat_message_left"/>
    
        <RelativeLayout
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/logoGreen"
            android:layout_above="@+id/relSendMessage" />
    
        <RelativeLayout
            android:id="@+id/relSendMessage"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:background="@color/white"
            android:paddingLeft="10dp"
            android:paddingStart="10dp"
            android:paddingRight="0dp"
            android:paddingEnd="0dp"
            android:layout_alignParentBottom="true">
    
    
            <EditText
                android:layout_marginBottom="-4dp"
                android:id="@+id/messageBodyField"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="14sp"
                android:textColor="@color/logoGreen"
                android:layout_marginRight="10dp"
                android:layout_marginEnd="10dp"
                android:layout_alignTop="@+id/btnSend"
                android:background="@color/white"
                android:layout_alignBottom="@+id/btnSend"
                android:layout_toLeftOf="@+id/btnSend"
                android:layout_toStartOf="@+id/btnSend"
                android:inputType="text"
                tools:ignore="LabelFor" />
    
            <Button
                android:id="@+id/btnSend"
                android:layout_width="72dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_margin="4dp"
                android:fontFamily="sans-serif-condensed"
                android:textStyle="bold"
                android:textSize="16sp"
                android:text="Send"
                android:textColor="@color/white"
                android:background="@color/logoGreen"
                tools:ignore="HardcodedText" />
    
        </RelativeLayout>
    </RelativeLayout>
    

    更改为下面的选项可以滚动,但现在以非常慢以外的任何速度向上滚动时,如果有人知道解决方法,例如在触摸recyclerview时禁用滑入功能,则会关闭navigationview:

    <android.support.v4.widget.NestedScrollView
            android:layout_marginTop="30dp"
            android:id="@+id/herdchatHerdsNestedSV"
            android:layout_width="match_parent"
            android:layout_height="75dp">
    
            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/herdchatHerdsRecyclerView"
                android:nestedScrollingEnabled="true"
                android:orientation="horizontal"
                tools:listitem="@layout/herdchat_herds"
                tools:ignore="UnusedAttribute" />
        </android.support.v4.widget.NestedScrollView>
    
        <View
            android:id="@+id/separatorView"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/logoGreen"
            android:layout_below="@id/herdchatHerdsNestedSV"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"/>
    
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/messagesNestedSV"
            android:layout_width="match_parent"
            android:layout_below="@id/separatorView"
            android:layout_above="@+id/divider"
            android:layout_height="match_parent">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/messagesRecylcerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:transcriptMode="alwaysScroll"
                android:nestedScrollingEnabled="true"
                android:divider="@null"
                android:clickable="false"
                android:dividerHeight="0dp"
                android:stackFromBottom="true"
                tools:listitem="@layout/chat_message_left"
                tools:ignore="UnusedAttribute" />
        </android.support.v4.widget.NestedScrollView>
    

    此外,alwaysScroll不再工作了,所以我正在尝试这个,它是半工作的,我想我只需要再玩一些,除非有人知道更好的方法:

        mMessagesNestedSV.fullScroll(View.FOCUS_DOWN);
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Rizwan Atta    6 年前

    为了在单个视图中拥有更多的滚动条,您必须将嵌套滚动条作为根元素,或者至少是所有滚动条的父元素。

        <?xml version="1.0" encoding="utf-8"?>
    <NestedScrollView     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"
    >
        <RelativeLayout 
            android:id="@+id/activity_user__subscriptions"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.test.test.HerdchatActivity"
            android:background="@color/white"
            android:orientation="vertical">
    
            <android.support.v7.widget.RecyclerView
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:id="@+id/herdchatHerdsRecyclerView"
                android:orientation="horizontal"
                tools:listitem="@layout/herdchat_herds"/>
    
            <View
                android:id="@+id/separatorView"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/logoGreen"
                android:layout_below="@id/herdchatHerdsRecyclerView"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="0dp"
                android:layout_marginRight="0dp"/>
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/messagesRecylcerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/separatorView"
                android:transcriptMode="alwaysScroll"
                android:layout_above="@+id/divider"
                android:divider="@null"
                android:clickable="false"
                android:dividerHeight="0dp"
                android:stackFromBottom="true"
                tools:listitem="@layout/chat_message_left"/>
    
            <RelativeLayout
                android:id="@+id/divider"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/logoGreen"
                android:layout_above="@+id/relSendMessage" />
    
            <RelativeLayout
                android:id="@+id/relSendMessage"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:background="@color/white"
                android:paddingLeft="10dp"
                android:paddingStart="10dp"
                android:paddingRight="0dp"
                android:paddingEnd="0dp"
                android:layout_alignParentBottom="true">
    
    
                <EditText
                    android:layout_marginBottom="-4dp"
                    android:id="@+id/messageBodyField"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="14sp"
                    android:textColor="@color/logoGreen"
                    android:layout_marginRight="10dp"
                    android:layout_marginEnd="10dp"
                    android:layout_alignTop="@+id/btnSend"
                    android:background="@color/white"
                    android:layout_alignBottom="@+id/btnSend"
                    android:layout_toLeftOf="@+id/btnSend"
                    android:layout_toStartOf="@+id/btnSend"
                    android:inputType="text"
                    tools:ignore="LabelFor" />
    
                <Button
                    android:id="@+id/btnSend"
                    android:layout_width="72dp"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:layout_margin="4dp"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:textSize="16sp"
                    android:text="Send"
                    android:textColor="@color/white"
                    android:background="@color/logoGreen"
                    tools:ignore="HardcodedText" />
    
            </RelativeLayout>
        </RelativeLayout>
    
    </NestedScrollView?