N
N
nohchiadam2019-09-11 14:02:43
Android
nohchiadam, 2019-09-11 14:02:43

How to restore RecyclerView position when returning on fragment?

How to restore RecyclerView position when returning on fragment?
The position returns correctly, but then scrolls up.
fragment_main.class

public class main extends Fragment {

    View v;
    private RecyclerView myrecylerview;
    private List<sp> spList;
    private Context mContext;
    private CardView CardResume;
    private TextView TextResume;
    private TextView TextX;
    private ImageView ImageMain;




    public main() {
    }


    @SuppressLint("SetTextI18n")
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        final SharedPreferences settings = this.getActivity().getSharedPreferences("MyAppSett", MODE_PRIVATE);
        v = inflater.inflate(R.layout.fragment_main, container, false);
        myrecylerview =(RecyclerView) v.findViewById(R.id.pr);
        MyRecyclerViewAdapter recyclerViewAdapter = new MyRecyclerViewAdapter(getContext(),spList);
        myrecylerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        myrecylerview.setAdapter(recyclerViewAdapter);
        CardResume =(CardView) v.findViewById(R.id.card);
        TextResume = (TextView)v.findViewById(R.id.resume);
        TextX = (TextView)v.findViewById(R.id.x);
        ImageMain = (ImageView)v.findViewById(R.id.imageid);

        CardResume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(getContext(),prophets.class);
                intent.putExtra("resume",1);
                getContext().startActivity(intent);
            }
        });
        int index = settings.getInt("index", 0);
        int name_resume = settings.getInt("name_resume", 0);
        if(index == -1)
        {
        CardResume.setVisibility(View.GONE);
        }else{
            String[] myStringsArray= getResources().getStringArray(R.array.resume);
            TextResume.setText(myStringsArray[name_resume]);
        }

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences settings = this.getActivity().getSharedPreferences("MyAppSett", MODE_PRIVATE);
        int index = settings.getInt("index", 0);
        if(index == -1)
        {
            CardResume.setVisibility(View.GONE);
        }
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       spList = new ArrayList<>();
        spList.add(new sp("1",R.drawable.1));
        spList.add(new sp("2",R.drawable.2_small));
        spList.add(new sp("3",R.drawable.3_small));
        spList.add(new sp("4",R.drawable.4_small));
        spList.add(new sp("5",R.drawable.5_small));
        spList.add(new sp("6",R.drawable.6_small));
        spList.add(new sp("7",R.drawable.7_small));
        spList.add(new sp("8",R.drawable.8_small));
        spList.add(new sp("9",R.drawable.9_small));
        spList.add(new sp("10",R.drawable.10_small));
        spList.add(new sp("11",R.drawable.11_small));
        spList.add(new sp("12",R.drawable.12_small));
        spList.add(new sp("13",R.drawable.13_small));
    }
}

fragment_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sv"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".main"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

<RelativeLayout
    android:id="@+id/rr"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="ScrollViewSize">

    <android.support.v7.widget.RecyclerView
        android:layout_below="@+id/card"
        android:id="@+id/pr"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </android.support.v7.widget.RecyclerView>



</RelativeLayout>
</ScrollView>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question