android 开发之 ListView 与Adapter 应用实践

在开发android中,ListView 的应用显得非常频繁,只要需要显示列表展示的应用,可以说是必不可少,下面是记录开发中应用到ListView与Adapter 使用的实例:

ListView 所在页面中的布局(listview_item.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true">
     <ListView
             android:id="@+id/listview_item"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:divider="@null"
             android:scrollbars="none">
</LinearLayout>

 Adapter 展示的布局 (item.xml):

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

    <LinearLayout
        android:id="@+id/layout_sub_item_content"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <ImageView
            android:id="@+id/img_choose"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="10dp"
            android:src="@drawable/xuanzhong_icon"
            android:layout_gravity="center_vertical"
            />
        <TextView
            android:id="@+id/textview_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:gravity="center_vertical"
            android:fontFamily="noto-sans-hans-regular"
            android:textSize="14sp"
            android:textColor="#333333"
            android:text="项目名称"
            android:paddingLeft="5dp"/>

        <TextView
            android:id="@+id/textview_price"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:fontFamily="noto-sans-hans-regular"
            android:textSize="14sp"
            android:textColor="#333333"
            android:text="454.0"
            android:gravity="center"/>

        <TextView
            android:id="@+id/textview_vip"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:fontFamily="noto-sans-hans-regular"
            android:textSize="14sp"
            android:text="非会员价"
            android:gravity="center"
            android:textColor="@color/red"/>


    </LinearLayout>
</LinearLayout>

  activity_item.java:

/**
 * 项目列表适配器
 */
private MyBaseAdapt itemInfoAdapt;

@InjectView(R.id.listview_item)
ListView listview_item;

private class ViewHolder {
    TextView textview_name;
    TextView textview_price;
    TextView textview_vip;
    ImageView img_choose;
}
/**
*项目数据List
*/
private List<ItemInfo> listItemInfo=new ArrayList<>();

/**
 * 初始化列表
 */
private void initItemListView() {
itemInfoAdapt = new MyBaseAdapt<ItemInfo>(this, R.layout.item, listItemInfo) {
            @Override
            public View myAdapt(int position, View convertView, View mView, List<ItemInfo> mEntityList) {
                ViewHolder holder = null;
                if (convertView == null) {
                    convertView = mView;
                    holder = new ViewHolder();
                    holder.textview_name = (TextView) convertView.findViewById(R.id.textview_name);
                    holder.textview_price = (TextView) convertView.findViewById(R.id.textview_price);
                    holder.textview_vip = (TextView) convertView.findViewById(R.id.textview_vip);
                    holder.img_choose=(ImageView) convertView.findViewById(R.id.img_choose);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                ItemInfo itemInfo = mEntityList.get(position);
                holder.textview_name.setText(itemInfo.getName());
                holder.textview_price.setText(PriceManager.getPrice(itemInfo.getPriceStr(), itemInfo.getPriceTypeStr()).get(Common.PRICE_TYPE_ORIGIN));
                holder.textview_vip.setText(PriceManager.getPrice(itemInfo.getPriceStr(), itemInfo.getPriceTypeStr()).get(Common.PRICE_TYPE_PIONEER));
return convertView; } }; listview_item.setAdapter(itemInfoAdapt); }

 

android 开发之 ListView 与Adapter 应用实践

全文结束