re

    @BindingAdapter(value = "view_status2")
    public static void showStatusView(MultiStatusLayout2 layout, int status) {
        if (status == STATUS_CONTENT) {
            layout.showContentView();
        } else if (status == STATUS_EMPTY) {
            layout.showEmptyView();
        } else if (status == STATUS_ERROR) {
            layout.showErrorView();
        } else if (status == STATUS_LOADING) {
            layout.showLoadingView();
        } else if (status == STATUS_NO_NETWORK) {
            layout.showNoNetworkView();
        }
    }
bind:view_status2="@{monthFavouriteVM.liveData.viewStatus}"
    @Override
    public void onBindViewHolder(BindingViewHolder holder, int position) {

        DataType data = this.data.get(position);

        if (holder.viewModel == null) {
            holder.viewModel = createVM(data, holder, position);
            holder.binding.setVariable(holder.viewModel.getVariableId(), holder.viewModel);
            holder.binding.executePendingBindings();
        } else {
            holder.viewModel.loadData(data);
        }
    }
protected abstract IItemViewModel createVM(DataType data, BindingViewHolder holder, int position);
protected abstract int getLayoutResId(int viewType);
    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (viewType == VIEW_TYPE_LOADING) {
            if (loadingView == null) {
                loadingView = inflater.inflate(R.layout.layout_loading, parent, false);
            }
            return new BindingViewHolder<>(loadingView);
        } else {
            ViewDataBinding binding = DataBindingUtil.inflate(inflater, getLayoutResId(viewType), parent, false);
            return new BindingViewHolder<>(binding);
        }
    }
public class BindingViewHolder extends RecyclerView.ViewHolder {

    public final ViewDataBinding binding;

    public IItemViewModel viewModel;

    public BindingViewHolder(ViewDataBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }

    BindingViewHolder(View itemView) {
        super(itemView);
        binding = null;
    }
}
@BindingAdapter(value = {"itemLayoutId", "variableId", "dataList"}, requireAll = false)
    public static  void setAdapter(RecyclerView recyclerView, int itemLayoutId,
                                      int variableId, List dataList) {
        if (itemLayoutId < 0 || variableId < 0) {
            return;
        }
        BindingRecyclerViewAdapter adapter;
        RecyclerView.Adapter oldAdapter = recyclerView.getAdapter();
        if (oldAdapter == null || !(oldAdapter instanceof BindingRecyclerViewAdapter)) {
            adapter = new BindingRecyclerViewAdapter<>(itemLayoutId, variableId);
            recyclerView.setAdapter(adapter);
        } else {
            //noinspection unchecked
            adapter = (BindingRecyclerViewAdapter) oldAdapter;
        }
        adapter.setItems(dataList);
    }

    @BindingAdapter(value = {"itemLayoutId", "variableId", "dataList"})
    public static  void setAdapter(RecyclerView recyclerView, int itemLayoutId,
                                      int variableId, List dataList) {
        if (itemLayoutId < 0 || variableId < 0) {
            return;
        }
        BindingRecyclerViewAdapter adapter;
        RecyclerView.Adapter oldAdapter = recyclerView.getAdapter();
        if (oldAdapter == null || !(oldAdapter instanceof BindingRecyclerViewAdapter)) {
            adapter = new BindingRecyclerViewAdapter<>(itemLayoutId, variableId);
            recyclerView.setAdapter(adapter);
        } else {
            //noinspection unchecked
            adapter = (BindingRecyclerViewAdapter) oldAdapter;
        }
        adapter.setItems(dataList);
    }









你可能感兴趣的:(re)