Actually, I am updating "RecylerView" list adapter based on filter data list. When I update adapter data updated successfully but Scrolling start from half of adapter list. Does anyone face problem like this before?
Things i have already tried - Put Recyclerview inside RelativeLayout - Set Recyclerview height to wrap_content - Use requestLayout method
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/spinnerLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/dp45"
android:background="@color/black_color"
android:orientation="horizontal"
android:paddingStart="@dimen/dp10"
android:paddingEnd="@dimen/dp10"
android:weightSum="2">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="start|center"
android:text="@string/statement_type"
android:textAllCaps="true"
android:textColor="@color/colorAccent"
android:textSize="@dimen/subtitle"
android:textStyle="bold"
app:fontFamily="@string/fontBold" />
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/filterItemSpinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/dp45"
android:layout_below="@+id/spinnerLayout"
android:background="@color/colorAccent"
android:orientation="horizontal"
android:padding="@dimen/dp10"
android:weightSum="3">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtdate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:gravity="start|center"
android:text="@string/lbl_date_particular"
android:textAllCaps="true"
android:textColor="@color/black_color"
android:textSize="@dimen/bodyText"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtdr_cr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:gravity="center"
android:text="@string/lbl_dr_cr"
android:textAllCaps="true"
android:textColor="@color/black_color"
android:textSize="@dimen/bodyText"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtbalance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|end"
android:text="@string/balance"
android:textAllCaps="true"
android:textColor="@color/black_color"
android:textSize="@dimen/bodyText"
android:textStyle="bold" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/contentLayout"
tools:listitem="@layout/item_account_statement" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtEmptyView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/no_acc_statement_available"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/sp18"
android:visibility="gone" />
<include
android:id="@+id/includeLoaderLine"
layout="@layout/custom_loaderview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</layout>
You can listen to adapter updates and scroll your RecyclerView
to starting position when changes are detected.
If you're using Java:
//Initializing your adapter
MyAdapter myAdapter = new MyAdapter(...);
//Listening to adapter updates
myAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
//Scrolling to starting position
recyclerView.getLayoutManager().scrollToPosition(0);
}
});
//Setting your adapter on your RecyclerView
myRecyclerView.setAdapter(myAdapter);
...
If you're using Kotlin:
//Initializing your adapter
var myAdapter = MyAdapter(...)
//Listening to adapter updates
myAdapter.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onChanged() {
//Scrolling to starting position
myRecyclerView.layoutManager.scrollToPosition(0)
}
})
//Setting your adapter on your RecyclerView
myRecyclerView.adapter = myAdapter
...
Hope it helps! =)