|
|
@@ -1,2 +1,379 @@
|
|
|
-package com.yunbao.main.activity;public class GoodsFilterActivity {
|
|
|
-}
|
|
|
+package com.yunbao.main.activity;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.graphics.drawable.Drawable;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.text.Editable;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.text.TextWatcher;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.EditText;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.LinearLayout;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import androidx.core.content.ContextCompat;
|
|
|
+import androidx.recyclerview.widget.GridLayoutManager;
|
|
|
+import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.yunbao.common.activity.AbsActivity;
|
|
|
+import com.yunbao.common.adapter.RefreshAdapter;
|
|
|
+import com.yunbao.common.bean.GoodsBean;
|
|
|
+import com.yunbao.common.custom.CommonRefreshView;
|
|
|
+import com.yunbao.common.custom.ItemDecoration;
|
|
|
+import com.yunbao.common.http.HttpCallback;
|
|
|
+import com.yunbao.common.interfaces.OnItemClickListener;
|
|
|
+import com.yunbao.common.utils.DpUtil;
|
|
|
+import com.yunbao.common.utils.ToastUtil;
|
|
|
+import com.yunbao.main.R;
|
|
|
+import com.yunbao.main.adapter.EcommercePrimaryFilterAdapter;
|
|
|
+import com.yunbao.main.adapter.EcommerceSecondaryFilterAdapter;
|
|
|
+import com.yunbao.main.adapter.EcommerceProductAdapter;
|
|
|
+import com.yunbao.main.adapter.MallClassAdapter;
|
|
|
+import com.yunbao.main.bean.EcommerceCategoryBean;
|
|
|
+import com.yunbao.main.bean.EcommerceProductBean;
|
|
|
+import com.yunbao.main.bean.MainMallGoodsBean;
|
|
|
+import com.yunbao.main.http.MainHttpUtil;
|
|
|
+import com.yunbao.main.views.AbsMainViewHolder;
|
|
|
+import com.yunbao.mall.activity.GoodsDetailActivity;
|
|
|
+import com.yunbao.mall.bean.GoodsSimpleBean;
|
|
|
+import com.yunbao.mall.bean.GoodsHomeClassBean;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class GoodsFilterActivity extends AbsActivity implements OnItemClickListener<GoodsSimpleBean>, View.OnClickListener {
|
|
|
+
|
|
|
+ public static void forward(Context context) {
|
|
|
+ Intent intent = new Intent(context, GoodsFilterActivity.class);
|
|
|
+ context.startActivity(intent);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增带classId参数的forward方法
|
|
|
+ public static void forward(Context context, String classId) {
|
|
|
+ Intent intent = new Intent(context, GoodsFilterActivity.class);
|
|
|
+ intent.putExtra("classId", classId);
|
|
|
+ context.startActivity(intent);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final String SORT_DESC = "desc";
|
|
|
+ private static final String SORT_ASC = "asc";
|
|
|
+ private static final String SORT_NONE = "";
|
|
|
+
|
|
|
+ private EditText mEtSearch;
|
|
|
+ private RecyclerView mRvPrimaryFilter;
|
|
|
+ private RecyclerView mRvSecondaryFilter;
|
|
|
+ private CommonRefreshView mRefreshView;
|
|
|
+ private LinearLayout mEmptyView;
|
|
|
+
|
|
|
+ private EcommercePrimaryFilterAdapter mPrimaryFilterAdapter;
|
|
|
+ private EcommerceSecondaryFilterAdapter mSecondaryFilterAdapter;
|
|
|
+ private EcommerceProductAdapter mProductAdapter;
|
|
|
+
|
|
|
+ // 修改为使用GoodsHomeClassBean
|
|
|
+ private List<GoodsHomeClassBean> mPrimaryCategoryList;
|
|
|
+ private List<GoodsHomeClassBean> mSecondaryCategoryList;
|
|
|
+
|
|
|
+ private String mCurrentPrimaryCategoryId = "";
|
|
|
+ private String mCurrentSecondaryCategoryId = "";
|
|
|
+ private String mSearchKeyword = "";
|
|
|
+ private String mClassId = ""; // 新增classId字段
|
|
|
+ private String mSaleSort = SORT_NONE;
|
|
|
+ private String mPriceSort = SORT_NONE;
|
|
|
+ private int mIsNew;
|
|
|
+ @Override
|
|
|
+ protected boolean isStatusBarWhite() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected int getLayoutId() {
|
|
|
+ return R.layout.activity_filter_goods;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void main() {
|
|
|
+ setTitle("商品列表");
|
|
|
+ // 获取传入的classId参数
|
|
|
+ Intent intent = getIntent();
|
|
|
+ if (intent != null) {
|
|
|
+ mClassId = intent.getStringExtra("classId");
|
|
|
+ if (mClassId == null) {
|
|
|
+ mClassId = "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ initView();
|
|
|
+ initAdapter();
|
|
|
+ loadPrimaryCategories();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initView() {
|
|
|
+ mEtSearch = findViewById(R.id.et_search);
|
|
|
+ mRvPrimaryFilter = findViewById(R.id.rv_primary_filter);
|
|
|
+ mRvSecondaryFilter = findViewById(R.id.rv_secondary_filter);
|
|
|
+// mEmptyView = findViewById(R.id.empty_view);
|
|
|
+
|
|
|
+ // 搜索框监听
|
|
|
+ mEtSearch.addTextChangedListener(new TextWatcher() {
|
|
|
+ @Override
|
|
|
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterTextChanged(Editable s) {
|
|
|
+ mSearchKeyword = s.toString().trim();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initAdapter() {
|
|
|
+ // 一级筛选项适配器 - 修改为使用GoodsHomeClassBean
|
|
|
+ mPrimaryCategoryList = new ArrayList<>();
|
|
|
+ mPrimaryFilterAdapter = new EcommercePrimaryFilterAdapter(mContext, convertToEcommerceCategoryList(mPrimaryCategoryList));
|
|
|
+ mPrimaryFilterAdapter.setOnItemClickListener(new EcommercePrimaryFilterAdapter.OnItemClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onItemClick(EcommerceCategoryBean category, int position) {
|
|
|
+ mCurrentPrimaryCategoryId = category.getId();
|
|
|
+ mPrimaryFilterAdapter.setSelectedPosition(position);
|
|
|
+ loadSecondaryCategories(category.getId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mRvPrimaryFilter.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
|
|
|
+ mRvPrimaryFilter.setAdapter(mPrimaryFilterAdapter);
|
|
|
+
|
|
|
+ // 二级筛选项适配器 - 修改为使用GoodsHomeClassBean
|
|
|
+ mSecondaryCategoryList = new ArrayList<>();
|
|
|
+ mSecondaryFilterAdapter = new EcommerceSecondaryFilterAdapter(mContext, convertToEcommerceCategoryList(mSecondaryCategoryList));
|
|
|
+ mSecondaryFilterAdapter.setOnItemClickListener(new EcommerceSecondaryFilterAdapter.OnItemClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onItemClick(EcommerceCategoryBean category, int position) {
|
|
|
+ mCurrentSecondaryCategoryId = category.getId();
|
|
|
+ mSecondaryFilterAdapter.setSelectedPosition(position);
|
|
|
+ loadData();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mRvSecondaryFilter.setLayoutManager(new LinearLayoutManager(mContext));
|
|
|
+ mRvSecondaryFilter.setAdapter(mSecondaryFilterAdapter);
|
|
|
+
|
|
|
+ // 商品列表适配器
|
|
|
+ mRefreshView = findViewById(R.id.refresh_view);
|
|
|
+ mRefreshView.setEmptyLayoutId(R.layout.view_no_data_main_mall_class);
|
|
|
+ mRefreshView.setLayoutManager(new GridLayoutManager(mContext, 1, GridLayoutManager.VERTICAL, false));
|
|
|
+ mRefreshView.setDataHelper(new CommonRefreshView.DataHelper<GoodsSimpleBean>() {
|
|
|
+ @Override
|
|
|
+ public RefreshAdapter<GoodsSimpleBean> getAdapter() {
|
|
|
+ if (mProductAdapter == null) {
|
|
|
+ mProductAdapter = new EcommerceProductAdapter(mContext);
|
|
|
+ }
|
|
|
+ return mProductAdapter;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void loadData(int p, HttpCallback callback) {
|
|
|
+ MainHttpUtil.getShopClassList(mCurrentSecondaryCategoryId, mSaleSort, mPriceSort, mIsNew, p, callback);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<GoodsSimpleBean> processData(String[] info) {
|
|
|
+ return JSON.parseArray(Arrays.toString(info), GoodsSimpleBean.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRefreshSuccess(List<GoodsSimpleBean> list, int listCount) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRefreshFailure() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onLoadMoreSuccess(List<GoodsSimpleBean> loadItemList, int loadItemCount) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onLoadMoreFailure() {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadData() {
|
|
|
+ if (mRefreshView != null) {
|
|
|
+ mRefreshView.initData();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增转换方法:将GoodsHomeClassBean列表转换为EcommerceCategoryBean列表
|
|
|
+ private List<EcommerceCategoryBean> convertToEcommerceCategoryList(List<GoodsHomeClassBean> goodsHomeClassList) {
|
|
|
+ List<EcommerceCategoryBean> ecommerceCategoryList = new ArrayList<>();
|
|
|
+ if (goodsHomeClassList != null) {
|
|
|
+ for (GoodsHomeClassBean goodsHomeClass : goodsHomeClassList) {
|
|
|
+ EcommerceCategoryBean ecommerceCategory = new EcommerceCategoryBean(
|
|
|
+ goodsHomeClass.getId(),
|
|
|
+ goodsHomeClass.getName(),
|
|
|
+ goodsHomeClass.getIcon()
|
|
|
+ );
|
|
|
+ ecommerceCategoryList.add(ecommerceCategory);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ecommerceCategoryList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onItemClick(GoodsSimpleBean bean, int position) {
|
|
|
+// GoodsDetailActivity.forward(mContext, bean.getId(), false, bean.getType());
|
|
|
+ GoodsDetailActivity.forward(mContext, bean.getId(), false, bean.getType());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadPrimaryCategories() {
|
|
|
+ // 使用MainHttpUtil.getHomeTwoClassList接口调用
|
|
|
+ int shopClassId = 1;
|
|
|
+ if (!TextUtils.isEmpty(mClassId)) {
|
|
|
+ try {
|
|
|
+ shopClassId = Integer.parseInt(mClassId);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ shopClassId = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ MainHttpUtil.getHomeTwoClassList(1, shopClassId, new HttpCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(int code, String msg, String[] info) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 直接解析为GoodsHomeClassBean
|
|
|
+ List<GoodsHomeClassBean> categories = JSON.parseArray(Arrays.toString(info), GoodsHomeClassBean.class);
|
|
|
+ if (categories == null) {
|
|
|
+ categories = new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加"全部"选项
|
|
|
+ List<GoodsHomeClassBean> finalCategories = new ArrayList<>();
|
|
|
+// GoodsHomeClassBean allCategory = new GoodsHomeClassBean();
|
|
|
+// allCategory.setId("0");
|
|
|
+// allCategory.setName("全部");
|
|
|
+// allCategory.setIcon("");
|
|
|
+// finalCategories.add(allCategory);
|
|
|
+ finalCategories.addAll(categories);
|
|
|
+
|
|
|
+ mPrimaryCategoryList.clear();
|
|
|
+ mPrimaryCategoryList.addAll(finalCategories);
|
|
|
+
|
|
|
+ // 更新适配器数据
|
|
|
+ List<EcommerceCategoryBean> ecommerceCategoryList = convertToEcommerceCategoryList(mPrimaryCategoryList);
|
|
|
+ mPrimaryFilterAdapter = new EcommercePrimaryFilterAdapter(mContext, ecommerceCategoryList);
|
|
|
+ mPrimaryFilterAdapter.setOnItemClickListener(new EcommercePrimaryFilterAdapter.OnItemClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onItemClick(EcommerceCategoryBean category, int position) {
|
|
|
+ mCurrentPrimaryCategoryId = category.getId();
|
|
|
+ mPrimaryFilterAdapter.setSelectedPosition(position);
|
|
|
+ loadSecondaryCategories(category.getId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mRvPrimaryFilter.setAdapter(mPrimaryFilterAdapter);
|
|
|
+
|
|
|
+ // 默认选中第一个
|
|
|
+ if (!finalCategories.isEmpty()) {
|
|
|
+ mCurrentPrimaryCategoryId = finalCategories.get(0).getId();
|
|
|
+ mPrimaryFilterAdapter.setSelectedPosition(0);
|
|
|
+ loadSecondaryCategories(mCurrentPrimaryCategoryId);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ ToastUtil.show("请求出错,请重试");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError() {
|
|
|
+// toast提示
|
|
|
+ ToastUtil.show("请求出错,请重试");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadSecondaryCategories(String primaryCategoryId) {
|
|
|
+ // 使用getShopThreeClass接口获取二级分类数据
|
|
|
+ if (!TextUtils.isEmpty(primaryCategoryId)) {
|
|
|
+ MainHttpUtil.getShopThreeClass(primaryCategoryId, new HttpCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(int code, String msg, String[] info) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 直接解析为GoodsHomeClassBean
|
|
|
+ List<GoodsHomeClassBean> categories = JSON.parseArray(Arrays.toString(info), GoodsHomeClassBean.class);
|
|
|
+ if (categories == null) {
|
|
|
+ categories = new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加"全部"选项
|
|
|
+ List<GoodsHomeClassBean> finalCategories = new ArrayList<>();
|
|
|
+// GoodsHomeClassBean allCategory = new GoodsHomeClassBean();
|
|
|
+// allCategory.setId("0");
|
|
|
+// allCategory.setName("全部");
|
|
|
+// allCategory.setIcon("");
|
|
|
+// finalCategories.add(allCategory);
|
|
|
+ finalCategories.addAll(categories);
|
|
|
+
|
|
|
+ mSecondaryCategoryList.clear();
|
|
|
+ mSecondaryCategoryList.addAll(finalCategories);
|
|
|
+
|
|
|
+ // 更新适配器数据
|
|
|
+ List<EcommerceCategoryBean> ecommerceCategoryList = convertToEcommerceCategoryList(mSecondaryCategoryList);
|
|
|
+ mSecondaryFilterAdapter = new EcommerceSecondaryFilterAdapter(mContext, ecommerceCategoryList);
|
|
|
+ mSecondaryFilterAdapter.setOnItemClickListener(new EcommerceSecondaryFilterAdapter.OnItemClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onItemClick(EcommerceCategoryBean category, int position) {
|
|
|
+ mCurrentSecondaryCategoryId = category.getId();
|
|
|
+ mSecondaryFilterAdapter.setSelectedPosition(position);
|
|
|
+ loadData();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mRvSecondaryFilter.setAdapter(mSecondaryFilterAdapter);
|
|
|
+
|
|
|
+ // 默认选中第一个
|
|
|
+ if (!finalCategories.isEmpty()) {
|
|
|
+ mCurrentSecondaryCategoryId = finalCategories.get(0).getId();
|
|
|
+ mSecondaryFilterAdapter.setSelectedPosition(0);
|
|
|
+ loadData();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ ToastUtil.show("请求出错,请重试");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError() {
|
|
|
+ ToastUtil.show("请求出错,请重试");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ int id = v.getId();
|
|
|
+ if (id == R.id.btn_search) {
|
|
|
+ performSearch();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void performSearch() {
|
|
|
+ if (TextUtils.isEmpty(mSearchKeyword)) {
|
|
|
+ ToastUtil.show("请输入搜索关键词");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ToastUtil.show("搜索: " + mSearchKeyword);
|
|
|
+ }
|
|
|
+}
|