RecyclerView手指按下时,item背景变色

需求:recyclerview显示的时候,手指按下,item的背景发生变化

    步骤1、设置item的布局

xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:gravity="center_vertical"
    android:background="@drawable/bg_selector"
    android:focusable="true"
    >

    <de.hdodenhof.circleimageview.CircleImageView/>
    <LinearLayout />
LinearLayout>

RecyclerView手指按下时,item背景变色_第1张图片

步骤2、background背景选择 

android:background="@drawable/bg_selector"

xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/pressed"/>
    <item android:drawable="@drawable/normal"/>
selector>


步骤3、item的“事件”处理       代码阅读 onBindViewHolder

public class MyRecycleViewAdapter extends RecyclerView.Adapter {
    private ListmList;
    private Context context;
    public MyRecycleViewAdapter(List mList,Context context) {
        this.mList = mList;
        this.context = context;
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int postion);
        void onItemLongClick(View view,int postion);
    }

    private OnItemClickListener mOnItemClickListener;
    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_home, parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.title.setText(mList.get(position).getTitles());
        int r = new Random().nextInt(100);
        r = r % 31;
        if(r == 0) r=1;

        String uri = "http://***/resources/default_image_"+ r +".jpg";
        Picasso.with(context).load(uri).resize(550, 550).centerInside().into(holder.imageView);

        if(mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView,pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(holder.itemView,pos);
                    return false;
                }
            });
        }


    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{
        CircleImageView imageView;
        TextView title;
        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = (CircleImageView) itemView.findViewById(R.id.icon);
            title = (TextView) itemView.findViewById(R.id.title);
        }
    }
}

你可能感兴趣的:(android,recyclerview,item,background)