我有一个可以预期的回收利用.我在布局中有一个按钮来填充列表.该按钮应该进行异步调用,结果我更改了按钮的外观.所有这一切都很好.

但是当我点击按钮并快速向下滚动列表时,异步调用的结果会更新新视图的按钮(替换旧视图的按钮).我该如何处理?当特定视图被重用时,我可以处理吗?

更新:

执行异步调用的适配器类的代码片段和ui的更新.

@Override

public void onBindViewHolder(CommentsViewHolder holder, int position) {

try {

Comments comment = comments.get(position);

holder.bindView(comment,position);

}

catch(Exception ex){ex.printStackTrace();}

}

@Override

public int getItemCount() {

if(comments==null)

{return 0;}

return comments.size();

//return comments.length();

}

public class CommentsViewHolder extends RecyclerView.ViewHolder {

TextView score ;

TextView commentText;

TextView commentTime;

TextView avatarId;

ImageButton minusOne;

ImageButton plusOne;

ParseObject model;

public CommentsViewHolder(View itemView) {

super(itemView);

//itemView.setBackgroundColor(Color.DKGRAY);

minusOne =(ImageButton)itemView.findViewById(R.id.decScore);

plusOne =(ImageButton)itemView.findViewById(R.id.incScore);

commentText = (TextView)itemView.findViewById(R.id.comment);

score = (TextView)itemView.findViewById(R.id.commentScore);

commentTime =(TextView)itemView.findViewById(R.id.commentTime);

avatarId = (TextView)itemView.findViewById(R.id.ivUserAvatar);

}

public void bindView(Comments comment, int position) {

commentText.setText(comment.getCommentText());

score.setText(Integer.toString(comment.getScore()));

String timeText = DateUtils.getRelativeTimeSpanString( comment.getCreatedAt().getTime(), System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS).toString();

timeText = timeText.replace("hours","hrs");

timeText = timeText.replace("seconds","secs");

timeText = timeText.replace("minutes","mins");

commentTime.setText(timeText);

int commentHandler = comment.getCommenterHandle();

String commenterNumber = "";

if(commentHandler==0)

{

commenterNumber = "OP";

}

else{

commenterNumber = "#"+commentHandler;

}

avatarId.setText( commenterNumber);

model = comment;

String choice = "none";

minusOne.setEnabled(true);

plusOne.setEnabled(true);

minusOne.setVisibility(View.VISIBLE);

plusOne.setVisibility(View.VISIBLE);

for (ParseObject choiceIter : choices) {

if ((choiceIter.getParseObject("comment").getObjectId()).equals(comment.getObjectId())) {

choice = choiceIter.getString("userChoice");

break;

}

}

Log.i("debug",comment.getCommentText()+" "+comment.getScore()+" "+choice);

switch (choice) {

case "plusOne":

Log.i("darkplus","setting darkplus");

plusOne.setImageResource(R.drawable.ic_add_circle_black_18dp);

plusOne.setOnClickListener(reversePlusOneOnClickListener);

//minusOne.setOnClickListener(minusOneOnClickListener);

minusOne.setVisibility(View.GONE);

break;

case "minusOne":

Log.i("darkminus","setting darkminus");

minusOne.setImageResource(R.drawable.ic_remove_circle_black_18dp);

minusOne.setOnClickListener(reverseMinusOneOnClickListener);

//plusOne.setOnClickListener(plusOneOnClickListener);

plusOne.setVisibility(View.GONE);

break;

case "none":

Log.i("darkregular","setting regular");

minusOne.setImageResource(R.drawable.ic_remove_black_18dp);

plusOne.setImageResource(R.drawable.ic_add_black_18dp);

plusOne.setOnClickListener(plusOneOnClickListener);

minusOne.setOnClickListener(minusOneOnClickListener);

break;

}

}

View.OnClickListener reversePlusOneOnClickListener = new View.OnClickListener() {

@Override

public void onClick(View v) {

if (!FourUtils.isConnected(v.getContext())) {

return;

}

minusOne.setEnabled(false);

plusOne.setEnabled(false);

model.increment("plusOne", -1);

model.increment("score", -1);

model.saveEventually(new SaveCallback() {

@Override

public void done(ParseException e) {

if (e == null) {

ParseQuery query = ParseQuery.getQuery("CommentChoice");

query.whereEqualTo("user", ParseUser.getCurrentUser());

query.whereEqualTo("comment", model);

query.fromPin(Four.COMMENT_CHOICE);

query.getFirstInBackground(new GetCallback() {

@Override

public void done(ParseObject parseObject, ParseException e) {

if (e == null) {

if (parseObject == null) {

parseObject = ParseObject.create("CommentChoice");

parseObject.put("comment", model);

parseObject.put("user", ParseUser.getCurrentUser());

}

parseObject.put("userChoice", "none");

parseObject.pinInBackground(Four.COMMENT_CHOICE, new SaveCallback() {

@Override

public void done(ParseException e) {

if (e == null) {

score.setText(Integer.toString(model.getInt("score")));

//votes.setText((model.getInt("minusOne") + model.getInt("plusOne")) + " votes");

minusOne.setVisibility(View.VISIBLE);

plusOne.setImageResource(R.drawable.ic_add_black_18dp);

plusOne.setOnClickListener(plusOneOnClickListener);

minusOne.setEnabled(true);

plusOne.setEnabled(true);

// minusOne.setOnClickListener(minusOneOnClickListener);

BusProvider.getInstance().post(new NewCommentChoicesAdded());

} else {

e.printStackTrace();

}

}

});

}

else{e.printStackTrace();}

}

});

} else {

e.printStackTrace();

Log.i("plus1 error", e.getMessage());

}

}

});

}

};

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐