简体   繁体   English

错误点击使用回收器视图适配器调用了项目列表吗?

[英]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. 我有一个包含三个片段的活动,每个片段都有一个带有一堆物品的单独的RecyclerView 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. 在片段之间滚动时, onClick()变得混乱不清,并单击片段1中的项目到相同位置,但是对于片段3中的项目,因此将我带到错误的屏幕。 Am I suppose to differentiate the pages in the custom onClick() method I have setup? 我是否可以在已设置的自定义onClick()方法中区分页面? 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. 因此,要克服您的问题,您应该为不同的片段制作不同的适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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