androidandroid-recyclerview

RecyclerView wrong position set onBindViewHolder


At First, my RecyclerView items are ok, but on scrolling, items are shown on the wrong position for example: item 6 shown in position 67. Although onClick listener and getAdapterPosition() works good and shows correct items. 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 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);

Solution

  • in public void onBindViewHolder()

    add

    holder.setIsRecyclable(false);