以编程方式滚动Recycler View不会更新CoordinatorLayout.Behaviour,同时通过触摸更新CoordinatorLayout.Behaviour滚动它

时间:2017-04-26 05:54:57

标签: android android-recyclerview android-coordinatorlayout

我提到了This Question,但没有答案适合我。所以基本上我有一个BottomNavigationView隐藏在向下滚动并在向上滚动时显示。当我通过触摸滚动Recycler View但在以编程方式滚动RecyclerView时不起作用时,它工作正常。似乎它在以编程方式滚动时不会触发CoordinatorLayout.Behaviour。在某种情况下,我必须打开包含RecyclerView的片段并滚动到某个位置,并且BottomNavigationView隐藏它必须在用户向上滚动时返回这意味着我不想隐藏其可见性BottomNavigationView只需通过编程或触摸滚动隐藏滚动。

  

activity_main.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"
    tools:context="com.kliff.digitaldwarka.activity.MainActivity">
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:id="@+id/myAppBar"
            app:elevation="0dp"
            android:theme="@style/AppTheme.AppBarOverlay">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:contentInsetStart="0dp"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>
        </android.support.design.widget.AppBarLayout>
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.design.widget.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemBackground="@android:color/white"
            android:id="@+id/bottom_nav"
            android:layout_gravity="bottom"
            app:menu="@menu/bottom_nav_menu"/>
    </android.support.design.widget.CoordinatorLayout>
    <com.appeaser.sublimenavigationviewlibrary.SublimeNavigationView
    android:id="@+id/nav_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_gravity="start"
    app:snvHeaderLayout="@layout/nav_header"
    app:snvMenu="@menu/nav_menu"
    style="@style/NavigationViewStyle"
    app:snvItemTextColor="#555555"
    app:snvSubheaderItemTextColor="#555555"
    app:snvItemIconTint="#555555"
    app:snvBadgeTextColor="#555555"
    app:snvHintTextColor="#757575"
    app:snvHintTypefaceStyle="normal"
    app:snvBadgeTypefaceStyle="normal"
    app:snvSubheaderItemTypefaceStyle="normal"
    app:snvItemTypefaceStyle="normal" />
</android.support.v4.widget.DrawerLayout>
  

fragment_category.xml

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="1dp"
    app:cardUseCompatPadding="true">
    <android.support.v7.widget.RecyclerView
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />
</android.support.v7.widget.CardView>
  

滚动代码

 mRecyclerView.getLayoutManager().scrollToPosition(34); //specific postion

通过这种方式滚动我怎样才能实现这种行为。感谢任何帮助。

4 个答案:

答案 0 :(得分:2)

CoordinatorLayout.Behaviour仅适用于NestedScroll事件。当您尝试以编程方式滚动RecyclerView时,将其视为普通滚动。

在下面的行中编写以通知RecyclerView启动NesteadScroll,并使用 ViewCompat.SCROLL_AXIS_VERTICAL ViewCompat.TYPE_NON_TOUCH

ViewCompat.SCROLL_AXIS_VERTICAL::指示沿垂直轴滚动。 ViewCompat.TYPE_NON_TOUCH::指示手势的输入类型是由用户非触摸屏幕引起的。这通常是由于扑杀而来。

recycler_view.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, ViewCompat.TYPE_NON_TOUCH)
recycler_view.smoothScrollBy(0,200)

答案 1 :(得分:1)

我使用此代码请检查它是否适合您: -

activity_mail.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.example.constraintlayout.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_scrolling" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

content_scrolling.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.constraintlayout.ScrollingActivity"
    tools:showIn="@layout/activity_scrolling">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/recycler_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:scrollbars="vertical" />

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

答案 2 :(得分:0)

尝试以下代码。它为我工作。我使用的是BottomBar Library

 declare @t table(col1 int,col2 varchar(50),col3 int)
insert into @t (col1,col2) VALUES (0,'Zero'),(1,'One'),(2,'Two')
,(3,'Three'),(4,'Four'),(5,'Five'),(6,'Six'),(7,'Seven'),(8,'Eight')
,(9,'Nine'),(10,'Ten'),(11,'eleven'),(12,'Twelve'),(13,'thirteen')
,(14,'Fourteen'),(15,'Fifteen'),(16,'Sixteen'),(17,'Seventeen')
,(18,'Eighteen'),(19,'Nineteen'),(20,'Twenty'),(30,'Thirty'),(40,'Forty')
,(50,'Fifty'),(60,'Sixty'),(70,'Seventy'),(80,'Eighty'),(90,'Ninety')
,(100,'Hundreds'),(1000,'Thousands'),(10000,'Thousands')

    declare @input varchar(10)='1897'

    declare @Words varchar(2000)=''
    declare @i int=len(@input)

    while((len(@input)>0))
    begin

    if(@input>=0 and @input<=19 and @i=len(@input))
    begin
    select @Words=@Words+' '+ col2 from @t where col1 =@input
    BREAK
    END
    else if(len(@input) between 3 and 4)
    BEGIN
    select @Words=@Words+' '+ col2 from @t where col1 =@input/cast(('1'+replicate('0',len(@input)-1)) as int)
    select @Words=@Words+' '+ col2 from @t where col1 ='1'+replicate('0',len(@input)-1)
    END
    else
    begin
    select @Words=@Words+' '+ col2 from @t where col1 =left(@input,1)+ replicate('0',@i-1)

    END
    set @input=stuff(@input,1,1,'')
    if(cast(@input as int)=0)
    BREAK
    set @i=@i-1


    end
    select @Words

答案 3 :(得分:0)

对我而言,什么都没有起作用,直到我将Bhavesh Jabuvanis的答案包装在帖子中

mRecyclerView?.post  {
  mRecyclerView?.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, ViewCompat.TYPE_NON_TOUCH)
  mRecyclerView?.smoothScrollToPosition(scrollToPosition)
}