ViewHolder模式 - 如何正确更新视图

时间:2016-02-25 09:31:29

标签: android listview android-viewholder

在扩展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;
     }
}

2 个答案:

答案 0 :(得分:3)

你在做什么

当前可见的行位于让我们说位置0,1,2。您正在更新已设置为特定标记(用于更好的单词)的特定 ViewHolder - 视图。稍后当您致电holder = (ViewHolder)rowview.getTag()时,会重新使用此视图。为了解释的目的,我们假设您正在更新位置1的视图。

滚动listView

时会发生什么

滚动列表视图时,通过从标记中获取持有者来重新使用旧视图。这意味着您之前更改的视图现在正在重复使用。

您需要做什么

因此,当您需要更新视图时,首先要更新相关变量。例如:

在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);
}

希望它有所帮助!

答案 1 :(得分:1)

问题在于回收列表项:

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

    // reuse views
    final ViewHolder holder;
    if (convertView == null) {
        ...
    } else {
        holder = (ViewHolder) rowView.getTag();
        //!!!
        // here the view item is recycled but
        // holder.buttonFollow is not reset to its initial state
    }

解决方案:您的CustomFeedsAdapter必须记住每个项目的成功资格