android MVVM开发框架——(2)DataBinding 应用于ListView

android MVVM开发框架——(1)DataBinding 基础应用
附上mvvm中databinding的基本应用链接,只有找到了基本用法才能继续进阶

1,listview布局


<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        
        <import type="com.lh.mvvm.adapter.ListViewAdapter" />

        <variable
            name="adapter"
            type="ListViewAdapter" />
    data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:adapter="@{adapter}" />
    LinearLayout>
layout>

注意,这里面有个自定义的adapter,这里先把布局写出来,adapter后面再写

2,item布局


<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        
        <import type="com.lh.mvvm.observable.UserBean" />

        <import
            alias="mUtils"
            type="com.lh.mvvm.utils.MvvmUtils" />

        <variable
            name="user"
            type="UserBean" />

    data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{`编号:` + user.userId }" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{`姓名:` +user.userName}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{`年龄:` +user.userAge}" />

        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{`性别:` +mUtils.getUserSex(user.userSex)}" />

        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{`性别:` + (user.userSex == 1f ? `男` : `女`)}" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:orientation="vertical">

            <Button
                android:id="@+id/btn_update"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="修改数据" />

            <Button
                android:id="@+id/btn_delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="删除数据" />
        LinearLayout>
    LinearLayout>
layout>

3,自定义adapter

public class ListViewAdapter extends BaseAdapter {
    //数据
    private List list;

    public ListViewAdapter(List list) {
        this.list = list;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //这个databinding也是根据布局文件item_mvvm而命名的
        ItemMvvmBinding itemMvvmBinding;
        if (convertView == null) {
            //创建一个databinding
            itemMvvmBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_mvvm, parent, false);
            //获取convertView
            convertView = itemMvvmBinding.getRoot();
        } else {
            //去除convertView中bangding的dataBinding
            itemMvvmBinding = DataBindingUtil.getBinding(convertView);
        }
        UserBean userBean = list.get(position);
        //绑定数据,这里的BR.user根据item布局文件中的变量声明来决定
        itemMvvmBinding.setVariable(BR.user, userBean);
        itemMvvmBinding.btnUpdate.setOnClickListener(new OnBtnClickListener(1, userBean));
        itemMvvmBinding.btnDelete.setOnClickListener(new OnBtnClickListener(2, position));
        return convertView;
    }

    private class OnBtnClickListener implements View.OnClickListener {
        private int stats;//1,修改;2,删除
        private UserBean userBean;
        private int position;

        OnBtnClickListener(int stats, UserBean userBean) {
            this.stats = stats;
            this.userBean = userBean;
        }

        OnBtnClickListener(int stats, int position) {
            this.stats = stats;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (stats) {
                case 1:
                    userBean.userName.set("修改后的名字");
                    break;
                case 2:
                    list.remove(position);
                    notifyDataSetChanged();
                    break;
            }
        }
    }
}

可以看出,这里并没有重用view的代码了,这就是mvvm中的一大好处

4,activity运行

public class ListViewActivity extends Activity {

    //相对应xml布局文件名
    ActivityListViewBinding activityListViewBinding;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityListViewBinding = DataBindingUtil.setContentView(this, R.layout.activity_list_view);
        //初始化一个listview的adapter
        ListViewAdapter listViewAdapter = new ListViewAdapter(initData());
        //绑定adapter
        activityListViewBinding.setAdapter(listViewAdapter);
        //通知adapter刷新数据
        listViewAdapter.notifyDataSetChanged();
    }

    //初始化测试数据
    private List initData() {
        List list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            UserBean userBean = new UserBean();
            userBean.userId.set(i);
            userBean.userName.set(i + "aaa");
            userBean.userAge.set(18 + i);
            userBean.userSex.set(i % 2 == 0 ? 1 : 0);
            list.add(userBean);
        }
        return list;
    }
}

listview与databinding的使用基本就是这样,一些更复杂的功能可以在此基础上继续深入研究

Demo下载

你可能感兴趣的:(Android-应用技术)