@nohchiadam

Как восстановить позицию RecyclerView при возврате на фрагменте?

Как восстановить позицию RecyclerView при возврате на фрагменте?
Позиция возвращается правильно, но потом скроллится вверх.

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));
    }
}


frafment_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>
  • Вопрос задан
  • 185 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы