RecyclerView wrong position set onBindViewHolder

At the beginning, my RecyclerView elements are fine, but when scrolling, the elements are displayed in the wrong position, for example: item 6, shown at position 67. Although the onClick listener and getAdapterPosition () work well and show the correct elements. Why?

import android.content.Context; import android.graphics.Typeface; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.neganet.darotabii.R; import com.neganet.darotabii.beans.headers_Bean; import java.util.List; public class FSecAdapter2 extends RecyclerView.Adapter<FSecAdapter2.mViewHolder> { private static List<headers_Bean> items_t; private static int secNo; private Typeface font; private boolean showSoreName; private static Context mContext; private static String olorString; private static String fSubjectText=""; private static Static_Datas static_datas; private static QDataGetter dataGetter; public FSecAdapter2(List<headers_Bean> t_items, Context cnt, int secNo) { setHasStableIds(true); this.mContext=cnt; this.items_t = t_items; this.static_datas=new Static_Datas(mContext); this.secNo=secNo; } public static class mViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private OnTitleClickListener titClickListener; static TextView title; public mViewHolder(View itemView) { super(itemView); this.title = (TextView) itemView.findViewById(R.id.title2); this.title.setTypeface(static_datas.get_titr_1_Font()); this.title.setOnClickListener(this); this.titClickListener=(OnTitleClickListener) FSecAdapter2.mContext; } @Override public void onClick(View v) { titClickListener.onTitleClick(v,items_t.get(getAdapterPosition()).serial+"*"+items_t.get(getAdapterPosition()).titr); } } @Override public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView; itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.titles_item, parent, false); mViewHolder mVH=new mViewHolder(itemView); return mVH; } @Override public void onBindViewHolder(mViewHolder holder, int position) { final headers_Bean curHbean=this.items_t.get(position); try { holder.title.setText(static_datas.fa_Ye_Ke(curHbean.titr)); } catch (Exception e) { e.printStackTrace(); } } @Override public int getItemCount() { return items_t.size(); } public interface OnTitleClickListener { public void onTitleClick(View v, String serial); } public void updateData(headers_Bean newHead){ items_t.add(items_t.size(),newHead); notifyItemInserted(items_t.size()-1); }} 

and in the fragment:

  mAdapter = new FSecAdapter2(titList,getActivity(),secNo); final LinearLayoutManager mLayoutManager = new LinearLayoutManager(this.getActivity()); mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); titRecycle.setLayoutManager(mLayoutManager); titRecycle.setItemAnimator(new DefaultItemAnimator()); titRecycle.setAdapter(mAdapter); 
+13
android android-recyclerview recycler-adapter
source share
3 answers

in public void onBindViewHolder()

add

 holder.setIsRecyclable(false); 
+29
source share

In your adapter class, override this method and replace position with getItemViewType (position)

 @Override public int getItemViewType(int position) { return position; } 
+6
source share

None of the above methods worked for me in any situation, and I do not want to sacrifice the possibility of processing. The only working solution I found was to change the return value for both of these methods inside the adapter to position values:

 override fun getItemViewType(position: Int): Int { return position } override fun getItemId(position: Int): Long { return position.toLong() } 

The returned position will always be correct. It needs to be changed if you use viewType.

0
source share

All Articles