设计模式-适配器模式

设计模式—适配器模式

最近在公司做一个装修的项目,有一个功能,根据装修进度画一个View,类似就是这样的一个View.

这里写图片描述

然后我就新建了一个继承LinearLayout的 Schedule

public class Schedule  extends LinearLayout {
    private List<ScheduleBean> scheduleBeanList;
     public Schedule(Context context,List<ScheduleBean> listData) {
        super(context);

        this.scheduleBeanList = listData;
        initView(context);
    }

    public Schedule(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public Schedule(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }



    private void initView(Context context) {
        this.setOrientation(HORIZONTAL);

        int size = scheduleBeanList.size();
        View view;
        CircleImageView imageView;
        TextView textView;
        View wire;
        for (int i = 0; i < size; i++) {
            view = LayoutInflater.from(context).inflate(R.layout.view_myhome_schedule, null);
            imageView = (CircleImageView) view.findViewById(R.id.iv_myhome_s_shedule);
            textView = (TextView) view.findViewById(R.id.tv_myhome_s_shedule);
            wire = view.findViewById(R.id.v_myhome_s_shedule);


            Glide.with(context)
                    .load(scheduleBeanList.get(i).getPhoto())
                    .into(imageView);
            textView.setText(scheduleBeanList.get(i).getTitle());
            if (i == size - 1) {
                wire.setVisibility(View.GONE);
            } else {
                wire.setVisibility(View.VISIBLE);
            }
            addView(view);
        }

    }

然后在Activity中使用的时候呢就是

Schedule schedule=new Schedule(this,listData);

得到一个schedule,然后在add到activity的layout布局中就算是完成了。可是。。。问题来了,就只在这个Schedule中,我写死了传递的数据类型

 private List<ScheduleBean> scheduleBeanList

那在后面的更新中发现这样的进度View,还会在其他地方出现.并且后台返回的数据是在另外一个数据集合中我必须将数据先提取出来,转成上面的数据模型传递到Schedule中,才能实现。为了使这个Schedule适配更多的数据源,我想到了适配器模式。
那这个思路就是,将数据A使用Adapter转换成Schedule能识别的数据B。
首先我写了一个ScheduleAdapter,如下

public abstract class ScheduleAdapter<T> {

    private List<T> mTagDatas;

    public ScheduleAdapter(List<T> datas) {
        mTagDatas = datas;
    }

    public ScheduleAdapter(T[] datas) {
        mTagDatas = new ArrayList<T>(Arrays.asList(datas));
    }

    static interface OnDataChangedListener {
        void onChanged();
    }


    public int getCount() {
        return mTagDatas == null ? 0 : mTagDatas.size();
    }

    public T getItem(int position) {
        return mTagDatas.get(position);
    }

    public abstract List<ScheduleBean> getView(List<T> t);

    public boolean setSelected(int position, T t) {
        return false;
    }

    public List<T> getData() {
        return mTagDatas;

    }

}

然后修改Schedule

...
    public Schedule(Context context, ScheduleAdapter adapter) {
        super(context);
        List<ScheduleBean> listData = adapter.getView(adapter.getData());
        this.scheduleBeanList = listData;
        initView(context);
    }
...

这里在Schedule中修改了构造方法,将原先传入的数据源用Adapter代替,而数据源来源于adapter,在new adapter时要实现其抽象方法

    public abstract List<ScheduleBean> getView(List<T> t);

getView()返回的就是Schedule所需要的数据格式。
那在activity中实现方式就是

  Schedule schedule = new Schedule(getActivity(), new ScheduleAdapter<BindBean.ResponseBean.FlowBean>(flowBean) {
                    @Override
                    public List<ScheduleBean> getView(List<BindBean.ResponseBean.FlowBean> t) {
                        ScheduleBean bean;
                        List<ScheduleBean> listBean = new ArrayList<>();
                        for (int i = 0; i < t.size(); i++) {
                            bean = new ScheduleBean();
                            bean.setTitle(t.get(i).getTitle());
                            bean.setPhoto(t.get(i).getPhoto());
                            listBean.add(bean);
                        }
                        return listBean;
                    }
                });

这个时候Schedule就得到了适配数据源,再add到activity中就实现了功能,是不是很赞!

再梳理下,在这个适配器模式中,数据A就相当于我们new ScheduleAdapter()时传入的flowBean,数据B就是getView()所返回的数据模型 List<ScheduleBean>,那这个就是我在项目中使用的适配器模式。当然,适配器模式是分两种,一种是类适配器,还有一种是对象适配器,我这个应该属于对象适配器,后面会再写类适配器!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值