基于DataBinding的基类(Activity、Fragment、Adapter)

基于DataBinding的基类(Activity、Fragment、Adapter)

1、BaseBindActivity

public abstract class BaseBindActivity extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DB db = DataBindingUtil.setContentView(this, getLayoutId());
        initView(db);
    }

    /**
     * 获取资源ID
     *
     * @return 布局资源ID
     */
    protected abstract int getLayoutId();

    /**
     * 初始化界面
     * @param bindView 界面绑定对象
     */
    protected abstract void initView(DB bindView);
}

布局文件示例



    
        
    
    
        
    

简单调用

public class MainActivity extends BaseBindActivity {
    @Override
    protected int getLayoutId() {
        return R.layout.acitivty_main;
    }

    @Override
    protected void initView(final AcitivtyMainBinding bindView) {
    	bindView.setExample("示例");
	}
}

2、BaseBindFragment

public abstract class BaseBindFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        DB db = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        initView(db);
        return db.getRoot();
    }

    /**
     * 获取资源ID
     *
     * @return 布局资源ID
     */
    protected abstract int getLayoutId();

    /**
     * 初始化界面
     * @param bindView 界面绑定对象
     */
    protected abstract void initView(DB bindView);
}

3、BaseBindAdapter

①继承RecycleView.Adapter

public abstract class BaseBindAdapter extends RecyclerView.Adapter {

    private List list;
    private int resourceId;

    public BaseBindAdapter(List list, int resourceId) {
        this.list = list;
        this.resourceId = resourceId;
        if (this.list == null) {
            this.list = new ArrayList();
        }
    }

    @NonNull
    @Override
    public BaseBindAdapter.BindViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        DB db = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), resourceId, parent, false);
        BindViewHolder holder = new BindViewHolder(db.getRoot());
        holder.bindView = db;
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseBindAdapter.BindViewHolder holder, int position) {

    }

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

    public class BindViewHolder extends RecyclerView.ViewHolder {

        public DB bindView;

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

②继承PagerAdapter

public abstract class BaseBindPagerAdapter extends PagerAdapter {
    private LinkedHashMap map;
    private LinkedHashMap views;
    private Map temp;

    public BaseBindPagerAdapter(LinkedHashMap map) {
        this.map = map;
        if(map == null) {
            map = new LinkedHashMap<>(0);
        }
        views = new LinkedHashMap<>(map.size());
        temp = new LinkedHashMap<>(map.size());
        Iterator> iterator = map.entrySet().iterator();
        int position = 0;
        while (iterator.hasNext()) {
            Map.Entry next = iterator.next();
            temp.put(position++, next.getKey());
        }
    }

    @Override
    public int getCount() {
        return map.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        DB binding = DataBindingUtil.inflate(LayoutInflater.from(container.getContext()), getResourceId(), container, false);
        BindViewHolder holder = new BindViewHolder();
        holder.bindView = binding;
        initView(holder, temp.get(position));
        views.put(temp.get(position), binding.getRoot());
        container.addView(binding.getRoot());
        return binding.getRoot();
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(views.get(temp.get(position)));
    }

    protected class BindViewHolder {
        public DB bindView;
    }

    /**
     * 获取资源ID
     *
     * @return 布局资源ID
     */
    public abstract int getResourceId();

    /**
     * 初始化界面
     * @param holder 界面句柄
     * @param key 子项对应的键
     * @param value 子项实例
     * */
    public abstract void initView(BindViewHolder holder, K key, V value);
}

你可能感兴趣的:(Android)