android mvvm databinding在listview中的使用

自定义adapter


/**
 * Created by zhaoxiaoyu on 2222/10/18 0018.
 */

public class BusinessDetailAdapter extends BaseAdapter {
    private List<BusinessDetail> businessDetails;
    private Context context;
    private int variableId;

    public BusinessDetailAdapter(List<BusinessDetail> businessDetails, Context context, int variableId) {
        this.businessDetails = businessDetails;
        this.context = context;
        this.variableId = variableId;
    }

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

    @Override
    public Object getItem(int i) {
        return businessDetails.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ItemBusinessDetailBinding itemBusinessDetailBinding=null;
        if(view==null){
            itemBusinessDetailBinding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_business_detail,viewGroup,false);
        }else {
            itemBusinessDetailBinding=DataBindingUtil.getBinding(view);
        }
        if(businessDetails.get(i).getTYPE().equals("消费")){
            itemBusinessDetailBinding.imgType.setBackgroundResource(R.drawable.ico_pay);
        }else if(businessDetails.get(i).getTYPE().equals("撤销")){
            itemBusinessDetailBinding.imgType.setBackgroundResource(R.drawable.ico_cancle);
        }else {
            itemBusinessDetailBinding.imgType.setBackgroundResource(R.drawable.ico_reject);
        }
        itemBusinessDetailBinding.setVariable(variableId,businessDetails.get(i));
        return itemBusinessDetailBinding.getRoot();
    }
}

布局item

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="businessDetail"
            type="com.nsspay.model.BusinessDetail" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_corner"
        android:layout_margin="20dp"
        android:orientation="horizontal"
        android:layout_gravity="center">
        <LinearLayout
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/img_type"
                android:layout_width="70dp"
                android:layout_height="70dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textSize="28dp"
                android:text="@{businessDetail.TYPE}"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_weight="1"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="时间:"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{businessDetail.DATE}"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="卡号:"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{businessDetail.CARDNUM}"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="单号:"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{businessDetail.ORDERNUM}"/>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:padding="10dp"
            android:gravity="center"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="30dp"
                    android:text="@{businessDetail.MONEY}"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="30dp"
                    android:text="元"/>
            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textSize="28dp"
                android:text="@{businessDetail.STATUS}"/>
        </LinearLayout>
    </LinearLayout>
</layout>

调用

 businessDetailAdapter=new BusinessDetailAdapter(businessDetails,activity, BR.businessDetail);
        activityBusinessDetailBinding.lvBusinessdetail.setAdapter(businessDetailAdapter);

你可能感兴趣的:(android架构)