protected LayoutManager() {
}
public interface LayoutManagerFactory {
RecyclerView.LayoutManager create(RecyclerView recyclerView);
}
public static LayoutManagerFactory linear() {
return new LayoutManagerFactory() {
@Override
public RecyclerView.LayoutManager create(RecyclerView recyclerView) {
return new LinearLayoutManager(recyclerView.getContext());
}
};
}
public static LayoutManagerFactory linear(@Orientation final int orientation, final boolean reverseLayout) {
return new LayoutManagerFactory() {
@Override
public RecyclerView.LayoutManager create(RecyclerView recyclerView) {
return new LinearLayoutManager(recyclerView.getContext(), orientation, reverseLayout);
}
};
}
public static LayoutManagerFactory grid(final int spanCount) {
return new LayoutManagerFactory() {
@Override
public RecyclerView.LayoutManager create(RecyclerView recyclerView) {
return new GridLayoutManager(recyclerView.getContext(), spanCount);
}
};
}
public static LayoutManagerFactory grid(final int spanCount, @Orientation final int orientation, final boolean reverseLayout) {
return new LayoutManagerFactory() {
@Override
public RecyclerView.LayoutManager create(RecyclerView recyclerView) {
return new GridLayoutManager(recyclerView.getContext(), spanCount, orientation, reverseLayout);
}
};
}
public static LayoutManagerFactory staggeredGrid(final int spanCount, @Orientation final int orientation) {
return new LayoutManagerFactory() {
@Override
public RecyclerView.LayoutManager create(RecyclerView recyclerView) {
return new StaggeredGridLayoutManager(spanCount, orientation);
}
};
}
@IntDef({LinearLayoutManager.HORIZONTAL, LinearLayoutManager.VERTICAL})
@Retention(RetentionPolicy.SOURCE)
public @interface Orientation {
}
}
2.ItemViewArg:这个类用于提供每个item界面的xml的id和BR中绑定实例的id
public class ItemViewArg {
public static ItemViewArg of(ItemView itemView) {
return new ItemViewArg<>(itemView);
}
public static ItemViewArg of(ItemViewSelector selector) {
return new ItemViewArg<>(selector);
}
private final ItemView itemView;
private final ItemViewSelector selector;
private ItemViewArg(ItemView itemView) {
this.itemView = itemView;
this.selector = new ItemViewSelector() {
@Override
public void select(ItemView itemView, int position, T item) {
}
@Override
public int viewTypeCount() {
return 1;
}
};
}
private ItemViewArg(ItemViewSelector selector) {
this.itemView = new ItemView();
this.selector = selector;
}
public void select(int position, T item) {
selector.select(itemView, position, item);
}
public int bindingVariable() {
return itemView.bindingVariable();
}
public int layoutRes() {
return itemView.layoutRes();
}
public int viewTypeCount() {
return selector.viewTypeCount();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ItemViewArg> that = (ItemViewArg>) o;
if (!itemView.equals(that.itemView)) return false;
return selector == that.selector;
}
@Override
public int hashCode() {
int result = itemView.hashCode();
result = 31 * result + selector.hashCode();
return result;
}
public interface ItemViewSelector {
void select(ItemView itemView, int position, T item);
int viewTypeCount();
}
public static class ItemView {
public static final int BINDING_VARIABLE_NONE = 0;
private int bindingVariable;
@LayoutRes
private int layoutRes;
public static ItemView of(int bindingVariable, @LayoutRes int layoutRes) {
return new ItemView().setBindingVariable(bindingVariable).setLayoutRes(layoutRes);
}
public ItemView set(int bindingVariable, @LayoutRes int layoutRes) {
this.bindingVariable = bindingVariable;
this.layoutRes = layoutRes;
return this;
}
public ItemView setBindingVariable(int bindingVariable) {
this.bindingVariable = bindingVariable;
return this;
}
public ItemView setLayoutRes(@LayoutRes int layoutRes) {
this.layoutRes = layoutRes;
return this;
}
public int bindingVariable() {
return bindingVariable;
}
@LayoutRes
public int layoutRes() {
return layoutRes;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ItemView itemView = (ItemView) o;
if (bindingVariable != itemView.bindingVariable) return false;
return layoutRes == itemView.layoutRes;
}
@Override
public int hashCode() {
int result = bindingVariable;
result = 31 * result + layoutRes;
return result;
}
}
}
这个类中有一个内部累和一个内部接口,接下来我将来介绍这个类。
- 1.先来看看内部类ItemView:该类有两个int字段,bindingVariable储存实例在BR中生成的id,layoutRes储存所需xml文件的id。其他的几个方法都是为这两个字段服务的,比较简单,大家看看应该就了解了。
- 2.再来看看内部接口ItemViewSelector:我们前面的内部类,只能提供一个布局给item,如果我的item有许多个布局怎么办的,这个接口就是用来解决这个问题。
- 1.select(ItemView itemView, int position, T item):这个方法是开发者自己实现的,item这个字段是咱么列表Item的ViewModel,里面储存着到底当前列表item的布局type。所以我们只要用判断item中布局的种类,然后调用itemView的of或者set方法,就能对列表item的布局进行选择。**其实总的说就是根据字段item中的参数来构造出不同的ItemView**
- 2.viewTypeCount():这个方法很简单,就是返回一下列表item所有布局的种类。
- 3.最后看看ItemViewArg:该类中储存着ItemView和ItemViewSelector的实体,我们在开发的时候只要传入一个实例另一个会自动生成。
- 这其中最重要的一个方法就是select(int position, T item),大家先记着这个方法,在后面我们整个流程串起来了之后,会讲到这个方法。
public class BindingAdapters {
@SuppressWarnings("unchecked")
@BindingAdapter(value = {"itemView", "items", "itemIds","itemAnimator","itemDecor"}, requireAll = false)
public static void setAdapter(final RecyclerView recyclerView, ItemViewArg arg, final List items, BindingRecyclerViewAdapter.ItemIds itemIds,RecyclerView.ItemAnimator animator,RecyclerView.ItemDecoration decor) {
if (arg == null) {
throw new IllegalArgumentException("itemView must not be null");
}
BindingRecyclerViewAdapter adapter = new BindingRecyclerViewAdapter<>(arg);
if (items!=null)adapter.setItems(items);
if (itemIds!=null)adapter.setItemIds(itemIds);
if (animator!=null)recyclerView.setItemAnimator(animator);
if (decor!=null)recyclerView.addItemDecoration(decor);
recyclerView.setAdapter(adapter);
}
@BindingAdapter("layoutManager")
public static void setLayoutManager(RecyclerView recyclerView, LayoutManager.LayoutManagerFactory layoutManagerFactory) {
recyclerView.setLayoutManager(layoutManagerFactory.create(recyclerView));
}
@BindingConversion
public static ItemViewArg toItemViewArg(ItemViewArg.ItemView itemView) {
return ItemViewArg.of(itemView);
}
@BindingConversion
public static ItemViewArg toItemViewArg(ItemViewArg.ItemViewSelector> selector) {
return ItemViewArg.of(selector);
}
}
setItems(@Nullable List items) 这个方法是我们在BindingAdapters中初始化Adapter的时候用的,通过代码大家可以看出,其实就是判断设置的List数据源的类型是否是ObservableList,如果是的话就设置我们前面说到的监听器,设置了之后我们的RecycleView就能自动增加和删除了。
public class MainActivityViewModel {
public MainActivity baseActivity;
public ItemViewArg.ItemViewSelector mViewSelector;
public final ObservableList dataItems= new ObservableArrayList<>();
public String positionStr;
public MainActivityViewModel(MainActivity b) {
baseActivity=b;
mViewSelector=new ItemViewArg.ItemViewSelector() {
@Override
public void select(ItemViewArg.ItemView itemView, int position, MainActivityItemViewModel item) {
itemView.set(com.example.administrator.mvvmrecycleview.BR.viewModel,item.type==1?R.layout.item_one:R.layout.item_two);
}
@Override
public int viewTypeCount() {
return 2;
}
};
for (int i = 0; i < 20; i++) {
if (i%2==1){
dataItems.add(new MainActivityItemViewModel(b,1,String .valueOf(i)));
}else {
dataItems.add(new MainActivityItemViewModel(b,2,String .valueOf(i)));
}
}
}
public void add(){
if (TextUtils.isEmpty(positionStr)||dataItems.size() text=new ObservableField<>();
public MainActivityItemViewModel(MainActivity baseActivity, int type, String text) {
mBaseActivity = baseActivity;
this.type = type;
this.text.set(text);
}
public View.OnClickListener click=new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mBaseActivity, ("你点击了"+text.get()), Toast.LENGTH_SHORT).show();
}
};
}
}
网上关于SQL优化的教程很多,但是比较杂乱。近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充。
这篇文章我花费了大量的时间查找资料、修改、排版,希望大家阅读之后,感觉好的话推荐给更多的人,让更多的人看到、纠正以及补充。
1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。
2.应尽量避免在 where
Zookeeper类是Zookeeper提供给用户访问Zookeeper service的主要API,它包含了如下几个内部类
首先分析它的内部类,从WatchRegistration开始,为指定的znode path注册一个Watcher,
/**
* Register a watcher for a particular p
何为部分应用函数?
Partially applied function: A function that’s used in an expression and that misses some of its arguments.For instance, if function f has type Int => Int => Int, then f and f(1) are p