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

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

不使用databinding(数据绑定)时

假设列表布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="15dp"
        android:paddingTop="10dp"
        android:paddingEnd="15dp"
        android:paddingBottom="10dp" />

</LinearLayout>

ListView

1,使用 BaseAdapter(普通用法)

class Adapter extends BaseAdapter<String, Adapter.ViewHolder> {

    public Adapter(Context context, List<String> 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<String, BindAdapter.BindViewHolder> {

    public BindAdapter(Context context, List<String> 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<String>(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<String, Adapter.ViewHolder> {

    public Adapter(Context context, List<String> 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<String, BindAdapter.BindViewHolder> {

    public BindAdapter(Context context, List<String> 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<String>(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(数据绑定)时

假设对应的布局如下

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="value"
            type="String" />

    </data>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="15dp"
            android:paddingTop="10dp"
            android:paddingEnd="15dp"
            android:paddingBottom="10dp"
            android:text="@{value}" />

    </LinearLayout>
</layout>

ListView(使用databinding)

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

class BindingAdapter extends BaseBindingAdapter<String> {

    public BindingAdapter(Context context, List<String> 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<String, LayoutBindingListItemBinding> {

    public BindingSingleAdapter(Context context, List<String> 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<String> {

    public BindingAdapter(Context context, List<String> 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<String, LayoutBindingListItemBinding> {

    public BindingSingleAdapter(Context context, List<String> 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框架:

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