CoordinatorLayout内部的RecyclerLayout scrollToPosition可见性

zap*_*tec 3 android android-layout

我正在使用内部带有AppBarLayoutCoordinatorLayout和一个元素来尝试在滚动RecyclerView时能够滚动。在此之前,一切都按预期进行,当我以编程方式尝试滚动RecyclerView的最后位置时,问题就来了。该recyclerViews不滚动,但是位置我告诉滚动到是不可见的(事实上,如果我滚动手动,它是可见的,一旦里面的其他元素AppBarLayout屏幕已经完全滚出)。 这是我的代码:

 <android.support.design.widget.CoordinatorLayout 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="match_parent">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:elevation="0dp">

                <com.github.mikephil.charting.charts.BarChart
                    android:id="@+id/chart"
                    android:layout_width="match_parent"
                    android:layout_height="280dp"
                    android:layout_below="@+id/toolbar_conso"
                    android:background="@color/background_dark_grey"
                    app:layout_scrollFlags="scroll|enterAlways" />

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <LinearLayout
                    android:id="@+id/linear_total_conso"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/current_consumption"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=""
                        android:textColor="@color/graph_consumption_color"
                        android:textSize="30sp" />

                    <TextView
                        android:id="@+id/consumption_unit"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_marginStart="5dp"
                        android:text=""
                        android:textAppearance="@style/TextAppearance.Smartlink.Large"
                        android:textColor="@color/graph_consumption_color" />
                </LinearLayout>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal" />
            </LinearLayout>
        </android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

以及RecyclerView的代码:

    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

该适配器是一个简单的文本适配器........

当我尝试滚动是从Service接收到事件后,因此有一个BroadcastReceiver可以接收事件并管理结果:

    @Override
    public void onReceive(Context context, Intent intent) {
            int position = intent.getExtras().getInt(POSITION);
            recyclerView.scrollToPosition(position);
    }
Run Code Online (Sandbox Code Playgroud)

我的想法是,我认为RecyclerView无法理解该位置不可见.....

Dhr*_*uvi 5

尝试以下代码,但这将折叠appbarlayout:

  @Override public void onReceive(Context context, Intent intent) { 
      int position = intent.getExtras().getInt(POSITION);
      app_bar_layout.setExpanded(false);
      recyclerView.scrollToPosition(position);
  }
Run Code Online (Sandbox Code Playgroud)