2017-09-19 27 views
-1

我有一個recyclerView,它使用適配器綁定視圖並在每個視圖上執行單擊按鈕。但是,當我點擊項目1,然後項目+ = 7(例如:行0,7,14,...將由於onClick事件觸發器而被更改UI)也點擊。請幫幫我。 這裏是我的viewholder代碼:RecyclerView onClick每7行調用一次

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事件,我不得不刪除一些定義,因爲計算器不會讓我張貼太多代碼:

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. 謝謝你們無論如何。

+0

您是否在點擊偵聽器的視圖更改的其他情況下添加了默認視圖? –

+0

是的,我在佈局文件中設置了默認視圖。這是問題嗎? – CloudKlose

+0

檢查我的回答 –

回答

0

謝謝你們,我發現這個問題的解決方案。 在我的Recycler Apdater中,我實現了 public long getItemId(int position) {return position;} 然後使用ItemCachedView for recyclerView rclMatchItem.setItemViewCacheSize(100); 而且我再也不會每7行重複一次。

0

更改onClickListener從視圖控制器到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)); 
        } 
       } 
      } 
     }); 
} 
+0

感謝您的回覆,但它仍然有相同的結果。 – CloudKlose

0

創建您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 
} 

希望這有助於

+0

對不起,遲到了,我很忙,忘了回覆你。 我試過你的答案,但我可以問你的意思是我應該在ViewHolder中設置ClickClickListener,對不對?或者在OnBindViewHolder中做到這一點? 順便說一句,它仍然無法正常工作。 – CloudKlose