RecycleView在另一个上的一个recycleview卡上重复操作

时间:2018-01-19 06:18:26

标签: android firebase android-recyclerview

我正在开发一个测验android应用程序,其中问题及其各自的选项和答案将从服务器获取并添加到回收视图。 我已经完成了相同的编码并且它正在工作除了一个问题,当我选择任何选项时,如果它是正确的我将其背景变为绿色,或者如果它是错误的话我将它变为红色但是当我对任何问题做同样的操作时在回收视图中转换为其他问题选项。

As i click on Option A

6th question from previous solved question have option A automatically selected

我发布了我的Adapter和Fragment代码。

适配器代码: -

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(context);
    Log.e("I : ", "Creating " );
    switch (viewType) {

        case QUESTION:
            View viewQuestion = inflater.inflate(R.layout.question_card, parent, false);
            viewHolder = new QuestionFeed(viewQuestion);
            break;
        case LOADING:
            View viewLoading = inflater.inflate(R.layout.item_progress, parent, false);
            viewHolder = new LoadingVH(viewLoading);
            break;


    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    final PostRead result = postResults.get(position);
    String catname = null;
    try {
        catname = dataBaseHelper.getCatName(result.getCat());
    } catch (SQLException e) {
        e.printStackTrace();
    }
    Log.e("Position: ", "" + position);
    switch (getItemViewType(position)) {


        case QUESTION:
            final QuestionFeed textFeed = (QuestionFeed) holder;
            textFeed.question.setText(result.getQuestion());
            textFeed.optionA.setText(result.getA());
            textFeed.optionB.setText(result.getB());
            textFeed.optionC.setText(result.getC());
            textFeed.optionD.setText(result.getD());
            textFeed.answerView.setText(catname);
            textFeed.time.setText("" + TimeAgo.getTimeAgo((10000000000000L - result.getTimestamp())));

            textFeed.optionA_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    if (result.getAnswer().equals("a")) {
                        Log.e("AAAAnswer- ", result.getAnswer());
                        textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                    } else {
                        Log.e("Answer- ", result.getAnswer());
                        textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.wrong_answer));
                        if (result.getAnswer().equals("b")) {
                            textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        } else if (result.getAnswer().equals("c")) {
                            textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        } else if (result.getAnswer().equals("d")) {
                            textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
                        }
                    }
                    if (textFeed.descView.getVisibility() == View.GONE) {
                        textFeed.descView.setVisibility(View.VISIBLE);
                    }
                }
            });
            textFeed.optionB_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (result.getAnswer().equals("b")) {
                        Log.e("BBBAnswer- ", result.getAnswer());
                        textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
                    } else {
                        textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.wrong_answer));
                        if (result.getAnswer().equals("a")) {
                            textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        } else if (result.getAnswer().equals("c")) {
                            textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        } else if (result.getAnswer().equals("d")) {
                            textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        }
                    }
                    if (textFeed.descView.getVisibility() == View.GONE) {
                        textFeed.descView.setVisibility(View.VISIBLE);
                    }
                }
            });
            textFeed.optionC_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (result.getAnswer().equals("c")) {
                        Log.e("CCCAnswer- ", result.getAnswer());
                        textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
                    } else {
                        textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.wrong_answer));
                        if (result.getAnswer().equals("b")) {
                            textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        } else if (result.getAnswer().equals("a")) {
                            textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        } else if (result.getAnswer().equals("d")) {
                            textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        }
                    }
                    if (textFeed.descView.getVisibility() == View.GONE) {
                        textFeed.descView.setVisibility(View.VISIBLE);
                    }
                }
            });
            textFeed.optionD_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (result.getAnswer().equalsIgnoreCase("d")) {
                        Log.e("DDDAnswer- ", result.getAnswer());
                        textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
                    } else {
                        Log.e("DDDAnswer- ", result.getAnswer());
                        textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.wrong_answer));
                        if (result.getAnswer().equals("b")) {
                            textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        } else if (result.getAnswer().equals("c")) {
                            textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        } else if (result.getAnswer().equals("a")) {
                            textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));

                        }
                    }
                    if (textFeed.descView.getVisibility() == View.GONE) {
                        textFeed.descView.setVisibility(View.VISIBLE);
                    }
                }
            });
            break;
    }
}



@Override
public int getItemCount() {
    return postResults == null ? 0 : postResults.size();
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public int getItemViewType(int position) {
    return QUESTION;
}
public void add(PostRead r) {
    postResults.add(r);
    notifyItemInserted(postResults.size() - 1);
}
public void addAll(List<PostRead> moveResults) {
    for (PostRead result : moveResults) {
        add(result);
    }
    notifyDataSetChanged();
}
public void remove(PostRead r) {
    int position = postResults.indexOf(r);
    if (position > -1) {
        postResults.remove(position);
        notifyItemRemoved(position);
    }
}
public void clear() {
    isLoadingAdded = false;
    while (getItemCount() > 0) {
        remove(getItem(0));
    }
}
public boolean isEmpty() {
    return getItemCount() == 0;
}
public void addLoadingFooter() {
    isLoadingAdded = true;
    add(new PostRead());
}
public void removeLoadingFooter() {
    isLoadingAdded = false;
    int position = postResults.size() - 1;
    PostRead result = getItem(position);
    if (result != null) {
        postResults.remove(position);
        notifyItemRemoved(position);
    }
}
public PostRead getItem(int position) {
    return postResults.get(position);
}


protected class QuestionFeed extends RecyclerView.ViewHolder {

    private ImageView back, share;
    private MyTextView question;
    private MyTextView optionA, optionB, optionC, optionD, answerView, time;
    private LinearLayout optionA_layout, optionB_layout, optionC_layout, optionD_layout, errorLayout, descView;
    private ProgressBar mProgress;
    private Button btnRetry;
    private TextView txtError;
    private RelativeLayout mainLayout, backButton;

    public QuestionFeed(View itemView) {
        super(itemView);

        back = (ImageView) itemView.findViewById(R.id.backView);
        back = (ImageView) itemView.findViewById(R.id.share);
        question = (MyTextView) itemView.findViewById(R.id.question);
        optionA = (MyTextView) itemView.findViewById(R.id.optionA);
        optionB = (MyTextView) itemView.findViewById(R.id.optionB);
        optionC = (MyTextView) itemView.findViewById(R.id.optionC);
        optionD = (MyTextView) itemView.findViewById(R.id.optionD);
        answerView = (MyTextView) itemView.findViewById(R.id.subject);
        time = (MyTextView) itemView.findViewById(R.id.time);
        optionA_layout = (LinearLayout) itemView.findViewById(R.id.optionbuttonA);
        optionB_layout = (LinearLayout) itemView.findViewById(R.id.optionbuttonB);
        optionC_layout = (LinearLayout) itemView.findViewById(R.id.optionbuttonC);
        optionD_layout = (LinearLayout) itemView.findViewById(R.id.optionbuttonD);
        mProgress = (ProgressBar) itemView.findViewById(R.id.main_progress);
        mainLayout = (RelativeLayout) itemView.findViewById(R.id.content_sign_up);
        backButton = (RelativeLayout) itemView.findViewById(R.id.back_button);
        descView = (LinearLayout) itemView.findViewById(R.id.desc);
        errorLayout = (LinearLayout) itemView.findViewById(R.id.error_layout);
        btnRetry = (Button) itemView.findViewById(R.id.error_btn_retry);
        txtError = (TextView) itemView.findViewById(R.id.error_txt_cause);


    }
}

片段代码

private void loadFirstPage() {

    hideErrorView();

    Query query = postRef.orderByChild("timestamp").startAt(firstTime).limitToFirst(14);
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            final List<PostRead> allPosts = new ArrayList<>();
            childCount = (int) dataSnapshot.getChildrenCount();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                // TODO: handle the post
                Log.e("Online ", "" + firstTime);
                PostRead catItems = postSnapshot.getValue(PostRead.class);
                catItems.setPostkey(postSnapshot.getKey());
                allPosts.add(catItems);
                childCount--;
                if (childCount == 0) {
                    firstStart = 0;
                    dataBaseHelper.updatePosts(allPosts);
                    hideErrorView();
                    progressBar.setVisibility(View.GONE);
                    adapter.addAll(allPosts);
                    isLoading = false;
                    if (allPosts.size() == 0) {
                        isLastPage = true;
                    } else adapter.addLoadingFooter();
                }
                firstTime = catItems.getTimestamp();


            }


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

1 个答案:

答案 0 :(得分:0)

这是因为ViewsRecyclerView回收,您手动将背景颜色设置为不同的颜色。
解决方案是将textFeedBindViewHolder的背景的默认颜色设置为:

textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.default_color));
textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.default_color));
textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.default_color));
textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.default_color));