RecyclerView 和 DataBinding

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;
    }

}

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值