[Android]PinnedSectionListView應用

1.宣告 PinnedSectionListView

private PinnedSectionListView mPinnedListView;

2.onCreat的時候,建立 PinnedSectionListView物件,再設定Adapter

mPinnedListView = (PinnedSectionListView) findViewById(R.id.pinned_listview);
mPinnedListView.setAdapter(new SimpleAdapter(this, R.layout.listitem_productlist_content_pinned, getPinnedProductList()));

3.要放入Adapter的物件,要設定兩種狀態

public class Product {
	public static final int ITEM = 0;
	public static final int SECTION = 1;
}

4.建立一個Adapter class繼承ArrayAdapter並實作PinnedSectionListAdapter
因為View有兩準狀態ITEM或SECTION,所以getViewTypeCount()要return 2
isItemViewTypePinned()設定返回要釘住的是哪一個狀態的view

	class SimpleAdapter extends ArrayAdapter<Product> implements PinnedSectionListAdapter {

		int mItemResource;
		List<Product> products;
		public SimpleAdapter(Context context, int resource,	List<Product> products) {
			super(context, resource, products);
			mItemResource = resource;
			this.products = products;
		}

		class ViewHolder{
			TextView name;
			TextView amount;
			TextView price;
			TextView tital;
			LinearLayout contentTital;
			LinearLayout content;
			TextView nameTital;
			TextView amountTital;
			TextView priceTital;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				LayoutInflater inflater = LayoutInflater.from(mContext);
				convertView = inflater.inflate(mItemResource, null);
				holder = new ViewHolder();
				holder.name = (TextView) convertView
						.findViewById(R.id.product_listitem_name_cont);
				holder.amount = (TextView) convertView
						.findViewById(R.id.product_listitem_amount_cont);
				holder.price = (TextView) convertView
						.findViewById(R.id.product_listitem_price_cont);
				holder.tital = (TextView) convertView
						.findViewById(R.id.additional_tital);
				holder.contentTital = (LinearLayout) convertView
						.findViewById(R.id.listview_tital);
				holder.content = (LinearLayout) convertView
						.findViewById(R.id.additional_content);
				holder.nameTital = (TextView) convertView
						.findViewById(R.id.product_listitem_name_content);
				holder.amountTital = (TextView) convertView
						.findViewById(R.id.product_listitem_amount_content);
				holder.priceTital = (TextView) convertView
						.findViewById(R.id.product_listitem_price_content);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			convertView.setOnClickListener(new Item_Click(position));
			if (products.get(position).getListType() == Product.SECTION) {
				holder.tital.setVisibility(View.VISIBLE);
				holder.contentTital.setVisibility(View.VISIBLE);
				holder.content.setVisibility(View.GONE);
				holder.tital.setText(products.get(position).getName());
				if (products.get(position).isAdditionalBuySelected()){
					holder.priceTital.setText(mContext.getResources().getString(R.string.additiional_price_tital));
				} else {
					holder.priceTital.setText(mContext.getResources().getString(R.string.price_tital));					
				}
			} else {
				holder.tital.setVisibility(View.GONE);
				holder.contentTital.setVisibility(View.GONE);
				holder.name.setText(products.get(position).getName());
				if (products.get(position).isAdditionalBuySelected())
					holder.amount.setText((products.get(position).getAdditionalBuySelectedIndex() + 1) + "");
				else
					holder.amount.setText("1");
				holder.price.setText(String.format(mContext
						.getString(R.string.currency_s),
						(Long.valueOf(products.get(position)
								.getPromotePrice()) * (products.get(position).getAdditionalBuySelectedIndex() + 1))));
			}
			
			return convertView;
		}
		
        @Override public int getItemViewType(int position) {
            return getItem(position).getListType();
        }

        @Override 
        public int getViewTypeCount() {
            return 2;
        }
		
        @Override
	public boolean isItemViewTypePinned(int viewType) {
		// TODO Auto-generated method stub
		return viewType == Product.SECTION;
	}
}

PinnedSectionListView下載

原始參考範例

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *