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>
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........
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);
}
My idea is that I think the RecyclerView does't understands that position is not visible.....
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With