android - How to save scroll position of recyclerview in fragment -
i have followed many answers here.. not single 1 solving issue .. that's why asking.
i want save scroll position in fragment. in many articles have suggested follow
@override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); }
and
@override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); }
but 2 method not available in fragment.
my code:
private int mpositioning = recyclerview.no_position; private string key_position_ing = "keypositioning";
in oncreateview()
if (savedinstancestate != null) { if (savedinstancestate.containskey(key_position_ing)) { mpositioning = savedinstancestate.getint(key_position_ing); } }
override methods in fragment not same method above. don't know doing wrong.
@override public void onsaveinstancestate(bundle outstate) { int scrollpositioning = mrecyclerviewingredients.computeverticalscrolloffset(); mpositioning = scrollpositioning; outstate.putint(key_position_ing, mpositioning); super.onsaveinstancestate(outstate); } @override public void onviewstaterestored(@nullable bundle savedinstancestate) { if (mpositioning != recyclerview.no_position) { mrecyclerviewingredients.getlayoutmanager().scrolltoposition(mpositioning); } super.onviewstaterestored(savedinstancestate); }
i need save scroll position while orientation changes .. please help. suggestion full. thanks......
update
everything wrote below correct, reason didn't work didn't realize how activity
's layout structured. here activity
's layout (slightly cleaned up):
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.tapan.recipemaster.activity.recipedetailactivity"> <scrollview android:layout_width="match_parent" android:layout_height="match_parent"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <framelayout android:id="@+id/fl_fragment_detail" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/padding_10dp"/> </relativelayout> </scrollview> </android.support.constraint.constraintlayout>
meanwhile, fragment
's layout (again, cleaned up):
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.tapan.recipemaster.fragment.recipedetailfragment"> <scrollview android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="@dimen/padding_10dp" android:orientation="vertical"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/coloraccent" android:orientation="vertical"> <textview android:id="@+id/tv_ingredient" android:layout_width="match_parent" android:layout_height="wrap_content" android:textcolor="#fff" android:textsize="@dimen/text_23sp" android:text="ingredients"/> <android.support.v7.widget.recyclerview android:id="@+id/rv_ingredients" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="@dimen/dimen_8dp"/> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <textview android:id="@+id/tv_step" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="@dimen/dimen_8dp" android:textcolor="@color/colorprimarydark" android:textsize="@dimen/text_23sp" android:text="steps"/> <android.support.v7.widget.recyclerview android:id="@+id/rv_steps" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="@dimen/dimen_8dp"/> </linearlayout> </linearlayout> </scrollview> </framelayout>
both recyclerview
s have android:layout_height="wrap_content"
, means not scroll. rather, scrollview
in activity
view providing scrolling behavior, this view scroll position must saved.
you can have system giving scrollview
id. id want, long it's unique. don't have write java @ all.
<scrollview android:id="@+id/thisfixestheproblem" android:layout_width="match_parent" android:layout_height="match_parent">
make sure you're modifying scrollview
in activity's layout, not 1 in fragment's layout.
original
none of code posted should necessary save recyclerview
's scroll position on orientation change. long recyclerview
has unique id in layout, save scroll position automatically.
here small sample app shows automatic saving of scroll position, dynamically added fragment
. can see, instance state i'm saving myself whether button start fragment should visible.
mainactivity.java
public class mainactivity extends appcompatactivity { private button button; @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putboolean("buttonvisible", button.getvisibility() == view.visible); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button = (button) findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { getsupportfragmentmanager() .begintransaction() .replace(r.id.content, new myfragment()) .commit(); button.setvisibility(view.gone); } }); if (savedinstancestate != null) { boolean buttonvisible = savedinstancestate.getboolean("buttonvisible"); button.setvisibility(buttonvisible ? view.visible : view.gone); } } }
myfragment.java
public class myfragment extends fragment { @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view root = inflater.inflate(r.layout.myfragment, container, false); recyclerview recycler = (recyclerview) root.findviewbyid(r.id.recycler); recycler.setadapter(new myadapter()); return root; } private static class myadapter extends recyclerview.adapter<myviewholder> { @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { layoutinflater inflater = layoutinflater.from(parent.getcontext()); view itemview = inflater.inflate(r.layout.itemview, parent, false); return new myviewholder(itemview); } @override public void onbindviewholder(myviewholder holder, int position) { drawable d = new colordrawable(color.argb(0xff, 0, 0, position)); viewcompat.setbackground(holder.image, d); holder.text.settext("" + position); } @override public int getitemcount() { return 256; } } private static class myviewholder extends recyclerview.viewholder { final view image; final textview text; myviewholder(view itemview) { super(itemview); this.image = itemview.findviewbyid(r.id.image); this.text = (textview) itemview.findviewbyid(r.id.text); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"> <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="create fragment"/> </framelayout>
myfragment.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.recyclerview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutmanager="android.support.v7.widget.linearlayoutmanager" tools:listitem="@layout/itemview"/>
itemview.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <view android:id="@+id/image" android:layout_width="48dp" android:layout_height="48dp" android:layout_margin="12dp" tools:background="#333"/> <textview android:id="@+id/text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" tools:text="text"/> </linearlayout>
Comments
Post a Comment