关闭

RecyclerView万能适配器以及点击事件

标签: Recycler万能适配器点击事件
195人阅读 评论(0) 收藏 举报
分类:

首先是ViewHolder类,RecyclerHolder.java

public class RecycleHolder extends RecyclerView.ViewHolder {

    /** 用于存储当前item当中的View */
    private SparseArray<View> mViews;

    public RecycleHolder(View itemView) {
        super(itemView);
        mViews = new SparseArray<View>();
    }
    public <T extends View> T findView(int ViewId) {
        View view = mViews.get(ViewId);
        //集合中没有,则从item当中获取,并存入集合当中
        if (view == null) {
            view = itemView.findViewById(ViewId);
            mViews.put(ViewId, view);
        }
        return (T) view;
    }
    public RecycleHolder setText(int viewId, String text) {
        TextView tv = findView(viewId);
        tv.setText(text);
        return this;
    }
    public RecycleHolder setText(int viewId, int text) {
        TextView tv = findView(viewId);
        tv.setText(text);
        return this;
    }
    public RecycleHolder setImageResource(int viewId, int ImageId) {
        ImageView image = findView(viewId);
        image.setImageResource(ImageId);
        return this;
    }
    public RecycleHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView imageView= findView(viewId);
        imageView.setImageBitmap(bitmap);
        return this;
    }
    public RecycleHolder setImageNet(int viewId, String url) {
        final ImageView imageView= findView(viewId);
        //使用你所用的网络框架等,这里使用imageloader
        ImageLoader.getInstance().loadImage(url, new SimpleImageLoadingListener() {

            @Override
            public void onLoadingComplete(String imageUri, View view,Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);
                imageView.setImageBitmap(loadedImage);

            }

        });
        return this;
    }
}

接下来是适配器RecyclerAdapter.java类

public abstract class RecyclerAdapter<T> extends RecyclerView.Adapter<RecycleHolder> {

    private Context mContext;
    private List<T> mDatas;
    private int mLayoutId;
    private LayoutInflater mInflater;

    private OnItemClickListener onItemClickListener;

    public RecyclerAdapter(Context mContext, List<T> mDatas, int mLayoutId) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        this.mLayoutId = mLayoutId;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecycleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecycleHolder(mInflater.inflate(mLayoutId, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecycleHolder holder, int position) {
        convert(holder, mDatas.get(position), position);
        if (onItemClickListener != null) {
            //设置背景
            Log.i("TAG","点击");
            //holder.itemView.setBackgroundResource(R.drawable.recycler_bg);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //注意,这里的position不要用上面参数中的position,会出现位置错乱\
                    onItemClickListener.OnItemClickListener(holder.itemView, holder.getLayoutPosition());
                }
            });
        }

    }

    public abstract void convert(RecycleHolder holder, T data, int position);

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

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void OnItemClickListener(View view, int position);
    }
}

最后是为RecyclerView设置适配器和点击事件

mainRecyclerView.setAdapter(adapter = new RecyclerAdapter<HuaBean>(MainActivity.this, huaBeanList, R.layout.item_main_recycler) {

       @Override
       public void convert(RecycleHolder holder, HuaBainListBean huaBainListBean,int position) {
           holder.setText(R.id.item_main_recycler_title, huaBainListBean.getTitle());
           holder.setText(R.id.item_main_recycler_time, huaBainListBean.getCtime());
           final String newsUrl = huaBainListBean.getUrl();
           newsUrlList.add(newsUrl);
          String picUrl = huaBainListBean.getPicUrl();
          holder.setImageNet(R.id.item_main_recycler_image,picUrl);

}
});
      adapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
          @Override
          public void OnItemClickListener(View view, int position) {
              String url=newsUrlList.get(position);
              Intent intent=new Intent(MainActivity.this,SecondActivity.class);
              intent.putExtra("chooseUrl",url);
              startActivity(intent);
           }
    });
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:53056次
    • 积分:1147
    • 等级:
    • 排名:千里之外
    • 原创:56篇
    • 转载:19篇
    • 译文:0篇
    • 评论:10条
    最新评论