Android 商品筛选功能实现
前段时间做了个网上商城项目,里面含有类似京东商品搜索功能,感觉此功能普遍适用所有在此贴出实现步骤
先看效果
主页面的布局
由于布局简单,就不多啰嗦了,只是在外面放了一个 DrawerLayout 布局,让筛选页面作为 DrawerLayout 的一个菜单页显示。
筛选页面布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/bottom_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<TextView
android:id="@+id/reset_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:background="@color/white_f4"
android:gravity="center"
android:text="重置"
android:textColor="@color/gray"
android:textSize="@dimen/content_text_size_14"/>
<TextView
android:id="@+id/confirm_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:background="@color/talk_text_red"
android:gravity="center"
android:text="确定"
android:textColor="@color/white"
android:textSize="@dimen/content_text_size_14"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingBottom="8dp"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_gravity="center">
<com.mylinkbuy.android.view.TagWidget
android:id="@+id/tag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_gravity="center">
<com.mylinkbuy.android.view.TagWidget
android:id="@+id/tag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_gravity="center">
<com.mylinkbuy.android.view.TagWidget
android:id="@+id/tag3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<com.mylinkbuy.android.view.TagWidget
android:id="@+id/tag4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/line_color_grey"/>
<LinearLayout
android:id="@+id/address_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="15dp"
android:paddingEnd="0dp"
android:background="@drawable/white_buttom"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="配送至"
android:textColor="@color/blacking"
android:textSize="@dimen/content_text_size_14"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="20dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@mipmap/icon_location_small"/>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:padding="8dp"
android:contentDescription="@string/app_name"
android:src="@mipmap/more_icon"/>
<TextView
android:id="@+id/location_txv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:layout_gravity="center_vertical"
android:padding="4dp"
android:lines="1"
android:text="上海市 青浦区 徐泾镇"
android:textColor="@color/talk_text_red"
android:textSize="12sp"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:paddingStart="13dp"
android:paddingEnd="13dp"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="价格区间"
android:textColor="@color/black"
android:textSize="@dimen/content_text_size_14"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp">
<EditText
android:id="@+id/min_price_edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:paddingStart="13dp"
android:paddingEnd="13dp"
android:background="@drawable/shape_gray"
android:hint="最低价"
android:inputType="numberDecimal"
android:lines="1"
android:maxEms="6"
android:maxLength="6"
android:textColor="@color/black"
android:textColorHint="@color/gray"
android:textSize="@dimen/content_text_size_12"/>
<EditText
android:id="@+id/max_price_edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:paddingStart="13dp"
android:paddingEnd="13dp"
android:background="@drawable/shape_gray"
android:hint="最高价"
android:inputType="numberDecimal"
android:lines="1"
android:maxEms="6"
android:maxLength="6"
android:textColor="@color/black"
android:textColorHint="@color/gray"
android:textSize="@dimen/content_text_size_12"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="一"/>
</RelativeLayout>
</LinearLayout>
<ListView
android:id="@+id/filter_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:scrollbars="none"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
TagWidget 控件实现
该控件用于显示筛选页面各个筛选条件,当点击标签时,其状态改变。该控件对外抛出的方法都给与了说明,都很简单。
package com.mylinkbuy.android.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.mylinkbuy.android.R;
/**
* **********************************************
* <p/>
* Date: 2018-01-30 22:07
* <p/>
* Author: SinPingWu
* <p/>
* Email: wuxinping@ubinavi.com.cn
* <p/>
* brief: 标签控件
* <p/>
* history:
* <p/>
* **********************************************
*/
public class TagWidget extends LinearLayout {
private ImageView mImg;
private TextView mTxv;
private boolean isSelected = false;
private float mDensity = 1.0f;
public TagWidget(Context context) {
super(context);
initView();
}
public TagWidget(Context context,
@Nullable AttributeSet attrs) {
super(context, attrs);
initView();
}
public TagWidget(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
setOrientation(HORIZONTAL);
setGravity(Gravity.CENTER);
mDensity = getScreenDensity();
setMinimumHeight((int) mDensity * 28);
setMinimumWidth((int) mDensity * 80);
setBackgroundResource(R.drawable.shape_gray);
mImg = new ImageView(getContext());
mImg.setScaleType(ScaleType.CENTER_INSIDE);
mImg.setImageResource(R.mipmap.ic_check);
mImg.setVisibility(View.GONE);
addView(mImg);
mTxv = new TextView(getContext());
mTxv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
mTxv.setTextColor(Color.GRAY);
mTxv.setGravity(Gravity.CENTER);
mTxv.setText("");
addView(mTxv);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
LinearLayout.LayoutParams parentParams = (LayoutParams) getLayoutParams();
parentParams.width = (int) mDensity * 80;
parentParams.height = (int) mDensity * 28;
setLayoutParams(parentParams);
LinearLayout.LayoutParams params = (LayoutParams) mImg.getLayoutParams();
params.setMarginEnd((int) mDensity * 4);
mImg.setLayoutParams(params);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
/**
* 切换标签选中状态
*/
public void toggle() {
isSelected = !isSelected;
if (isSelected) {
mImg.setVisibility(View.VISIBLE);
mTxv.setTextColor(Color.RED);
setBackgroundResource(R.drawable.shape_red);
} else {
mImg.setVisibility(View.GONE);
mTxv.setTextColor(Color.GRAY);
setBackgroundResource(R.drawable.shape_gray);
}
}
/**
* 获取标签里面的内容
*/
public String getContent() {
return mTxv == null ? "" : mTxv.getText().toString().trim();
}
/**
* 设置标签内容
*/
public void setContent(String content) {
mTxv.setText(content);
}
/**
* 设置标签内容
*
* @param res 内容对应的资源地址
*/
public void setContent(int res) {
mTxv.setText(getContext().getString(res));
}
/**
* 获取标签的选中状态
*
* @return 如果选中返回 true,否则返回 false。
*/
public boolean getStatus() {
return isSelected;
}
private float getScreenDensity() {
WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(dm);
return dm.density;
}
}
筛选功能实现
该类与适配器结合实现筛选功能。setMenuData()用于设置筛选菜单内容,getFilter()用于获取筛选项。
package com.mylinkbuy.android.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.mylinkbuy.android.R;
import com.mylinkbuy.android.adapter.GoodsMenuAdapter;
import com.mylinkbuy.android.adapter.GoodsMenuAdapter.ViewHolder;
import com.mylinkbuy.android.adapter.GoodsSubMenuAdapter;
import com.mylinkbuy.android.callback.OnAdapterDataNotifiedListener;
import com.mylinkbuy.android.model.GoodsFilterInfo;
import com.mylinkbuy.android.model.GoodsFilterInfo.Brand;
import com.mylinkbuy.android.model.GoodsFilterInfo.Spec;
import com.mylinkbuy.android.utils.ScreenUtil;
import com.mylinkbuy.android.view.TagWidget;
import java.util.ArrayList;
import java.util.HashMap;
/**
* **********************************************
* <p/>
* Date: 2018-01-30 22:58
* <p/>
* Author: SinPingWu
* <p/>
* Email: wuxinping@ubinavi.com.cn
* <p/>
* brief: 商品列表的过滤页面
* <p/>
* history:
* <p/>
* **********************************************
*/
public class GoodsMenuFragment extends Fragment implements OnAdapterDataNotifiedListener, OnClickListener {
private TagWidget[] mTAGWidget;
private TextView mLocationTxv;
private EditText mMinPriceEdt, mMaxPriceEdt;
private ListView mListView;
private GoodsMenuAdapter mMenuAdapter;
private GoodsFilterInfo mGoodsFilterInfo;
private OnGoodsSearchListener mGoodsSearchCallback;
public static final String SEPARATOR = "@";
public interface OnGoodsSearchListener {
/**
* 根据过滤条件进行商品搜索。
*
* supplier:供应商,供应商1@供应商2.
* price:价格区间,低价@高价.
* spec: 参数,spec1@spec2
* brand: 品牌,brand1@brand2
*/
void onFilterCompleted();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.goods_filter_menu, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView(view);
}
public void setSearchCallbackListener(OnGoodsSearchListener callbackListener) {
this.mGoodsSearchCallback = callbackListener;
}
/**
* 设置菜单的内容
*
* @param goodsFilterInfo 菜单内容所持有的数据类型。
*/
public void setMenuData(GoodsFilterInfo goodsFilterInfo) {
this.mGoodsFilterInfo = goodsFilterInfo;
mMenuAdapter.setData(mGoodsFilterInfo);
}
private void initView(View view) {
mTAGWidget = new TagWidget[4];
mTAGWidget[0] = (TagWidget) view.findViewById(R.id.tag1);
mTAGWidget[1] = (TagWidget) view.findViewById(R.id.tag2);
mTAGWidget[2] = (TagWidget) view.findViewById(R.id.tag3);
mTAGWidget[3] = (TagWidget) view.findViewById(R.id.tag4);
mLocationTxv = (TextView) view.findViewById(R.id.location_txv);
mMinPriceEdt = (EditText) view.findViewById(R.id.min_price_edt);
mMaxPriceEdt = (EditText) view.findViewById(R.id.max_price_edt);
mListView = (ListView) view.findViewById(R.id.filter_list);
mMenuAdapter = new GoodsMenuAdapter(getContext(), mGoodsFilterInfo);
mMenuAdapter.setDataNotifyListener(this);
mListView.setAdapter(mMenuAdapter);
setTAGWidgetsContent();
setParams(view);
}
/**
* 设置顶部标签内容
*/
private void setTAGWidgetsContent() {
String[] contents = {"自营", "仅看有货", "新品", "促销"};
for (int i = 0; i < mTAGWidget.length; i++) {
mTAGWidget[i].setContent(contents[i]);
}
}
/**
* 初始化页面数据,并设置事件
*/
private void setParams(View view) {
for (TagWidget widget : mTAGWidget) {
widget.setOnClickListener(this);
}
mLocationTxv.setText("");
view.findViewById(R.id.reset_btn).setOnClickListener(this);
view.findViewById(R.id.confirm_btn).setOnClickListener(this);
}
@Override
public void onDataNotified() {
setListViewHeightBasedOnChildren(mListView);
}
/**
* 修改ListView 的高度.
*/
private void setListViewHeightBasedOnChildren(ListView listView) {
//获取listView的适配器
ListAdapter listAdapter = listView.getAdapter(); //item的高度
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0); //计算子项View 的宽高 //统计所有子项的总高度
totalHeight += ScreenUtil.dip2px(getContext(), listItem.getMeasuredHeight()) + listView.getDividerHeight();
}
//TODO 这里计算高度的方式不正确,待修改
totalHeight += ScreenUtil.dip2px(getContext(), 150);
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight;
listView.setLayoutParams(params);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tag1:
mTAGWidget[0].toggle();
break;
case R.id.tag2:
mTAGWidget[1].toggle();
break;
case R.id.tag3:
mTAGWidget[2].toggle();
break;
case R.id.tag4:
mTAGWidget[3].toggle();
break;
case R.id.reset_btn:
resetFilter();
break;
case R.id.confirm_btn:
filterConfirm();
break;
}
}
/**
* 重置筛选条件
*/
private void resetFilter() {
int size = mListView.getCount();
for (int i = 0; i < size; i++) {
View view = mListView.getChildAt(i);
GoodsMenuAdapter.ViewHolder viewHolder = (ViewHolder) view.getTag();
if (viewHolder.menuAdapter != null) {
viewHolder.menuAdapter.clearSelectedIndex();
}
}
mMenuAdapter.notifyDataSetChanged();
for (TagWidget widget : mTAGWidget) {
if (widget.getStatus()) {
widget.toggle();
}
}
mMinPriceEdt.setText("");
mMaxPriceEdt.setText("");
}
/**
* 确认筛选条件
*/
private void filterConfirm() {
if (mGoodsSearchCallback == null) {
return;
}
mGoodsSearchCallback.onFilterCompleted();
}
/**
* 获取筛选信息
*/
public HashMap<String, String> getFilter() {
HashMap<String, String> filterParams = new HashMap<>();
String supplier = getSupplierFilter();
if (supplier.length() > 0) {
filterParams.put("supplier", supplier);
}
String price = getPriceFilter();
if (price.length() > 0) {
filterParams.put("price", price);
}
String spec = getSpecFilter();
if (spec.length() > 0) {
filterParams.put("spec", spec);
}
String brand = getBrandFilter();
if (brand.length() > 0) {
filterParams.put("brand", brand);
}
return filterParams;
}
/**
* 获取供应商筛选信息
*/
private String getSupplierFilter() {
return "";
}
/**
* 获取价格筛选信息
*/
public String getPriceFilter() {
try {
String minPrice = mMinPriceEdt.getText().toString().trim();
String maxPrice = mMaxPriceEdt.getText().toString().trim();
if (minPrice.length() == 0 | maxPrice.length() == 0)
return "";
else
return minPrice + SEPARATOR + maxPrice;
} catch (Exception e) {
return "";
}
}
/**
* 获取参数筛选信息
*/
public String getSpecFilter() {
try {
int size = mListView.getCount();
if (size <= 1)
return "";
StringBuilder strBuild = new StringBuilder();
for (int i = 1; i < size; i++) {
View childView = mListView.getChildAt(i);
ViewHolder viewHolder = (ViewHolder) childView.getTag();
GridView childGridView = viewHolder.gridView;
if (childGridView == null) {
continue;
}
Spec spec = mGoodsFilterInfo.getFilter_spec().get(i - 1);
for (int j = 0; j < childGridView.getCount(); j++) {
View childGridViewItem = childGridView.getChildAt(j);
GoodsSubMenuAdapter.ViewHolder gridViewHolder = (GoodsSubMenuAdapter.ViewHolder) childGridViewItem.getTag();
boolean isSelected = gridViewHolder.widget.getStatus();
if (!isSelected) {
continue;
}
String specValue;
try {
//specValue = spec.getItem().get(j).getApp().getSpec();
specValue = spec.getItem().get(j).getId();
} catch (Exception e) {
specValue = "";
}
if (specValue != null && specValue.length() > 0) {
strBuild.append(specValue).append(SEPARATOR);
}
}
}
String result = "";
if (strBuild.length() > SEPARATOR.length()) {
int endIndex = strBuild.lastIndexOf(SEPARATOR);
result = strBuild.substring(0, endIndex);
}
return result;
} catch (Exception e) {
return "";
}
}
/**
* 获取参数筛选信息
*/
public String getBrandFilter() {
try {
StringBuilder strBuild = new StringBuilder();
View childView = mListView.getChildAt(0);
ViewHolder viewHolder = (ViewHolder) childView.getTag();
GridView childGridView = viewHolder.gridView;
if (childGridView == null) {
return "";
}
ArrayList<Brand> brands = mGoodsFilterInfo.getFilter_brand();
for (int j = 0; j < childGridView.getCount(); j++) {
View childGridViewItem = childGridView.getChildAt(j);
GoodsSubMenuAdapter.ViewHolder gridViewHolder = (GoodsSubMenuAdapter.ViewHolder) childGridViewItem.getTag();
boolean isSelected = gridViewHolder.widget.getStatus();
if (!isSelected) {
continue;
}
String brandId;
try {
//brandId = brands.get(j).getApp().getBrand_id();
brandId = brands.get(j).getId();
} catch (Exception e) {
brandId = "";
}
if (brandId != null && brandId.length() > 0) {
strBuild.append(brandId).append(SEPARATOR);
}
}
String result = "";
if (strBuild.length() > SEPARATOR.length()) {
int endIndex = strBuild.lastIndexOf(SEPARATOR);
result = strBuild.substring(0, endIndex);
}
return result;
} catch (Exception e) {
return "";
}
}
}
package com.mylinkbuy.android.adapter;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.mylinkbuy.android.R;
import com.mylinkbuy.android.callback.OnAdapterDataNotifiedListener;
import com.mylinkbuy.android.model.GoodsFilterInfo;
import com.mylinkbuy.android.model.GoodsFilterInfo.Brand;
import com.mylinkbuy.android.model.GoodsFilterInfo.Spec;
import com.mylinkbuy.android.model.GoodsFilterInfo.SpecItem;
import java.util.ArrayList;
/**
* **********************************************
* <p/>
* Date: 2018-01-31 10:30
* <p/>
* Author: SinPingWu
* <p/>
* Email: wuxinping@ubinavi.com.cn
* <p/>
* brief: 商品列表菜单适配器
* <p/>
* history:
* <p/>
* **********************************************
*/
public class GoodsMenuAdapter extends BaseAdapter {
private Context mContext;
private GoodsFilterInfo mFilterInfo;
private OnAdapterDataNotifiedListener mNotifiedCallback = null;
public GoodsMenuAdapter(Context context, GoodsFilterInfo filterInfo) {
this.mContext = context;
this.mFilterInfo = filterInfo;
}
public void setDataNotifyListener(OnAdapterDataNotifiedListener callback) {
this.mNotifiedCallback = callback;
}
public void setData(GoodsFilterInfo filterInfo) {
this.mFilterInfo = filterInfo;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.goods_filter_menu_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.subTitle = (TextView) convertView.findViewById(R.id.sub_title);
viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
viewHolder.gridView = (GridView) convertView.findViewById(R.id.grid_view);
viewHolder.menuAdapter = new GoodsSubMenuAdapter(mContext, null);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.isExpanded = !viewHolder.isExpanded;
notifyDataSetChanged();
}
});
String title;
ArrayList<String> subFilterData;
if (position == 0) {
title = "品牌";
ArrayList<GoodsFilterInfo.Brand> brands = mFilterInfo.getFilter_brand();
subFilterData = transferBrandInfo(brands, !viewHolder.isExpanded);
} else {
Spec spec = mFilterInfo.getFilter_spec().get(position - 1);
title = spec.getName();
ArrayList<SpecItem> specItems = spec.getItem();
subFilterData = transferSpecItemInfo(specItems, !viewHolder.isExpanded);
}
viewHolder.title.setText(title);
viewHolder.subTitle.setText("");
if (viewHolder.menuAdapter == null) {
viewHolder.menuAdapter = new GoodsSubMenuAdapter(mContext, subFilterData);
} else {
viewHolder.menuAdapter.setData(subFilterData);
}
//GoodsSubMenuAdapter menuAdapter = new GoodsSubMenuAdapter(mContext, subFilterData);
viewHolder.gridView.setAdapter(viewHolder.menuAdapter);
return convertView;
}
@Override
public int getCount() {
if (mFilterInfo == null)
return 0;
int size = mFilterInfo.getFilter_spec().size();
if (mFilterInfo.getFilter_brand() != null) {
size = size + 1;
}
/*if (mFilterInfo.getFilter_price() != null) {
size = size + 1;
}*/
return size;
}
@Override
public Object getItem(int position) {
if (position == 0) {
return mFilterInfo.getFilter_brand();
} else {
return mFilterInfo.getFilter_spec().get(position - 1);
}
/*int specSize = mFilterInfo.getFilter_spec().size();
if (position < specSize) {
return mFilterInfo.getFilter_spec().get(position);
} else if (position == specSize) {
return mFilterInfo.getFilter_brand();
} else {
return mFilterInfo.getFilter_price();
}*/
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
if (mNotifiedCallback != null) {
mNotifiedCallback.onDataNotified();
}
}
public static class ViewHolder {
public TextView title;
TextView subTitle;
ImageView img;
public GridView gridView;
public GoodsSubMenuAdapter menuAdapter;
/**
* 代表 GridView 是否为展开的。默认false.
*/
boolean isExpanded;
}
private ArrayList<String> transferBrandInfo(ArrayList<GoodsFilterInfo.Brand> brands, boolean isOmitted) {
ArrayList<String> result = new ArrayList<>();
if (brands == null)
return result;
int size;
if (isOmitted && brands.size() > 3) {
size = 3;
} else {
size = brands.size();
}
for (int i = 0; i < size; i++) {
Brand brand = brands.get(i);
result.add(brand.getName());
}
return result;
}
private ArrayList<String> transferSpecItemInfo(ArrayList<GoodsFilterInfo.SpecItem> specItems, boolean isOmitted) {
ArrayList<String> result = new ArrayList<>();
int size;
if (isOmitted && specItems.size() > 3) {
size = 3;
} else {
size = specItems.size();
}
for (int i = 0; i < size; i++) {
SpecItem specItem = specItems.get(i);
result.add(specItem.getName());
}
return result;
}
}
package com.mylinkbuy.android.adapter;
import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.mylinkbuy.android.R;
import com.mylinkbuy.android.view.TagWidget;
import java.util.ArrayList;
/**
* **********************************************
* <p/>
* Date: 2018-01-31 11:17
* <p/>
* Author: SinPingWu
* <p/>
* Email: wuxinping@ubinavi.com.cn
* <p/>
* brief:
* <p/>
* history:
* <p/>
* **********************************************
*/
public class GoodsSubMenuAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mMenuItems;
private SparseBooleanArray mSelectedIndex = new SparseBooleanArray();
GoodsSubMenuAdapter(Context context, ArrayList<String> menuItems) {
this.mContext = context;
setParam(menuItems);
}
/**
* 清除选中的项目
*/
public void clearSelectedIndex() {
if (mSelectedIndex != null) {
mSelectedIndex.clear();
}
}
public void setData(ArrayList<String> menuItems) {
setParam(menuItems);
notifyDataSetChanged();
}
private void setParam(ArrayList<String> menuItems) {
if (menuItems == null) {
mMenuItems = new ArrayList<>();
} else {
mMenuItems = new ArrayList<>(menuItems);
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.goods_filter_menu_sub_item, null);
viewHolder = new ViewHolder();
viewHolder.widget = (TagWidget) convertView.findViewById(R.id.tag_widget);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String content = mMenuItems.get(position);
viewHolder.widget.setContent(content);
boolean isSelected;
try {
isSelected = mSelectedIndex.get(position);
} catch (Exception e) {
isSelected = false;
}
if (isSelected != viewHolder.widget.getStatus()) {
viewHolder.widget.toggle();
}
viewHolder.widget.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.widget.toggle();
mSelectedIndex.put(position, viewHolder.widget.getStatus());
}
});
return convertView;
}
@Override
public int getCount() {
return mMenuItems.size();
}
@Override
public Object getItem(int position) {
return mMenuItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TagWidget widget;
}
}