DataBinding 与 ListView 、RecyclerView 实现数据双向绑定

ListView

布局较简单两个Button 一个ListView


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加数据"
            />
        <Button
            android:id="@+id/btn_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="修改数据"
            />
    LinearLayout>

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    ListView>

LinearLayout>

Student对象

public class Student {
    public ObservableField name = new ObservableField<>();
    public ObservableInt age = new ObservableInt();
    public ObservableBoolean isBoy = new ObservableBoolean();
    public Student(String name,int age,boolean isBoy){
        this.name.set(name);
        this.age.set(age);
        this.isBoy.set(isBoy);
    }
}

item_student_layout.xml 内容


<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="student"
            type="com.example.databinding.Student"/>
    data>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        >
        <TextView android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:textColor="@color/colorBlack"
                  android:text="@{student.name}"/>
        <TextView android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:textColor="@color/colorBlack"
                  android:text='@{"" +student.age}'/>
        <TextView android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:textColor="@color/colorBlack"
                  android:text='@{student.isBoy?"男":"女"}'/>
    LinearLayout>
layout>

StudentAdapter

public class StudentAdapter extends BaseAdapter {
    private List studentList;
    private Context context;
    public StudentAdapter(Context context, List studentList) {
        this.studentList = studentList ;
        this.context = context ;
    }

    @Override
    public int getCount() {
        return studentList.size();
    }

    @Override
    public Student getItem(int position) {
        return studentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemStudentLayoutBinding itemStudentLayoutBinding = null;
        if (convertView == null){
            itemStudentLayoutBinding = DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.item_student_layout,parent,false);
            convertView = itemStudentLayoutBinding.getRoot();
        }else {
            itemStudentLayoutBinding = DataBindingUtil.getBinding(convertView);
        }
        itemStudentLayoutBinding.setStudent(studentList.get(position));

        return itemStudentLayoutBinding.getRoot();
    }
}

Adapter中加载布局,官方文档给的很明确
adapter.png

Avtivity中代码

public class ListViewActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView mListView;
    private List mStudentList;
    private StudentAdapter mStudentAdapter;
    private Button mAddButton;
    private Button mUpdateButton;

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

        mListView = (ListView) findViewById(R.id.list_view);
        mAddButton = (Button) findViewById(R.id.btn_add);
        mUpdateButton = (Button) findViewById(R.id.btn_update);

        mAddButton.setOnClickListener(this);
        mUpdateButton.setOnClickListener(this);

        initData();

    }

    private void initData() {
        mStudentList = new ArrayList<>();
        for (int i = 0 ; i < 5 ; i++){
            Student student = new Student("小老虎",18,true);
            mStudentList.add(student);
        }
        mStudentAdapter = new StudentAdapter(getApplicationContext(), mStudentList);
        mListView.setAdapter(mStudentAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:
                Student student = new Student("青蛙",28,false);
                mStudentList.add(student);
                mStudentAdapter.notifyDataSetChanged();
                break;
            case R.id.btn_update:
                mStudentList.get(0).name.set("河马呱呱叫");
                break;
            default:
                break;
        }
    }
}

初始化数据没什么好说的。
添加按钮中添加Student对象并刷新ListView
修改按钮中 修改Student集合中的第一个Student对象的名字为 河马呱呱叫 。
效果如下。
DataBinding 与 ListView 、RecyclerView 实现数据双向绑定_第1张图片

RecyclerView

界面不变,只是将ListView布局改为RecyclerView 。当然得依赖design包


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加数据"
            />
        <Button
            android:id="@+id/btn_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="修改数据"
            />
    LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    android.support.v7.widget.RecyclerView>

LinearLayout>

RecyclerAdapter

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private List studentList;
    private Context context;
    public RecyclerAdapter(Context context,List studentList) {
        this.studentList = studentList ;
        this.context = context ;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemStudentLayoutBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_student_layout, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.binding.setStudent(studentList.get(position));
        holder.binding.executePendingBindings();
    }


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

    public class ViewHolder extends RecyclerView.ViewHolder{

        public ItemStudentLayoutBinding binding;

        public ViewHolder(ItemStudentLayoutBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}

Activity

public class RecyclerViewActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView mRecyclerView;
    private Button mAddButton;
    private Button mUpdateButton;
    private RecyclerAdapter mRecyclerAdapter;
    private List mStudentList;

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

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);

        mAddButton = (Button) findViewById(R.id.btn_add);
        mUpdateButton = (Button) findViewById(R.id.btn_update);
        mAddButton.setOnClickListener(this);
        mUpdateButton.setOnClickListener(this);
        initData();
    }

    private void initData() {
        mStudentList = new ArrayList<>();
        for (int i = 0 ; i < 5 ; i++){
            Student student = new Student("小老虎",18,true);
            mStudentList.add(student);
        }
        mRecyclerAdapter = new RecyclerAdapter(getApplicationContext(), mStudentList);

        mRecyclerView.setAdapter(mRecyclerAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:
                Student student = new Student("青蛙",28,false);
                mStudentList.add(student);
                mRecyclerAdapter.notifyDataSetChanged();
                break;
            case R.id.btn_update:
                mStudentList.get(0).name.set("河马呱呱叫");
                break;
            default:
                break;
        }
    }
}

运行之后和ListView效果一样。只是Adapter中写法不同。
DataBinding 与 ListView 、RecyclerView 实现数据双向绑定_第2张图片

欢迎关注Android小菜菜公众号
DataBinding 与 ListView 、RecyclerView 实现数据双向绑定_第3张图片

你可能感兴趣的:(DataBinding)