gpt4 book ai didi

android - ViewHolder 模式 - 如何正确更新 View

转载 作者:搜寻专家 更新时间:2023-11-01 07:50:21 25 4
gpt4 key购买 nike

在扩展的 ArrayAdapter 中实现 viewHolder 模式,并在线性布局上实现 onClickListener 时,此 LinearLayout 的背景颜色发生了变化但对于其他随机行也是如此!

这是我的代码(已删除 viewHolder 以仅包含错误):

public class CustomFeedsAdapter extends ArrayAdapter<CustomCommunityQuestion>{

private static final String TAG = "CustomFeedsAdapter";
private Context context;
private ArrayList<CustomCommunityQuestion> customCommunityQuestions;

public CustomFeedsAdapter(Context context, ArrayList<CustomCommunityQuestion> customCommunityQuestions) {
super(context, -1, customCommunityQuestions);
this.context = context;
this.customCommunityQuestions = customCommunityQuestions;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;

// reuse views
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.cards_feeds_row, null, false);
holder.buttonFollow = (LinearLayout) rowView.findViewById(R.id.follow_layout);

rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}

final CustomCommunityQuestion currentQuestion = customCommunityQuestions.get(position);

// Set onClick Listeners
holder.buttonFollow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
VolleySingleton volleySingleton = VolleySingleton.getInstance(context);
StringRequest followJsonObjectRequest = new StringRequest(Request.Method.GET,
"http://private-e9644-popxoandroid.apiary-mock.com/quest_follow/1/0",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.d(TAG, response);
JSONObject responseObject = new JSONObject(response);
String message = responseObject.getString("message");
Integer success = responseObject.getInt("success");
Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
if(success!=0){
holder.buttonFollow.setBackgroundColor(R.color.holo_blue_bright); // Here the button I wish to update doesn't get the color, but instead some weird random row's button get's changed.
holder.buttonFollow.setEnabled(false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "There was some error! You were unable to follow", Toast.LENGTH_SHORT).show();
error.printStackTrace();
Log.e(TAG,error.getMessage());
}
});
volleySingleton.addToRequestQueue(followJsonObjectRequest);
}
});

return rowView;
}

private class ViewHolder {
private LinearLayout buttonFollow;
}
}

最佳答案

你在做什么

比方说,当前可见的行位于位置 0,1,2。您正在更新一个特定的 ViewHolder,它已经被设置为一个特定的标签(使用一个更好的词)- View 。当您稍后调用 holder = (ViewHolder)rowview.getTag() 时,会重新使用此 View 。为了便于说明,我们假设您正在更新位置 1 处的 View 。

滚动 listView 时会发生什么

当您滚动 ListView 时,通过从标签中获取 holder 来重新使用旧 View 。这意味着您之前更改的 View 现在正在重新使用。

您需要做什么

所以当你需要更新你的 View 时,首先更新一个相关的变量。例如:

在您的 CustomCommunityQuestion 类中创建一个属性并像这样实现一个 getter 和 setter:

boolean isBlue;

public void setIsBlue(booelan isblue){
this.isBlue = isblue;
}

public boolean isBlue(){
return isBlue;
}

现在在您的按钮 onClickListener 中,您可以将变量 isBlue 设置为 yes,并调用 notifyDataSetChanged()。此外,像这样处理属性 isBlue:

只需检查 isBlue 状态并相应地设置颜色

if(customCommunityQuestions.get(position).isBlue()){
holder.buttonFollow.setBackgroundColor(R.color.holo_blue_bright);
}else{
holder.buttonFollow.setBackgroundColor(R.color.black);
}

希望对您有所帮助!

关于android - ViewHolder 模式 - 如何正确更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35623303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com