Android官方DataBinding(三):RecyclerView 使用ViewDataBinding更新数据

Android官方DataBinding(三):RecyclerView 使用ViewDataBinding更新数据

本例基于Android官方DataBinding,在RecyclerView上实现一个简单需求:点击一个button按钮,增加一个数据元素,并更新的view上。

(1)首先写一个布局,这个布局上面放一个按钮button,下面一个标准Android RecyclerView。Button按钮的实现一个简单的功能:没点击一次就增加一个数据元素,然后更新到RecyclerView。recycler_view_layout.xml:




    


(2)和附录文章1,2,写数据模型User,对比这个User和附录1,2的User建模时候的异同,User.java:
package zhangphil.test;

import android.databinding.BaseObservable;
//import android.databinding.Bindable;

/**
 * Created by Phil on 2017/8/17.
 */

public class User extends BaseObservable {
    private String id;
    private String name;
    private String blog;

    public void setId(String id) {
        this.id = id;
        //notifyPropertyChanged(BR.id);
    }

    //@Bindable
    public String getId() {
        return this.id;
    }


    public void setName(String name) {
        this.name = name;
        //notifyPropertyChanged(BR.name);
    }

    //@Bindable
    public String getName() {
        return this.name;
    }

    public void setBlog(String blog) {
        this.blog = blog;
        //notifyPropertyChanged(BR.blog);
    }

    //@Bindable
    public String getBlog() {
        return this.blog;
    }
}


(3)因为是一个RecyclerView,RecyclerView需要Adapter,在Adapter中需要一个布局layout,为User的数据找到View,Adapter需要的子view布局这里将写View和Model的绑定代码,item.xml:



    

        
    

    

        

        

        

        
    


(4)上层Java代码,注意观察ViewHolder的写法。比较关键的是在创建ViewHolder时候传入的ViewDataBinding。在Adapter的onBindViewHolder里面,仅需两行代码就实现数据模型和View的绑定,MainActivity.java:
package zhangphil.test;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import static zhangphil.test.BR.user;

public class MainActivity extends AppCompatActivity {
    private int index = 0;

    private ItemAdapter mItemAdapter;
    private ArrayList mItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_view_layout);

        mItems = new ArrayList();
        for (int i = 0; i < 1; i++) {
            User u = new User();
            u.setId(index + "");
            u.setName("zhangphil @" + index);
            u.setBlog("blog.csdn.net/zhangphil @" + index);

            mItems.add(u);

            index++;
        }

        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mItemAdapter = new ItemAdapter();
        mRecyclerView.setAdapter(mItemAdapter);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User u = new User();
                u.setId(index + "");
                u.setName("zhangphil @" + index);
                u.setBlog("blog.csdn.net/zhangphil @" + index);

                mItems.add(u);
                mItemAdapter.notifyDataSetChanged();

                index++;
            }
        });
    }


    private class ItemAdapter extends RecyclerView.Adapter {

        @Override
        public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.item, viewGroup, false);
            ItemViewHolder holder = new ItemViewHolder(binding);
            return holder;
        }

        @Override
        public void onBindViewHolder(ItemViewHolder viewHolder, int i) {
            viewHolder.getBinding().setVariable(user, mItems.get(i));
            viewHolder.getBinding().executePendingBindings();
        }

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

    private class ItemViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding binding;

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

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }

        public ViewDataBinding getBinding() {
            return this.binding;
        }
    }
}


代码运行结果,每点击一次button,数据增加一个,更新:

Android官方DataBinding(三):RecyclerView 使用ViewDataBinding更新数据_第1张图片



附录:
1,《Android官方DataBinding简例(一)》链接:http://blog.csdn.net/zhangphil/article/details/77322530  
2,《Android官方DataBinding(二):动态数据更新notifyPropertyChanged》链接:http://blog.csdn.net/zhangphil/article/details/77328688 

你可能感兴趣的:(Android,Android)