自定义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;}@Overridepublic int getCount() {return businessDetails.size();}@Overridepublic Object getItem(int i) {return businessDetails.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic 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><variablename=\"businessDetail\"type=\"com.nsspay.model.BusinessDetail\" /></data><LinearLayoutandroid: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\"><LinearLayoutandroid: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.AppCompatImageViewandroid:id=\"@+id/img_type\"android:layout_width=\"70dp\"android:layout_height=\"70dp\"/><TextViewandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:textColor=\"@color/black\"android:textSize=\"28dp\"android:text=\"@{businessDetail.TYPE}\"/></LinearLayout><LinearLayoutandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:padding=\"10dp\"android:layout_weight=\"1\"android:orientation=\"vertical\"><LinearLayoutandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:padding=\"5dp\"android:orientation=\"horizontal\"><TextViewandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"时间:\"/><TextViewandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"@{businessDetail.DATE}\"/></LinearLayout><LinearLayoutandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:padding=\"5dp\"android:orientation=\"horizontal\"><TextViewandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"卡号:\"/><TextViewandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"@{businessDetail.CARDNUM}\"/></LinearLayout><LinearLayoutandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:padding=\"5dp\"android:orientation=\"horizontal\"><TextViewandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"单号:\"/><TextViewandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"@{businessDetail.ORDERNUM}\"/></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width=\"wrap_content\"android:layout_height=\"match_parent\"android:layout_weight=\"1\"android:padding=\"10dp\"android:gravity=\"center\"android:orientation=\"vertical\"><LinearLayoutandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:orientation=\"horizontal\"><TextViewandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:textColor=\"@color/black\"android:textSize=\"30dp\"android:text=\"@{businessDetail.MONEY}\"/><TextViewandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:textColor=\"@color/black\"android:textSize=\"30dp\"android:text=\"元\"/></LinearLayout><TextViewandroid: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);