There was a problem using CardView. When scrolling the list, there is an overlay of old data, i.e. the list seems to be displayed in the background, see the video link. Below is the code that I used. Tell me which way to look to solve this problem.

Video : https://www.dropbox.com/s/7p8pihs6aqxkovb/device-2016-07-14-151103.mp4?dl=0

Fragment code in which everything is displayed:

public class PreviewFragment extends Fragment { private List<Person> persons; private RecyclerView rv; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.recyclerview_activity, null); rv=(RecyclerView)v.findViewById(R.id.rv); LinearLayoutManager llm = new LinearLayoutManager(getActivity()); rv.setLayoutManager(llm); initializeData(); initializeAdapter(); return v; } private void initializeData(){ persons = new ArrayList<>(); for(int i=0; i<50; i++){ persons.add(new Person("Name_" + i, "years old " + i*i, android.R.drawable.arrow_up_float)); } } private void initializeAdapter(){ RVAdapter adapter = new RVAdapter(persons); rv.setAdapter(adapter); } } 

Layout for Fragment

 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="60dp"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> 

Layout list item

  <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/person_photo" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginRight="16dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/person_name" android:layout_toRightOf="@+id/person_photo" android:layout_alignParentTop="true" android:textSize="30sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/person_age" android:layout_toRightOf="@+id/person_photo" android:layout_below="@+id/person_name"/> </RelativeLayout> 

  • you may overlap each other fragments. How do you add a snippet to an Activity ? - Vladyslav Matviienko
  • FrameLayout lies in the main Activity, which I replace with the necessary fragment as follows: PreviewFragment previuFragment = new PreviewFragment (); fTrans = getSupportFragmentManager (). beginTransaction (); fTrans.add (R.id.fragment_container, previuFragment); fTrans.commit (); - Alexander Tkachenko
  • @AlexanderTkachenko to replace fragments, the replace() method is used and add() directly adds. - Silento
  • Asgard, thank you very much. In fact, this was the solution. Tell me, why did this happen? At the first launch, a new fragment was added to an empty container. Why did this lead to this behavior? - Alexander Tkachenko
  • @AlexanderTkachenko , see my answer - Yuriy SPb ♦

2 answers 2

The add() method, as the name suggests, puts a fragment in a container. And as soon as a situation occurred in which this code was re-called (screen rotation / application restart), he again added the fragment to this container - but there was no mechanism for deleting the old fragment.

Such a mechanism is present in replace() .

Therefore, if you need to dynamically add a fragment, and you know that it will not be replaced - use add() , if this container should be replaced with other fragments - use replace() .

  • This is not entirely true, with replace extra work on creating a fragment is done, whereas it can be recovered from memory without a single line of code. Those. the fragment that is visible after the one added when the screen is rotated is the fragment recovered from the memory. So you should not use replace , but check if there is a fragment in memory. See my answer. - Yuriy SPb ♦

Adding a fragment to the activation when the screen rotates should be as follows:

  1. Check if there is a fragment in memory.
  2. If not, add.

If you don’t check if there is a fragment in the memory and instead of adding replace() , you will do an extra job of creating the fragment. If it still has a work for data acquisition in it, it will be done. Do not do it this way. And it is necessary so:

 Fragment fragmentHotelsList = getSupportFragmentManager().findFragmentById(R.id.container); if (fragmentHotelsList == null) { fragmentHotelsList = new ModelsListFragment(); getSupportFragmentManager(). beginTransaction().add(R.id.container, fragmentHotelsList) .commit(); }