RecyclerView 和 DataBinding
2016-01-24 23:17
330 查看
1:创建fragments文件夹 2:写HomeFragment 然后先不写布局,先写网络请求。 网络请求在onCreate 里面 @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); NetworkUtil.getService().getHome().enqueue(this); } 注意interface 需要是public public interface Service{ @GET("http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=and-c57&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.44.2\n") Call<HomeEntity> getHome(); } 添加权限: <uses-permission android:name="android.permission.INTERNET"/> 调用HomeFragment, main_activity 部分,填写 <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"> activity里面放: getSupportFragmentManager().beginTransaction().replace(R.id.content,new HomeFragment()).commit(); ---------- Troubleshooting 现在有editorRecommendEntity 和hotRecommendEntity为空。 数据库里面的实体类一共是5个, AlbumEntity.java DiscoverEntity.java FocusImageEntity.java RecommendEntity.java TrackEntity.java ---------- HomeEntity里面的包含了: +++++++++++++++++++++++++++++++++++++++++++++ 注意了,实例的名字一定要和json里面的名字一样,不一样就不会获取了。 private DiscoveryColumns discoveryColumns; private RecommendEntity editorRecommendAlbums; private HotRecommendEntity hotRecommends; private FocusImages focusImages; private BulletArea bulletArea; +++++++++++++++++++++++++++++++++++++++++++++ 这个时候如果需要对数据进行修改,就是例如添加一个list这样的操作,那么需要自己一点一点修改了,就不能想刚才直接修改了。 这个时候添加一个断点来调节一下,看看网络数据有没有返回回来。 在这里添加断点: public void onResponse(Response<HomeEntity> response) { Toast.makeText(getContext(),"返回",Toast.LENGTH_LONG).show(); } --------------------------- 开始写布局: 现在需要做解耦的操作: 先写一个recyclerView 然后添加自定义属性: xmlns:app="http://schemas.android.com/apk/res-auto" <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" app:layoutManager="LinearLayoutManager" android:id="@+id/home_recycler"/> 使用databinding来做,会比较顺畅一点 在项目的build.gradle 里面添加: dataBinding{ enabled true } 效果如下: android { compileSdkVersion 23 buildToolsVersion "23.0.2" dataBinding{ enabled true } defaultConfig { applicationId "com.kodulf.ximalayating" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } ------------------------------ 做完上面的步骤记得要sync now。不然得不到ViewDataBinding这个类。 ------------------------------ ----------------------------- 其实databiding 就是减少了findViewById的了,因为findViewById实在是太慢了? ----------------------------- 现在需要用databiding来做adapter。这个要考虑多布局复用。 ----------------------------- 先去写一个多布局复用的adapter 新建JAVA class。adapters.BindingAdapter,这个就顺便把adapters的目录也新建了 不同的数据要展示在不同的view上,不同的view 又是不同的id。 ++++++++++++++++++->这些就是在onCreateViewHolder里面填写。 那么现在需要建立一个类去记录这些东西。 public static class BindingTool{ private int layoutId; private int variableId; public BindingTool(int layoutId, int variable) { this.layoutId = layoutId; this.variableId = variableId; } 建立ViewHolder public static class BindingViewHolder extends RecyclerView.ViewHolder{ //Base class for generated data binding classes. If possible, the generated binding should be instantiated using one of its generated static bind or inflate methods. If the specific binding is unknown, DataBindingUtil.bind(View) or DataBindingUtil.inflate(LayoutInflater, int, ViewGroup, boolean) should be used. private final ViewDataBinding binding; public BindingViewHolder(ViewDataBinding binding) {//----〉注意这里是ViewDataBinding ,因为 //因为DataBindingUtil.inflate返回的类型就是ViewDataBinding. ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater, layoutId,parent, attachToParent); super(binding.getRoot()); //getRoot() //Returns the outermost View in the layout file associated with the Binding. If this binding is for a merge layout file, this will return the first root in the merge tag. //Returns: //the outermost View in the layout file associated with the Binding. this.binding = binding; } } } private Map<Callable.Type,BindingTool> map; private List<Object> list;//因为这个list里面可以放任何数据,所以使用Object 然后继承 public class BindingAdapter extends RecyclerView.Adapter<BindingAdapter.BindingTool>{ 然后实现所有的抽象方法 //由于是多布局复用,所以还需要写一个 getItemViewType()的方法 @Override public int getItemViewType(int position) { // return super.getItemViewType(position); return map.get(list.get(position).getClass()).layoutId; //把它的layoutid直接作为type返回 } @Override public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), viewType, parent, false); return new BindingViewHolder(binding); } @Override public void onBindViewHolder(BindingViewHolder holder, int position) { Object o = list.get(position); int variableId =map.get(o.getClass()).variableId; holder.binding.setVariable(variableId,o); } 添加: @Override public int getItemCount() { return list.size(); } public void add(Object o) { int size = list.size(); list.add(o); notifyItemInserted(size); } 然后会到HomeFragment里面: onCreate方法里面添加: HashMap<Callable.Type,BindingAdapter.BindingTool> m = new HashMap<>(); 先写一个专辑的布局:
public class BindingAdapter extends RecyclerView.Adapter<BindingAdapter.BindingViewHolder>{ private Map<Callable.Type,BindingTool> map; private List<Object> list;//因为这个list里面可以放任何数据,所以使用Object private Context context; public BindingAdapter(Map<Callable.Type, BindingTool> map, List<Object> list, Context context) { this.map = map; this.list = list; this.context = context; } @Override public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), viewType, parent, false); return new BindingViewHolder(binding); } @Override public void onBindViewHolder(BindingViewHolder holder, int position) { Object o = list.get(position); int variableId =map.get(o.getClass()).variableId; holder.binding.setVariable(variableId,o); } //由于是多布局复用,所以还需要写一个 getItemViewType()的方法 @Override public int getItemViewType(int position) { // return super.getItemViewType(position); return map.get(list.get(position).getClass()).layoutId; //把它的layoutid直接作为type返回 } @Override public int getItemCount() { return list.size(); } public void add(Object o) { int size = list.size(); list.add(o); notifyItemInserted(size); } public void addAll(Collection<? extends Object> collection){ int size = list.size(); list.addAll(collection); notifyItemRangeInserted(size,collection.size()); } public static class BindingViewHolder extends RecyclerView.ViewHolder{ //Base class for generated data binding classes. If possible, the generated binding // should be instantiated using one of its generated static bind or inflate methods. If the specific binding is unknown, DataBindingUtil.bind(View) // or DataBindingUtil.inflate(LayoutInflater, int, ViewGroup, boolean) should be used. private final ViewDataBinding binding; public BindingViewHolder(ViewDataBinding binding) {//----〉注意这里是ViewDataBinding ,因为 //因为DataBindingUtil.inflate返回的类型就是ViewDataBinding. ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater, layoutId,parent, attachToParent); super(binding.getRoot()); //getRoot() //Returns the outermost View in the layout file associated with the Binding. If this binding is for a merge layout file, this will return the first root in the merge tag. //Returns:the outermost View in the layout file associated with the Binding. this.binding = binding; } } // 不同的数据要展示在不同的view上,不同的view 又是不同的id。 // 那么现在需要建立一个类去记录这些东西。 public static class BindingTool{ private int layoutId; private int variableId; public BindingTool(int layoutId, int variableId) { this.layoutId = layoutId; this.variableId = variableId; } } }
相关文章推荐
- Codeforces Round #340 (Div. 2) B. Chocolate 水题
- Efficient Graph-Based Image Segmentation解读
- 打印super_block结构中的一些域的值
- Codeforces Round #329 (Div. 2) D. Happy Tree Party(树链剖分)
- 自定义Directive使用ngModel
- 面向对象程序设计 ——第一次作业
- kuangbin_SegTree A (HDU 1166)
- BigDecimal用法详解
- 2015年大二上-数据结构-图-1-(4)-利用遍历思想求解图问题(2)
- printf的输出相关
- HDOJ 2043 密码
- POJ1833 排列
- 自创建内容提供其provider的权限问题
- 观察者模式(observer)
- Android基础_3 Activity相对布局
- A + B问题(字符串与数字之间的转换)
- OC基础(三)get/set/. @proterty和@synthesize关键字等使用
- Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树
- Selective Search for Object Recognition解读
- 线程_总结JAVA183