简体   繁体   中英

Wrong On Click called for list of items using recycler view adapter?

I have an activity with three fragments, each one with a separate RecyclerView with a bunch of items. When scrolling between fragments, the onClick() gets mixed up and click on an item in fragment 1 goes to the same position but for the item in fragment 3 therefore taking me to the wrong screen. Am I suppose to differentiate the pages in the custom onClick() method I have setup? All the fragments use the same adapter, so I am not sure if that is part to blame then? Not sure what code would be useful here, but here is my adapter class:

public class ExploreAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static OnEntryClickListener mOnEntryClickListener;
    private List<Explore> exploreList;
    private List<String> trackedProjects;
    private Context context;
    private Typeface typeFace, italicTypeface, boldTypeface;
    private int typeOfExplore;
    private HashMap<String, Boolean> uniqueExploreItems;
    private HashMap<String, Long> offSet;
    private int firstSet, secondSet;
    private boolean apiProcessing;

    private String stringValue;
    //firebase
    private DatabaseReference mDatabase;

    public void changeTracked(List<String> tracks) {
        trackedProjects = tracks;
        notifyDataSetChanged();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView projectTitle, projectCompany;
        public ImageView projectPicture;
        private ShineButton projectTrack;


        public MyViewHolder(View view) {
            super(view);
            projectTitle = (TextView) view.findViewById(R.id.exploreProjectTitle);
            projectCompany = (TextView) view.findViewById(R.id.exploreProjectCompany);
            projectPicture = (ImageView) view.findViewById(R.id.exploreProjectPicture);
            projectTrack = (ShineButton) view.findViewById(R.id.exploreProjectTrack);


            view.setOnClickListener(this);
            projectTrack.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if (mOnEntryClickListener != null) {
                mOnEntryClickListener.onEntryClick(v, getAdapterPosition());

            }
        }

    }



    public ExploreAdapter(Context mContext, List<Explore> explores, List<String> trackedProj, int typeOfE, String strV, HashMap<String, Boolean> uniqueE, HashMap<String, Long> offs, Typeface myTypeface, Typeface myTypefaceItalic, Typeface myTypefaceBold) {
        context = mContext;
        exploreList = explores;
        typeFace = myTypeface;
        italicTypeface = myTypefaceItalic;
        boldTypeface = myTypefaceBold;
        typeOfExplore = typeOfE;
        uniqueExploreItems = uniqueE;
        offSet = offs;
        stringValue = strV;

        apiProcessing = true;
        mDatabase = FirebaseDatabase.getInstance().getReference();
        trackedProjects = trackedProj;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new MyViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.explore_item, parent, false));


    }


    public void setOnEntryClickListener(OnEntryClickListener onEntryClickListener) {
        mOnEntryClickListener = onEntryClickListener;
    }

    public interface OnEntryClickListener {
        void onEntryClick(View view, int position);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            Explore proj = exploreList.get(position);
            myViewHolder.projectTitle.setTypeface(boldTypeface);
            myViewHolder.projectCompany.setTypeface(italicTypeface);
            myViewHolder.projectTitle.setText(proj.getProjectTitle());
            myViewHolder.projectCompany.setText(proj.getSubTitle());

            //for updates
            if(typeOfExplore == 1) {
                String createImagePath = Constants.PHASES_IMAGE + proj.getProjectPicture();
                Picasso.with(context).load(createImagePath).placeholder(R.drawable.default_liovinci_bg).resize(320, 240).into(myViewHolder.projectPicture);

            } else {
                String createImagePath = Constants.PROJECT_IMAGE + proj.getProjectPicture();
                Picasso.with(context).load(createImagePath).placeholder(R.drawable.default_liovinci_bg).resize(320, 240).into(myViewHolder.projectPicture);

            }

        if(typeOfExplore == 1 || typeOfExplore == 2) {

            myViewHolder.projectTrack.setVisibility(View.GONE);
        } else {
            if(trackedProjects != null && trackedProjects.contains(proj.getProjectId())) {

                myViewHolder.projectTrack.setColorFilter(ContextCompat.getColor(context, R.color.actionBlue), PorterDuff.Mode.SRC_IN);
                myViewHolder.projectTrack.setChecked(true);
            } else {

                myViewHolder.projectTrack.setColorFilter(ContextCompat.getColor(context, R.color.bottomBarGray), PorterDuff.Mode.SRC_IN);


            }

        }

If you are using view pager then it initialises all the fragments which you have bound to that view pager. So at last it will initialise last fragment and your adapter class will contains context from your last initialised fragment.

This is the reason. so to overcome your problem you should make different adapter for different fragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM