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

时间:2016-11-19 04:54:56

标签: java android android-fragments

我有一个包含三个片段的活动,每个片段都有一个单独的RecyclerView和一堆项目。当在片段之间滚动时,onClick()混淆并且单击片段1中的项目到达相同位置但对于片段3中的项目因此将我带到错误的屏幕。我想在我设置的自定义onClick()方法中区分页面吗?所有碎片使用相同的适配器,所以我不确定那是否是责任的一部分呢?不知道这里有什么代码有用,但这是我的适配器类:

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);


            }

        }

1 个答案:

答案 0 :(得分:2)

如果您正在使用视图寻呼机,那么它会初始化您绑定到该视图寻呼机的所有片段。所以最后它会初始化最后一个片段,你的适配器类将包含你上一个初始化片段的上下文。

这就是原因。所以为了克服你的问题,你应该为不同的片段制作不同的适配器。