Android万能适配器-FSS开源框架之FssAdapter

fss_adapter提供了ListView,RecyclerView的通用适配器,它简化了ListView,RecyclerView的适配器开发。
gitHub地址:https://github.com/gongbox/fss

不使用databinding(数据绑定)时

假设列表布局如下:




    


ListView

1,使用 BaseAdapter(普通用法)

class Adapter extends BaseAdapter {

    public Adapter(Context context, List datas) {
        super(context, datas, R.layout.layout_list_item);
    }

    @Override
    protected void setView(ViewHolder holder, String s, int position) {
        holder.tvText.setText(s);
    }

    static class ViewHolder extends BaseViewHolder {
        TextView tvText;

        public ViewHolder(View view) {
            super(view);
            this.tvText = findViewById(R.id.tv_text);
        }
    }
}

也可以使用BindView绑定视图(需要集成fss_bind),如下:

class BindAdapter extends BaseAdapter {

    public BindAdapter(Context context, List datas) {
        super(context, datas, R.layout.layout_list_item);
    }

    @Override
    protected void setView(BindViewHolder holder, String s, int position) {
        holder.tvText.setText(s);
    }

    class BindViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_text)
        TextView tvText;

        public BindViewHolder(View itemView) {
            super(itemView);
        }
    }
}

2,使用 CommonAdapter(简洁用法)

CommonAdapter adapter = new CommonAdapter(this, Arrays.asList("1", "2", "3"), R.layout.layout_list_item) {
            @Override
            protected void setView(CommonViewHolder holder, String str, int position) {
                TextView textView = holder.getView(R.id.tv_text);
                textView.setText(str);
                //可以用holder.setText(R.id.tv_text, str);代替
            }
        };

RecyclerView

1,使用 BaseAdapter (普通用法)

class Adapter extends BaseAdapter {

    public Adapter(Context context, List datas) {
        super(context, datas, R.layout.layout_list_item);
    }

    @Override
    public void onBindView(@NonNull ViewHolder holder, String s, int position) {
        super.onBindView(holder, s, position);
        holder.tvText.setText(s);
    }

    class ViewHolder extends BaseViewHolder {
        TextView tvText;

        public ViewHolder(View view) {
            super(view);
            this.tvText = findViewById(R.id.tv_text);
        }
    }
}

也可以使用BindView绑定视图,如下:

class BindAdapter extends BaseAdapter {

    public BindAdapter(Context context, List datas) {
        super(context, datas, R.layout.layout_list_item);
    }

    @Override
    public void onBindView(@NonNull BindViewHolder holder, String s, int position) {
        super.onBindView(holder, s, position);
        holder.tvText.setText(s);
    }

    class BindViewHolder extends BaseViewHolder {
        @BindView(id = R.id.tv_text)
        TextView tvText;

        public BindViewHolder(View view) {
            super(view);
        }
    }
}

2,使用 CommonAdapter (简洁用法)

CommonAdapter adapter = new CommonAdapter(this, Arrays.asList("1", "2", "3"), R.layout.layout_list_item) {
            @Override
            public void onBindView(@NonNull CommonViewHolder holder, String str, int position) {
                super.onBindView(holder, str, position);
                TextView textView = holder.getView(R.id.tv_text);
                textView.setText(str);
                //可以用holder.setText(R.id.tv_text, str);代替
            }
        };

使用了databinding(数据绑定)时

假设对应的布局如下




    

        

    

    

        

    

ListView(使用databinding)

1,使用 BaseBindingAdapter
支持多个类型的layout,注意构造参数中需要传入databinding变量对应的id

class BindingAdapter extends BaseBindingAdapter {

    public BindingAdapter(Context context, List datas) {
        super(context, datas, BR.value, R.layout.layout_binding_list_item);
    }

    @Override
    protected void onBindView(ViewDataBinding viewDataBinding, String s, int position) {
        super.onBindView(viewDataBinding, s, position);
    }
}

2,使用 BaseBindingSingleAdapter
支持单个类型的layout,注意构造参数中需要传入databinding变量对应的id

class BindingSingleAdapter extends BaseBindingSingleAdapter {

    public BindingSingleAdapter(Context context, List datas) {
        super(context, datas, BR.value, R.layout.layout_binding_list_item);
    }

    @Override
    protected void onBindView(LayoutBindingListItemBinding viewDataBinding, String str, int position) {
        super.onBindView(viewDataBinding, str, position);
    }
}

RecyclerView (使用databinding)

1,使用 BaseBindingAdapter
支持多个类型的layout,注意构造参数中需要传入databinding变量对应的id

class BindingAdapter extends BaseBindingAdapter {

    public BindingAdapter(Context context, List datas) {
        super(context, datas, BR.value, R.layout.layout_binding_list_item);
    }

    @Override
    public void onBindView(@NonNull ViewDataBinding viewDataBinding, String str, int position) {
        super.onBindView(viewDataBinding, str, position);
    }
}

2,使用 BaseBindingSingleAdapter
支持单个类型的layout,注意构造参数中需要传入databinding变量对应的id

class BindingSingleAdapter extends BaseBindingSingleAdapter {

    public BindingSingleAdapter(Context context, List datas) {
        super(context, datas, BR.value, R.layout.layout_binding_list_item);
    }

    @Override
    public void onBindView(@NonNull LayoutBindingListItemBinding viewDataBinding, String str, int position) {
        super.onBindView(viewDataBinding, str, position);
    }
}

更多FSS框架:

  • Android开发利器-FSS开源框架之fss_common
  • Android开发利器-FSS开源框架之方法运行优先级
  • Android开发利器-FSS开源框架之绑定
  • Android开发利器-FSS开源框架之ListView,RecyclerView适配器

如果想要了解fss框架的具体使用,我为大家提供了demo工程

  • Demo

你可能感兴趣的:(Android万能适配器-FSS开源框架之FssAdapter)