如何自定义RecyclerView onclick更改图像像复选框一样工作?

时间:2015-06-06 06:20:39

标签: android android-recyclerview

我是一名新的Android开发人员,有人可以给我一个自定义回收站视图的示例,点击并更改图片。

请帮帮我。

提前致谢..

1 个答案:

答案 0 :(得分:0)

编写此代码以创建自定义视图

public class CustomView extends RecyclerView.Adapter<CustomView.RecyclerViewHolder> {


    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

        final TextView mName, mPhone;

        View mCircle;

        RecyclerViewHolder(View itemView) {
            super(itemView);

            mName = (TextView) itemView.findViewById(R.id.CONTACT_name);
            mPhone = (TextView) itemView.findViewById(R.id.CONTACT_phone);
            mCircle = itemView.findViewById(R.id.CONTACT_circle);


        }
    }

    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_layout, viewGroup, false);
        return new RecyclerViewHolder(v);
    }

    public void onBindViewHolder(RecyclerViewHolder viewHolder, int i) {
        // get the single element from the main array
        final Contact contact = Contact.CONTACTS[i];
        // Set the values
        viewHolder.mName.setText(contact.get(Contact.Field.NAME));
        viewHolder.mPhone.setText(contact.get(Contact.Field.PHONE));


        // Set the color of the shape
        GradientDrawable bgShape = (GradientDrawable) viewHolder.mCircle.getBackground();
        bgShape.setColor(Color.parseColor(contact.get(Contact.Field.COLOR)));
    }

    @Override
    public int getItemCount() {
        return Contact.CONTACTS.length;
    }
}