Incorrect updating of CardView list



  • CardView was a problem. With the scrubbing of the list, there's an upload of old data, i.e. a list as if it's displayed in in the bagraund, see the link to the video. The code used below. Tell me which way to address this issue.

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

    The fragment code that shows everything:

    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&lt;&gt;();
        for(int i=0; i&lt;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 part of the list

        <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"/>

    &lt;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"/&gt;
    
    &lt;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"/&gt;
    

    </RelativeLayout>



  • Method add() as it should be from the name, put a fragment in the container. And as soon as there was a situation in which the code was re-declared (screen/reset) it added a fragment to the container again, but there was no mechanism to remove the old fragment.

    Such a mechanism is present replace()

    So if you need to add a dynamic fragment, and you know that won't be replaced, use it. add()if this container is to be replaced by other fragments - use it replace()




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2