RecyclerView实现滚动滑动以及点击改变控件颜色

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jifashihan/article/details/51888963

项目中需要横向滚动效果,按照以前的思路,我会写一个ScrollView,里边加一个LinearLayout,在代码中动态加入控件,然后动态删除或者改变颜色,现在android有了新控件RecyclerView,可以方便的实现这样的功能,具体使用方法请参考这篇文章:Android 自定义RecyclerView 实现真正的Gallery效果
主要问题在于,我点击一个item,改变颜色,点击其他项,改变颜色,上一次点击的变回原来的颜色,刚开始实现的方法是在控件上直接修改,发现滑动的时候,颜色会乱掉,所以在adapter里边加入一个list进行标记,根据标记改变控件颜色
相应的adapter

public class AddDeviceAdapter extends RecyclerView.Adapter<AddDeviceAdapter.ViewHolder> /*implements View.OnClickListener*/{

    private OnItemClickListener mOnItemClickListener = null;

    private LayoutInflater mInflater;
    private List<AddDevice> mDatas;

    private AddDevice addDevice;
    private List<Boolean> isClicks;//控件是否被点击,默认为false,如果被点击,改变值,控件根据值改变自身颜色

    public AddDeviceAdapter(Context context, List<AddDevice> datas) {
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
        isClicks = new ArrayList<>();
        for(int i = 0;i<mDatas.size();i++){
            isClicks.add(false);
        }
    }

    public void setDatas(List<AddDevice> datas) {
        mDatas = datas;
        L.e("AddDeviceAdapter's mDatas===" + new Gson().toJson(mDatas));
    }

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

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

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_add_device, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.viewSpace = view.findViewById(R.id.viewSpace);
        viewHolder.ivAddDevice = (ImageView) view.findViewById(R.id.ivAddDevice);
        viewHolder.tvAddDevice = (TextView) view.findViewById(R.id.tvAddDevice);

        //将创建的View注册点击事件
     //   view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        addDevice = mDatas.get(i);
        viewHolder.ivAddDevice.setImageResource(addDevice.getResId());
        viewHolder.tvAddDevice.setText(addDevice.getItemText());
        //将数据保存在itemView的Tag中,以便点击时进行获取
        viewHolder.itemView.setTag(viewHolder.tvAddDevice);
        if(isClicks.get(i)){
            viewHolder.tvAddDevice.setTextColor(Color.parseColor("#00a0e9"));
        }else{
            viewHolder.tvAddDevice.setTextColor(Color.parseColor("#ffffff"));
        }
//        if(addDevice.isFirst()) {
//            viewHolder.viewSpace.setVisibility(View.GONE);
//        }

        // 如果设置了回调,则设置点击事件
        if (mOnItemClickListener != null) {
           viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int i = 0; i <isClicks.size();i++){
                        isClicks.set(i,false);
                    }
                    isClicks.set(i,true);
                     notifyDataSetChanged();
                    mOnItemClickListener.onItemClick(viewHolder.itemView,viewHolder.tvAddDevice, i);
               }
            });
        }
    }

   /* @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(, textView,(int)v.getTag());
        }
    }*/

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        View viewSpace;
        ImageView ivAddDevice;
        TextView tvAddDevice;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, TextView textView,int position);
    }
}
阅读更多
换一批

没有更多推荐了,返回首页