首页 文章

RecyclerView onClick每隔7行调用一次

提问于
浏览
-1

我有一个recyclerView,它使用适配器绑定视图并在每个视图上执行单击按钮 . 但是当我点击第1项时,那么item = 7(例如:第0行,第7行,第14行,......因为onClick事件触发器而将更改UI)也会点击 . 请帮我 . 这是我的观察者代码:

public MatchItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);
    View matchView = inflater.inflate(R.layout.match_item, parent, false);

    MatchItemViewHolder matchItemViewHolder = null;
    matchItemViewHolder = new MatchItemViewHolder(matchView);
    return matchItemViewHolder;
}

我在这里调用onClick事件,我必须删除一些定义因为stackoverflow不让我发布太多代码:

public class MatchItemViewHolder extends RecyclerView.ViewHolder {
    public TextView txtTeamA;
    public TextView txtTeamB;
    public ImageView imvLive;
    public Button btnPredict;

    public boolean isLive = false;
    public boolean hasUnderOver = false;

    public MatchItemViewHolder(final View itemView) {
        super(itemView);
        txtTeamA = (TextView) itemView.findViewById(R.id.match_item_txt_teamA);
        txtTeamB = (TextView) itemView.findViewById(R.id.match_item_txt_teamB);
        txtRatioA = (TextView) itemView.findViewById(R.id.match_item_txt_ratioA);
        txtRatioB = (TextView) itemView.findViewById(R.id.match_item_txt_ratioB);
        txtPercentage = (TextView) itemView.findViewById(R.id.match_item_txt_percentage);
        txtBetDirection = (TextView) itemView.findViewById(R.id.match_item_txt_betDirection);
        txtMatchBo = (TextView) itemView.findViewById(R.id.match_item_txt_matchBo);
        txtMatchNote = (TextView) itemView.findViewById(R.id.match_item_txt_matchNote);
        txtMatchTime = (TextView) itemView.findViewById(R.id.match_item_txt_MatchTime);
        txtMatchHour = (TextView) itemView.findViewById(R.id.match_item_txt_matchHour);
        txtTourName = (TextView) itemView.findViewById(R.id.match_item_txt_TourName);
        imvTeamA = (ImageView) itemView.findViewById(R.id.match_item_imv_TeamA);
        imvTeamB = (ImageView) itemView.findViewById(R.id.match_item_imv_TeamB);
        imvGameImage = (ImageView) itemView.findViewById(R.id.match_item_imv_GameImage);
        imvLive = (ImageView) itemView.findViewById(R.id.match_item_imv_LiveImage);
        btnPredict = (Button) itemView.findViewById(R.id.match_item_btn_Predict);

        //set Item Click listener for item
        btnPredict.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean winner = false;
                PredictController predictController = new PredictController();
                winner = predictController.getWinner(txtTeamA.getText().toString(), txtTeamB.getText().toString());
                if (winner) {
                    txtBetDirection.setText("Xuôi nha");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        txtTeamA.setTextColor(context.getResources().getColor(R.color.colorPrimary, null));
                        txtTeamB.setTextColor(context.getResources().getColor(R.color.primary_text, null));
                    }else{
                        txtTeamA.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
                        txtTeamB.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
                    }
                } else {
                    txtBetDirection.setText("Ngược nhé");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        txtTeamB.setTextColor(context.getResources().getColor(R.color.colorPrimary, null));
                        txtTeamA.setTextColor(context.getResources().getColor(R.color.primary_text, null));
                    }else{
                        txtTeamB.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
                        txtTeamA.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
                    }
                }
            }
        });
    }
}

和OnBindViewHolder代码:

@Override
public void onBindViewHolder(MatchItemViewHolder holder, int position) {
    MatchesModel matchesModel = lstMatchItem.get(position);
    holder.txtTeamA.setText(matchesModel.teamA);
    holder.txtTeamB.setText(matchesModel.teamB);
    holder.txtRatioA.setText(matchesModel.teamARatio);
    holder.txtRatioB.setText(matchesModel.teamBRatio);
    holder.txtPercentage.setText(matchesModel.teamPercentage);
    holder.txtMatchBo.setText(matchesModel.matchBo);
    if (matchesModel.matchDate != null) {
        holder.txtMatchTime.setText(matchesModel.matchDate);
        holder.txtMatchHour.setText(matchesModel.matchDate.split("\\s")[1]);
    }
    holder.txtMatchNote.setText(matchesModel.matchNote);
    Picasso.with(context).load(matchesModel.teamAImage).fit().into(holder.imvTeamA);
    Picasso.with(context).load(matchesModel.teamBImage).fit().into(holder.imvTeamB);
    holder.txtTourName.setText(matchesModel.tourName);
    Picasso.with(context).load(matchesModel.gameImage).fit().into(holder.imvGameImage);

更新:我已经弄清楚如何解决问题 . 只需使用 rclMatchItem.setItemViewCacheSize(100); 设置recyclerView缓存视图是问题,也许默认它是7.无论如何,谢谢你们 .

3 回答

  • 0

    谢谢大家,我找到了解决这个问题的方法 . 在我的Recycler Apdater中,我实现 public long getItemId(int position) {return position;} 然后使用ItemCachedView进行recyclerView rclMatchItem.setItemViewCacheSize(100); 而且我再也不会每7行重复一次 .

  • 0

    onClickListener 从ViewController更改为 onBindView()

    @Override
    public void onBindViewHolder(MatchItemViewHolder holder, int position) {
    
    holder.btnPredict.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean winner = false;
                    PredictController predictController = new PredictController();
                    winner = predictController.getWinner(txtTeamA.getText().toString(), txtTeamB.getText().toString());
                    if (winner) {
                        txtBetDirection.setText("Xuôi nha");
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            txtTeamA.setTextColor(context.getResources().getColor(R.color.colorPrimary, null));
                            txtTeamB.setTextColor(context.getResources().getColor(R.color.primary_text, null));
                        }else{
                            txtTeamA.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
                            txtTeamB.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
                        }
                    } else {
                        txtBetDirection.setText("Ngược nhé");
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            txtTeamB.setTextColor(context.getResources().getColor(R.color.colorPrimary, null));
                            txtTeamA.setTextColor(context.getResources().getColor(R.color.primary_text, null));
                        }else{
                            txtTeamB.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
                            txtTeamA.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
                        }
                    }
                }
            });
    }
    
  • 1

    MatchesModel 对象中创建一个布尔变量 showWhoWinner varibale .

    onBindViewHolder 方法设置中单击监听器并使 showWhoWinner 变量的值变为反转 .

    btnPredict.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            matchesModel.setShowWhoWinner(!matchesModel.isShowWhoWinner);
            notifyItemChanged(position);
    }
    

    然后在 onBindViewHolder 中,将颜色变化的条件:

    if(matcherModel.isShowWhoWinner()){
                boolean winner = false;
                PredictController predictController = new PredictController();
                winner = predictController.getWinner(txtTeamA.getText().toString(), txtTeamB.getText().toString());
                if (winner) {
                 //Winner Color
    
                } else {
                 //Default color    
                }
    }
    else{
    //Default color 
    }
    

    希望这可以帮助

相关问题