CoordinatorLayout内部的RecyclerLayout scrollToPosition可见性 [英] RecyclerLayout inside CoordinatorLayout scrollToPosition visibility

查看:129
本文介绍了CoordinatorLayout内部的RecyclerLayout scrollToPosition可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a using a CoordinatorLayout with a AppBarLayout inside and an element to try to be able to scroll when RecyclerView is being scrolled. Until that, everything is working as it is suposed to, the problems comes when I try programatically to scrollToPosition of the lasts positions of the RecyclerView. The recyclerViews does the scroll, but the position I tell to scroll to is not visible (in fact, if I scroll manually, it is visible once the other element inside the AppBarLayout has completely scrolled out of the screen). This is my code:

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

还有RecyclerView的代码:

And the code of the RecyclerView:

    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.setAdapter(adapter);

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

The adapter is a simple text adapter........

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

When I try to scroll is after receiving an event from a Service, so there is a BroadcastReceiver which receives the event and manage the result:

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

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

My idea is that I think the RecyclerView does't understands that position is not visible.....

推荐答案

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

Try this code, but this will collapse appbarlayout:

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

这篇关于CoordinatorLayout内部的RecyclerLayout scrollToPosition可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆