Google電子市場7--分類,Google電子市場7--
1.對象封裝
public class CategoryInfo { public String name1; public String name2; public String name3; public String url1; public String url2; public String url3; public String title; public boolean isTitle;// 表示當前對象是否是標題}
2.CategoryFragment
public class CategoryFragment extends BaseFragment { private ArrayList<CategoryInfo> mList; @Override public View onCreateSuccessView() { MyListView view = new MyListView(UIUtils.getContext()); view.setAdapter(new CategoryAdapter(mList)); return view; } @Override public ResultState onLoad() { CategoryProtocol protocol = new CategoryProtocol(); mList = protocol.getData(0); return check(mList); } class CategoryAdapter extends MyBaseAdapter<CategoryInfo> { public CategoryAdapter(ArrayList<CategoryInfo> list) { super(list); } // 根據當前位置,返回相應的Holder對象 @Override public BaseHolder<CategoryInfo> getHolder(int position) { CategoryInfo info = getItem(position); if (info.isTitle) { return new TitleHolder();// 標題列holder } return new CategoryHolder();// 普通類型holer } @Override public ArrayList<CategoryInfo> onLoadMore() { return null; } @Override public boolean hasMore() { return false;// 沒有更多資料, 無需載入下一頁 } @Override public int getViewTypeCount() { return super.getViewTypeCount() + 1;// 在原來基礎上,新增標題列類型 } // 根據位置判斷當前item的類型 @Override public int getInnerType(int position) { CategoryInfo info = getItem(position); if (info.isTitle) {// 標題列類型 return super.getInnerType(position) + 1; } else {// 普通類型 return super.getInnerType(position); } } }}
3.Holder
/** * 分類網頁標題欄holder */public class TitleHolder extends BaseHolder<CategoryInfo> { private TextView tvTitle; @Override public View initView() { View view = View.inflate(UIUtils.getContext(), R.layout.list_item_title, null); tvTitle = (TextView) view.findViewById(R.id.tv_title); return view; } @Override public void refreshView(CategoryInfo data) { tvTitle.setText(data.title); }}---------------------------------------------list_item_title.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:textSize="18sp" /></LinearLayout>--------------------------------------------------/** * 分類holder * * @author Kevin */public class CategoryHolder extends BaseHolder<CategoryInfo> implements OnClickListener { private ImageView ivIcon1, ivIcon2, ivIcon3; private TextView tvName1, tvName2, tvName3; private LinearLayout llGrid1, llGrid2, llGrid3; private BitmapUtils mBitmapUtils; @Override public View initView() { View view = View.inflate(UIUtils.getContext(), R.layout.list_item_category, null); mBitmapUtils = BitmapHelper.getBitmapUtils(); mBitmapUtils.configDefaultLoadingImage(R.drawable.ic_default); ivIcon1 = (ImageView) view.findViewById(R.id.iv_icon1); ivIcon2 = (ImageView) view.findViewById(R.id.iv_icon2); ivIcon3 = (ImageView) view.findViewById(R.id.iv_icon3); tvName1 = (TextView) view.findViewById(R.id.tv_name1); tvName2 = (TextView) view.findViewById(R.id.tv_name2); tvName3 = (TextView) view.findViewById(R.id.tv_name3); llGrid1 = (LinearLayout) view.findViewById(R.id.ll_grid1); llGrid2 = (LinearLayout) view.findViewById(R.id.ll_grid2); llGrid3 = (LinearLayout) view.findViewById(R.id.ll_grid3); llGrid1.setOnClickListener(this); llGrid2.setOnClickListener(this); llGrid3.setOnClickListener(this); return view; } @Override public void refreshView(CategoryInfo data) { if (data != null) { tvName1.setText(data.name1); tvName2.setText(data.name2); tvName3.setText(data.name3); mBitmapUtils.display(ivIcon1, HttpHelper.URL + "image?name=" + data.url1); mBitmapUtils.display(ivIcon2, HttpHelper.URL + "image?name=" + data.url2); mBitmapUtils.display(ivIcon3, HttpHelper.URL + "image?name=" + data.url3); } } @Override public void onClick(View v) { CategoryInfo data = getData(); switch (v.getId()) { case R.id.ll_grid1: Toast.makeText(UIUtils.getContext(), data.name1, Toast.LENGTH_SHORT) .show(); break; case R.id.ll_grid2: Toast.makeText(UIUtils.getContext(), data.name2, Toast.LENGTH_SHORT) .show(); break; case R.id.ll_grid3: Toast.makeText(UIUtils.getContext(), data.name3, Toast.LENGTH_SHORT) .show(); break; default: break; } }}--------------------------------------------list_item_category.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:orientation="horizontal" > <LinearLayout android:id="@+id/ll_grid1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/grid_item_bg_selector" android:gravity="center" android:orientation="vertical" android:padding="5dp" > <ImageView android:id="@+id/iv_icon1" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/ic_default" /> <TextView android:id="@+id/tv_name1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="休閑" android:textColor="#000" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/ll_grid2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/grid_item_bg_selector" android:gravity="center" android:orientation="vertical" android:padding="5dp" > <ImageView android:id="@+id/iv_icon2" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/ic_default" /> <TextView android:id="@+id/tv_name2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="休閑" android:textColor="#000" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/ll_grid3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/grid_item_bg_selector" android:gravity="center" android:orientation="vertical" android:padding="5dp" > <ImageView android:id="@+id/iv_icon3" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/ic_default" /> <TextView android:id="@+id/tv_name3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="休閑" android:textColor="#000" android:textSize="16sp" /> </LinearLayout></LinearLayout>