ListView+RecyclerView缓存类的封装

Stella981
• 阅读 509
public class BaseViewHoler extends RecyclerView.ViewHolder {
    private Context context;
    //行布局的view
    private View mView;
    //用来装载id的集合 用法和map类似
    private SparseArray<View> sparseArray;


    public BaseViewHoler(View itemView, Context context) {
        super(itemView);
        mView = itemView;
        sparseArray = new SparseArray<>();
        this.context = context;
    }

    //初始化RecyclerView的
    public static BaseViewHoler createRecyclerViewHolder(Context context, ViewGroup group, int layoutId) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, group, false);
        BaseViewHoler baseViewHoler = new BaseViewHoler(itemView, context);
        return baseViewHoler;
    }

    //初始化listView的
    public static BaseViewHoler createListViewHolder(View view, ViewGroup group, int layoutId) {
        BaseViewHoler baseViewHoler = null;
        if (view == null) {
            view = LayoutInflater.from(group.getContext()).inflate(layoutId, group, false);
            baseViewHoler = new BaseViewHoler(view, group.getContext());
            view.setTag(baseViewHoler);
        } else {
            baseViewHoler = (BaseViewHoler) view.getTag();
        }
        return baseViewHoler;
    }

    //根据id来获得组件
    public <T extends View> T getView(int id) {
        //创建一个view 根据id从集合里取出
        View view = sparseArray.get(id);
        //如果没有取到  那么就通过findViewById的方式找到这个组件,然后将组件存放在集合里
        if (view == null) {
            view = mView.findViewById(id);
            sparseArray.put(id, view);
        }
        //最后返回的这个就是我们想要的组件了
        return (T) view;
    }

    //自己随意的定义方法
    //最基本的设置文字的方法
    public BaseViewHoler setText(int id, String str) {
        TextView textView = getView(id);
        if (str != null) {
            textView.setText(str);
        }
        return this;
    }


    //第三方轮播图banner
    public BaseViewHoler setBanner(int id, String url) {
        final Banner banner = getView(id);
        if (url != null) {
            NetTool.getInstance().startRequest(url, BannerBean.class, new CallBack<BannerBean>() {
                @Override
                public void onSuccess(BannerBean response) {
                    List<String> bannerBean = new ArrayList<String>();
                    for (int i = 0; i < response.getData().getBanners().size(); i++) {
                        bannerBean.add(response.getData().getBanners().get(i).getImage_url());
                    }
                    banner.setImageLoader(new GlideImageLoader());
                    banner.setImages(bannerBean);
                    banner.isAutoPlay(true);//自动播放
                    banner.setDelayTime(3000);//设置延时时间
                    banner.setIndicatorGravity(BannerConfig.CENTER);//设置小圆点的位置
                    banner.start();
                }

                @Override
                public void onError(Throwable e) {

                }
            });


        }
        return null;
    }

    class GlideImageLoader extends ImageLoader {

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }
    }


    //设置网络图片
    public BaseViewHoler setImage(int id, String url) {
        ImageView imageView = getView(id);
        if (url != null) {
            Glide.with(context).load(url).into(imageView);
        }
        return this;
    }

    //设置圆形图
    public BaseViewHoler setImage(int id, String url, Context context) {
        ImageView imageView = getView(id);
        if (url != null) {
            Glide.with(context).load(url).bitmapTransform(new CropCircleTransformation(context)).into(imageView);
        }
        return this;
    }

    //设置本地图片
    public BaseViewHoler setImage(int id, int resId) {
        ImageView imageView = getView(id);
        if (resId != 0) {
            imageView.setImageResource(resId);
        }
        return this;
    }


}
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Souleigh ✨ Souleigh ✨
2年前
前端性能优化 - 雅虎军规
无论是在工作中,还是在面试中,web前端性能的优化都是很重要的,那么我们进行优化需要从哪些方面入手呢?可以遵循雅虎的前端优化35条军规,这样对于优化有一个比较清晰的方向.35条军规1.尽量减少HTTP请求个数——须权衡2.使用CDN(内容分发网络)3.为文件头指定Expires或CacheControl,使内容具有缓存性。4.避免空的
Wesley13 Wesley13
2年前
java8新特性
Stream将List转换为Map,使用Collectors.toMap方法进行转换背景:User类,类中分别有id,name,age三个属性。List集合,userList,存储User对象1、指定keyvalue,value是对象中的某个属性值。 Map<Integer,StringuserMap1userList.str
Stella981 Stella981
2年前
Hive 删除行, 表 ,清空表
删除行A表数据如下id(String)       name(String)\1                       aaa2                      bbb3                      ccc\
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
达里尔 达里尔
3个月前
给数组添加新数据,判断数据是否重复
多选要进行数组拼接,希望判断往原数组里添的新数据是否重复,封装个简易方法languageconstdataArrayname:'aaa',id:1,name:'bbb',id:2;constnewDataname:'ccc',id:2;//要添加的新数