首页 > 代码库 > 仿一号店APP商品分类效果开发Android版本

仿一号店APP商品分类效果开发Android版本


      昨天我们一起学习了一下一号店(IOS版本)(仿一号店APP商品分类效果开发IOS)商品分类效果编写今天我们一起来看一下Android版本上面的实现。其实我们的实现的具体思路差不多。总体先搞定一级分类,然后二级我们这边使用GridView布局。然后点击一级分类的每一项的时候,动态显示以及隐藏相关的二级分类数据。现在我们来看一下实现的效果.


     技术分享技术分享

      然后这边还有一点需要介绍的是ViewGrounp的一个动画android:animateLayoutchanges属性。这个属性设置这样就自动地按照默认方式来对要移除或添加的View,还有Group中的其他View进行动画。

     下面一起来看一下代码实现.

      (一)布局文件:

<span style="font-size:18px;"><?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:orientation="vertical" >

    <include layout="@layout/top_bar_one_layout" />

    <FrameLayout
        android:id="@+id/frame_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ScrollView
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:animateLayoutChanges="true"
                android:orientation="vertical" >

                <LinearLayout
                    android:id="@+id/line_1"
                    android:layout_width="match_parent"
                    android:layout_height="108dip"
                    android:orientation="horizontal" >

                    <LinearLayout
                        android:id="@+id/linear_1"
                        android:layout_width="0dip"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical" >

                        <ImageView
                            android:id="@+id/iv_1"
                            android:layout_width="match_parent"
                            android:layout_height="0dip"
                            android:layout_marginTop="12dip"
                            android:layout_marginLeft="12dip"
                            android:layout_marginRight="12dip"
                            android:layout_marginBottom="5dip"
                            android:layout_weight="1"
                            android:src=http://www.mamicode.com/"@drawable/ic_launcher" />>(二)解决GridView与ScrollView的冲突问题

package com.zttmall.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

public class CustomGridView extends GridView {
	public CustomGridView(Context context) {
		super(context);
	}

	public CustomGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
				MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}
}
(三).具体实现代码如下:

package com.zttmall.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import tv.pps.modules.imagelogic.ImageLogic;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.zttmall.BaseFragment;
import com.zttmall.ZTTMallApplication;
import com.zttmall.adapter.SubCategoryAdapter;
import com.zttmall.common.DefineView;
import com.zttmall.common.DeliverConsts;
import com.zttmall.common.RequestURL;
import com.zttmall.listlogic.ListFetcher;
import com.zttmall.listlogic.ListSimpleParse;
import com.zttmall.model.category.CategoryModel;
import com.zttmall.model.category.ShowCategoryModel;
import com.zttmall.model.category.SubCategoryModel;
import com.zttmall.ui.ClassifyThreeCategoryActivity;
import com.zttmall.ui.R;
import com.zttmall.utils.Log;

/**
 * 商品分类功能
 * 
 * @author jiangqq
 * 
 */
public class ClassifyFragment extends BaseFragment implements DefineView {
	private View mView;
	private TextView tab_bar_title_one;
	private ShowCategoryModel model;

	private FrameLayout frame_content;
	private FrameLayout prompt_framelayout;
	private LinearLayout loading;
	private LinearLayout empty;
	private LinearLayout error;

	String descString;
	private ListFetcher mListWorker;
	private HashMap<String, String> requestMap;

	private SubCategoryAdapter mSubCategoryAdapter;
	ImageLogic mImageLogic;
	private List<LinearLayout> linearList = new ArrayList<LinearLayout>();
	private List<ImageView> ivList = new ArrayList<ImageView>();
	private List<TextView> tvList = new ArrayList<TextView>();
	private List<LinearLayout> hideList = new ArrayList<LinearLayout>();
	private List<GridView> gvList = new ArrayList<GridView>();

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == ListFetcher.LIST_OK) {
				getDataOk();
			} else if (msg.what == ListFetcher.LIST_ERROR) {
				getDataError();
			}

		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mView == null) {
			mView = inflater.inflate(R.layout.classify_fragment_layout,
					container, false);
			initView();
			initValidata();
			initListener();
			bindData();
		}
		return mView;
	}

	@Override
	public void initView() {
		tab_bar_title_one = (TextView) mView
				.findViewById(R.id.tab_bar_title_one);
		tab_bar_title_one.setText(R.string.string_classify_name);
		frame_content = (FrameLayout) mView.findViewById(R.id.frame_content);
		prompt_framelayout = (FrameLayout) mView
				.findViewById(R.id.prompt_framelayout);
		loading = (LinearLayout) mView.findViewById(R.id.loading);
		empty = (LinearLayout) mView.findViewById(R.id.empty);
		error = (LinearLayout) mView.findViewById(R.id.error);

		initViewList();

	}

	public void initViewList() {

		/*
		 * line1 = (LinearLayout) mView.findViewById(R.id.line_1); line2 =
		 * (LinearLayout) mView.findViewById(R.id.line_2); line3 =
		 * (LinearLayout) mView.findViewById(R.id.line_3); line4 =
		 * (LinearLayout) mView.findViewById(R.id.line_4);
		 */

		linearList.add((LinearLayout) mView.findViewById(R.id.linear_1));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_2));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_3));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_4));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_5));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_6));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_7));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_8));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_9));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_10));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_11));
		linearList.add((LinearLayout) mView.findViewById(R.id.linear_12));

		ivList.add((ImageView) mView.findViewById(R.id.iv_1));
		ivList.add((ImageView) mView.findViewById(R.id.iv_2));
		ivList.add((ImageView) mView.findViewById(R.id.iv_3));
		ivList.add((ImageView) mView.findViewById(R.id.iv_4));
		ivList.add((ImageView) mView.findViewById(R.id.iv_5));
		ivList.add((ImageView) mView.findViewById(R.id.iv_6));
		ivList.add((ImageView) mView.findViewById(R.id.iv_7));
		ivList.add((ImageView) mView.findViewById(R.id.iv_8));
		ivList.add((ImageView) mView.findViewById(R.id.iv_9));
		ivList.add((ImageView) mView.findViewById(R.id.iv_10));
		ivList.add((ImageView) mView.findViewById(R.id.iv_11));
		ivList.add((ImageView) mView.findViewById(R.id.iv_12));

		tvList.add((TextView) mView.findViewById(R.id.tv_1));
		tvList.add((TextView) mView.findViewById(R.id.tv_2));
		tvList.add((TextView) mView.findViewById(R.id.tv_3));
		tvList.add((TextView) mView.findViewById(R.id.tv_4));
		tvList.add((TextView) mView.findViewById(R.id.tv_5));
		tvList.add((TextView) mView.findViewById(R.id.tv_6));
		tvList.add((TextView) mView.findViewById(R.id.tv_7));
		tvList.add((TextView) mView.findViewById(R.id.tv_8));
		tvList.add((TextView) mView.findViewById(R.id.tv_9));
		tvList.add((TextView) mView.findViewById(R.id.tv_10));
		tvList.add((TextView) mView.findViewById(R.id.tv_11));
		tvList.add((TextView) mView.findViewById(R.id.tv_12));

		hideList.add((LinearLayout) mView.findViewById(R.id.hide_1));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_2));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_3));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_4));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_5));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_6));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_7));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_8));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_9));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_10));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_11));
		hideList.add((LinearLayout) mView.findViewById(R.id.hide_12));

		gvList.add((GridView) mView.findViewById(R.id.gv_1));
		gvList.add((GridView) mView.findViewById(R.id.gv_2));
		gvList.add((GridView) mView.findViewById(R.id.gv_3));
		gvList.add((GridView) mView.findViewById(R.id.gv_4));
		gvList.add((GridView) mView.findViewById(R.id.gv_5));
		gvList.add((GridView) mView.findViewById(R.id.gv_6));
		gvList.add((GridView) mView.findViewById(R.id.gv_7));
		gvList.add((GridView) mView.findViewById(R.id.gv_8));
		gvList.add((GridView) mView.findViewById(R.id.gv_9));
		gvList.add((GridView) mView.findViewById(R.id.gv_10));
		gvList.add((GridView) mView.findViewById(R.id.gv_11));
		gvList.add((GridView) mView.findViewById(R.id.gv_12));
	}

	@Override
	public void initValidata() {
		// TODO Auto-generated method stub
		mImageLogic = ImageLogic.create(getActivity());
		mImageLogic.configBitmapMaxHeight(120);
		mImageLogic.configBitmapMaxWidth(120);

		mListWorker = new ListFetcher(getActivity(),
				ListSimpleParse.DEFAULT_JSON_CATEGORY, mHandler);
		mListWorker.loadJsonDataList(RequestURL.CATEGORY_URL);

		mSubCategoryAdapter = new SubCategoryAdapter(getActivity());

		frame_content.setVisibility(View.GONE);
		prompt_framelayout.setVisibility(View.VISIBLE);
		loading.setVisibility(View.VISIBLE);
		empty.setVisibility(View.GONE);
		error.setVisibility(View.GONE);
	}

	@Override
	public void initListener() {
		// TODO Auto-generated method stub
		error.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mListWorker.loadJsonDataList(RequestURL.CATEGORY_URL);
				frame_content.setVisibility(View.GONE);
				prompt_framelayout.setVisibility(View.VISIBLE);
				loading.setVisibility(View.VISIBLE);
				empty.setVisibility(View.GONE);
				error.setVisibility(View.GONE);
			}
		});
		for (int i = 0; i < linearList.size(); i++) {
			linearList.get(i).setOnClickListener(
					new MyCustomerOnclickListener());
		}
	}

	@Override
	public void bindData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void getPageData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void getDataOk() {
		// TODO Auto-generated method stub
		HashMap<String, Object> tempMap = ZTTMallApplication.getInstance()
				.getTempMap();
		model = (ShowCategoryModel) tempMap
				.get(DeliverConsts.KEY_CATEGORY_MODEL);
		if (model != null && model.getCode().equals("1")
				&& "SUCCESS".equals(model.getMsg())) {
			if (model.getData() != null && model.getData().size() > 0) {
				showData(model);
				frame_content.setVisibility(View.VISIBLE);
				prompt_framelayout.setVisibility(View.GONE);
				loading.setVisibility(View.GONE);
				empty.setVisibility(View.GONE);
				error.setVisibility(View.GONE);
			} else {
				frame_content.setVisibility(View.GONE);
				prompt_framelayout.setVisibility(View.VISIBLE);
				loading.setVisibility(View.GONE);
				empty.setVisibility(View.VISIBLE);
				error.setVisibility(View.GONE);
			}
		} else {
			frame_content.setVisibility(View.GONE);
			prompt_framelayout.setVisibility(View.VISIBLE);
			loading.setVisibility(View.GONE);
			empty.setVisibility(View.GONE);
			error.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void getDataError() {
		// TODO Auto-generated method stub

	}

	@Override
	public void getDataRefresh() {
		// TODO Auto-generated method stub

	}

	class MyCustomerOnclickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.linear_1:
				showOrHide(0, model.getData().get(0).getSub_category());
				break;
			case R.id.linear_2:
				showOrHide(1, model.getData().get(1).getSub_category());
				break;
			case R.id.linear_3:
				showOrHide(2, model.getData().get(2).getSub_category());
				break;
			case R.id.linear_4:
				showOrHide(3, model.getData().get(3).getSub_category());
				break;
			case R.id.linear_5:
				showOrHide(4, model.getData().get(4).getSub_category());
				break;
			case R.id.linear_6:
				showOrHide(5, model.getData().get(5).getSub_category());
				break;
			case R.id.linear_7:
				showOrHide(6, model.getData().get(6).getSub_category());
				break;
			case R.id.linear_8:
				showOrHide(7, model.getData().get(7).getSub_category());
				break;
			case R.id.linear_9:
				showOrHide(8, model.getData().get(8).getSub_category());
				break;
			case R.id.linear_10:
				showOrHide(9, model.getData().get(9).getSub_category());
				break;
			case R.id.linear_11:
				showOrHide(10, model.getData().get(10).getSub_category());
				break;
			case R.id.linear_12:
				showOrHide(11, model.getData().get(11).getSub_category());
				break;
			}
		}

		void showOrHide(final int i, final List<SubCategoryModel> list) {
			if (list != null && list.size() > 0) {
				mSubCategoryAdapter.setmList(list);
				gvList.get(i).setAdapter(mSubCategoryAdapter);

				descString = model.getData().get(i).getDesc();
				mSubCategoryAdapter.notifyDataSetChanged();

				for (int j = 0; j < hideList.size(); j++) {
					if (j != i) {
						hideList.get(j).setVisibility(View.GONE);
					} else {
						if (hideList.get(i).getVisibility() == View.VISIBLE) {
							hideList.get(i).setVisibility(View.GONE);
						} else {
							hideList.get(i).setVisibility(View.VISIBLE);
							gvList.get(i).setOnItemClickListener(
									new OnItemClickListener() {

										@Override
										public void onItemClick(
												AdapterView<?> parent,
												View view, int position, long id) {
											// TODO Auto-generated method stub
											Intent _Intent = new Intent(
													getActivity(),
													ClassifyThreeCategoryActivity.class);
											_Intent.putExtra("title",
													descString);
											_Intent.putExtra(
													"content",
													JSON.toJSONString(list
															.get(position)
															.getThree_category()));
											startActivity(_Intent);
										}
									});
						}
					}
				}
			} else {
				List<SubCategoryModel> lists = new ArrayList<SubCategoryModel>();
				mSubCategoryAdapter.setmList(lists);
				mSubCategoryAdapter.notifyDataSetChanged();
			}

		}

	}

	void showData(ShowCategoryModel model) {
		List<CategoryModel> mLists = model.getData();
		int length = model.getData().size();
		for (int i = 0; i < model.getData().size(); i++) {
			tvList.get(i).setText(model.getData().get(i).getTitle());
			mImageLogic
					.display(ivList.get(i), model.getData().get(i).getIcon());
		}
		int temp = (length - 1) / 3 + 2;
		for (int i = length; i < linearList.size(); i++) {
			linearList.get(i).setVisibility(View.INVISIBLE);
		}
		if (linearList.size() > (temp - 1) * 3)
			for (int i = (temp - 1) * 3; i < linearList.size(); i++) {
				linearList.get(i).setVisibility(View.GONE);
			}

	}
}
相应的信息实体类(Model)就不贴了,直接上一篇博客就行了。

下面给一下测试的地址:http://img2.xxh.cc:8080/SalesWebTest/CategoryList

最后再说一下最近我自己在搞工作室,希望小编(如果小编看到这段觉得不妥,请帮我删掉这段,非常感谢!)以及路过的朋友们不要介意哦。优清科技工作室团队,主要承接Android/IOS,J2EE,.Net,PHP,移动建站,网站建设,微信系统项目开发。

仿一号店APP商品分类效果开发Android版本