RecyclerView项目单击选中状态

时间:2017-10-03 11:54:29

标签: android android-recyclerview

我有一个横向RecyclerView,其中我有一个圆形ImageView,在项目点击时,我希望所选的圆形图像突出显示。

我将https://github.com/hdodenhof/CircleImageView用于广告ImageView

我试过但是没有得到如何禁用当前位置以外的其他位置的突出显示项目。

我的适配器代码如下所示

public class AddEventIconsAdapter extends RecyclerView.Adapter<AddEventIconsAdapter.NavigationViewHolder> {

private Context context;
public int mSelectedItem = -1;
List<ChildSpouseCustomModel> listIcons;


public AddEventIconsAdapter(Context context, List<ChildSpouseCustomModel> 
 listIcons) {
    this.context = context;
    this.listIcons = listIcons;


}

@Override
public AddEventIconsAdapter.NavigationViewHolder 
onCreateViewHolder(ViewGroup parent, int viewType) {

  View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_add_icons, parent, false);
    return new AddEventIconsAdapter.NavigationViewHolder(view);
}

@Override
public void onBindViewHolder(final AddEventIconsAdapter.NavigationViewHolder holder, final int position) {

    Glide.with(context).load(listIcons.get(position).getImgUrl())
            .thumbnail(0.5f)
            .crossFade()
            .error(R.drawable.ic_user_placeholder)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(holder.img_EventIcon);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (mSelectedItem == position) {
                mSelectedItem=-1;

                holder.img_EventIcon.setBorderWidth(0);
                holder.img_EventIcon.setBorderColor(context.getResources().getColor(R.color.colorPrimary));

            } else {
                mSelectedItem = position;
                notifyDataSetChanged();
                holder.img_EventIcon.setBorderWidth(5);
                holder.img_EventIcon.setBorderColor(context.getResources().getColor(R.color.colorPrimary));

            }

        }
    });
}

@Override
public int getItemCount() {
    return listIcons.size();
}


class NavigationViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


    private CircleImageView img_EventIcon;


    public NavigationViewHolder(View itemView) {
        super(itemView);
        img_EventIcon = (CircleImageView) itemView.findViewById(R.id.profile_image_parent);


    }

    @Override
    public void onClick(View v) {

    }
}
}

我的列表项xml在下面给出

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="6dp"
android:layout_marginRight="8dp"
android:gravity="center"
android:orientation="vertical">


<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/profile_image_parent"
    android:layout_width="52dp"
    android:layout_height="52dp"
    android:src="@drawable/ic_user_placeholder" />

</LinearLayout>

我的预期输出:一次突出显示一个单击的图像

enter image description here

运行上述代码时的当前输出:

enter image description here

如何仅突出显示所点击的项目圆圈图像?无论如何,任何人都可以帮助我解决它。

2 个答案:

答案 0 :(得分:3)

检查此答案,这将管理选择和删除

 @Override
        public void onBindViewHolder(final AddEventIconsAdapter.NavigationViewHolder holder, final int position) {

        Glide.with(context).load(listIcons.get(position).getImgUrl())
                .thumbnail(0.5f)
                .crossFade()
                .error(R.drawable.ic_user_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.img_EventIcon);

        if(mSelectedItem == position)
        {
            holder.img_EventIcon.setBorderWidth(5);
            holder.img_EventIcon.setBorderColor(context.getResources().getColor(R.color.colorPrimary));

        }
        else
        {
            holder.img_EventIcon.setBorderWidth(0);
            holder.img_EventIcon.setBorderColor(context.getResources().getColor(R.color.colorPrimary));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mSelectedItem == position) {
                    mSelectedItem=-1;



                } else {
                    mSelectedItem = position;

                }
                notifyDataSetChanged();
            }
        });
}

答案 1 :(得分:0)

class NavigationViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

-->>in

public NavigationViewHolder(View itemView) {

-->> in 

 imageview initiation..

 -->> below

 itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  Toast.makeText(contact, "Any message", Toast.LENGTH_SHORT).show();      
            }
        });