云代码会员 - 云代码空间
——
package com.addaegg.androidwork4.adapter; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.Intent; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.addaegg.androidwork4.R; import com.addaegg.androidwork4.activity.MainActivity; import com.addaegg.androidwork4.activity.ProInfoActivity; import com.addaegg.androidwork4.activity.StoreActivity; import com.addaegg.androidwork4.entity.CartProductInfo; import com.addaegg.androidwork4.entity.ProductInfo; /** * 2列ListView的适配器 * @author tongleer.com * */ public class MyAdapter extends BaseAdapter{ protected Context context; protected LayoutInflater inflater; protected int resource; protected ArrayList<CartProductInfo> list; public MyAdapter(Context context, int resource, ArrayList<CartProductInfo> list){ inflater = LayoutInflater.from(context); this.context = context; this.resource = resource; if(list==null){ this.list=new ArrayList<CartProductInfo>(); }else{ this.list = list; } } @Override public int getCount() { if(list.size()%2>0) { return list.size()/2+1; } else { return list.size()/2; } } @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) { ViewHolder vh = null; if (convertView == null ) { convertView = inflater.inflate(resource, null); vh = new ViewHolder(); vh.list_1=(LinearLayout) convertView.findViewById(R.id.list_1); vh.list_2=(LinearLayout) convertView.findViewById(R.id.list_2); vh.item1_img=(ImageView) convertView.findViewById(R.id.item1_img); vh.item1_info=(TextView) convertView.findViewById(R.id.item1_info); vh.item1_price=(TextView) convertView.findViewById(R.id.item1_price); vh.item1_num=(TextView) convertView.findViewById(R.id.item1_num); vh.item2_img=(ImageView) convertView.findViewById(R.id.item2_img); vh.item2_info=(TextView) convertView.findViewById(R.id.item2_info); vh.item2_price=(TextView) convertView.findViewById(R.id.item2_price); vh.item2_num=(TextView) convertView.findViewById(R.id.item2_num); convertView.setTag(vh); }else { vh = (ViewHolder)convertView.getTag(); } int distance = list.size() - position*2; int cellCount = distance >= 2? 2:distance; final List<CartProductInfo> itemList = list.subList(position*2,position*2+cellCount); if (itemList.size() >0) { vh.item1_img.setImageResource(itemList.get(0).getImg()); vh.item1_info.setText(itemList.get(0).getInfo()); Log.e("getPeopleNum","¥"+itemList.get(0).getOnePrice()+"券后价"); vh.item1_price.setText("¥"+itemList.get(0).getOnePrice()+"券后价"); vh.item1_num.setText(itemList.get(0).getPeopleNum()+"人付款"); vh.list_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent= new Intent(context, ProInfoActivity.class); CartProductInfo cartProductInfo = new CartProductInfo( itemList.get(0).getImg(), itemList.get(0).getName(), itemList.get(0).getInfo(), itemList.get(0).getNum(), itemList.get(0).getOnePrice(), itemList.get(0).getAllPrice(), itemList.get(0).getPeopleNum()); intent.putExtra("product",cartProductInfo); context.startActivity(intent); } }); if (itemList.size() >1){ vh.list_2.setVisibility(View.VISIBLE); vh.item2_img.setImageResource(itemList.get(1).getImg()); vh.item2_info.setText(itemList.get(1).getInfo()); vh.item2_price.setText("¥"+itemList.get(1).getOnePrice()+"券后价"); vh.item2_num.setText(itemList.get(1).getPeopleNum()+"人付款"); vh.list_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent= new Intent(context, ProInfoActivity.class); CartProductInfo cartProductInfo = new CartProductInfo( itemList.get(1).getImg(), itemList.get(1).getName(), itemList.get(1).getInfo(), itemList.get(1).getNum(), itemList.get(1).getOnePrice(), itemList.get(1).getAllPrice(), itemList.get(1).getPeopleNum()); intent.putExtra("product",cartProductInfo); context.startActivity(intent); } }); }else{ vh.list_2.setVisibility(View.INVISIBLE); } } return convertView; } /** * 封装ListView中item控件以优化ListView * @author tongleer * */ public static class ViewHolder{ ImageView item1_img,item2_img; TextView item1_info,item1_price,item1_num,item2_info,item2_price,item2_num; LinearLayout list_1,list_2; //TextView tv2; } }