Skip to content Skip to sidebar Skip to footer

An Unordered Recycleview List After Scrolling

If I scroll this list RecycleView with the mouse wheel my items in the list look like unordered. I do not understand , why? An incorrect clicked item in the new activity from Rec

Solution 1:

I think the items in the RecyclerView are unordered because the references for views which are present in your list_item_crime_police.xml or list_item_crime.xml layout file is declared as class members of CrimeListFragment and not inside CrimeHolder class as :

publicclassCrimeListFragmentextendsFragment {

    // your other views and variables related declarations// these are problematic declaration that keeps on being reusedprivate TextView mTitleTextView;
    private TextView mDateTextView;
    private ImageView mSolvedImageView;
    private Button mButtonCallPolice;
    // your other declarations// your other code
}

That's why, the same view instances such as mTitleTextView, mDateTextView, mSolvedImageView, mButtonCallPolice, etc. are being reused for every CrimeHolder instances causing the unordering of the items in the list. Now, in order to fix this problem, you can simply move these lines to code to CrimeHolder class as class variable which would ensures that every new CrimeHolder instance will have separate instances of above-mentioned views as :

privateclassCrimeHolderextendsRecyclerView.ViewHolder implementsView.OnClickListener {
         // declare all of them here so there will be unique instances of these views for unique viewholderprivate TextView mTitleTextView;
         private TextView mDateTextView;
         private ImageView mSolvedImageView;
         private Button mButtonCallPolice;

         publicCrimeHolder(int layout, LayoutInflater inflater, ViewGroup parent ) {

            ViewitemView= inflater.inflate(layout, parent, false));
            super(itemView);

            mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title);
            mDateTextView = (TextView) itemView.findViewById(R.id.crime_date);
            mSolvedImageView = (ImageView) itemView.findViewById(R.id.crime_solved);

            if (itemView.findViewById(R.id.call_police)!=null) {
                mButtonCallPolice = (Button) itemView.findViewById(R.id.call_police);
            }

            itemView.setOnClickListener(this);
        }

        publicvoidbind(Crime crime) {

            mCrime = crime;
            mTitleTextView.setText(mCrime.getTitle());
            mDateFormat = DateFormat.format("EEE, MMM dd, yyyy", mCrime.getDate());

            mDateTextView.setText(mDateFormat);
            mSolvedImageView.setVisibility(mCrime.isSolved() ? View.VISIBLE :
                    View.GONE);

            if(mCrime.isRequiresPolice()){
                mButtonCallPolice.setEnabled(true);
            }
        }

        @OverridepublicvoidonClick(View view) {

            Intentintent= CrimePagerActivity.newIntent(getActivity(),
                    CrimeLab.get(requireActivity()).getCrimes().get((getAdapterPosition())).getId());

            startActivityForResult(intent, REQUEST_CRIME);
        }

    }  

It fixed my problem and I hope it will fix yours too.

Post a Comment for "An Unordered Recycleview List After Scrolling"