|
|
@@ -2,19 +2,43 @@ package com.yunbao.main.views;
|
|
|
|
|
|
import android.content.Context;
|
|
|
import android.graphics.Typeface;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Looper;
|
|
|
+import android.text.TextUtils;
|
|
|
import android.view.Gravity;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
import android.widget.HorizontalScrollView;
|
|
|
import android.widget.LinearLayout;
|
|
|
import android.widget.TextView;
|
|
|
-
|
|
|
-import androidx.fragment.app.Fragment;
|
|
|
-import androidx.fragment.app.FragmentActivity;
|
|
|
-import androidx.viewpager2.adapter.FragmentStateAdapter;
|
|
|
-import androidx.viewpager2.widget.ViewPager2;
|
|
|
-
|
|
|
+import android.view.inputmethod.EditorInfo;
|
|
|
+import android.widget.EditText;
|
|
|
+import android.view.inputmethod.InputMethodManager;
|
|
|
+
|
|
|
+import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.yunbao.common.CommonAppConfig;
|
|
|
+import com.yunbao.common.activity.AbsActivity;
|
|
|
+import com.yunbao.common.bean.VideoBean;
|
|
|
+import com.yunbao.common.custom.CommonRefreshView;
|
|
|
+import com.yunbao.common.event.LocationCityEvent;
|
|
|
+import com.yunbao.common.event.LocationEvent;
|
|
|
+import com.yunbao.common.http.HttpCallback;
|
|
|
+import com.yunbao.common.interfaces.OnItemClickListener;
|
|
|
+import com.yunbao.common.utils.LocationUtil;
|
|
|
import com.yunbao.main.R;
|
|
|
+import com.yunbao.main.activity.ShopInfoActivity;
|
|
|
+import com.yunbao.main.adapter.ShopListAdapter;
|
|
|
+import com.yunbao.main.bean.AddressBean;
|
|
|
+import com.yunbao.main.bean.ShopItem;
|
|
|
+import com.yunbao.main.http.MainHttpUtil;
|
|
|
+
|
|
|
+import org.greenrobot.eventbus.EventBus;
|
|
|
+import org.greenrobot.eventbus.Subscribe;
|
|
|
+import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
@@ -22,9 +46,10 @@ import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 农村电商主页ViewHolder
|
|
|
- * 展示地区选择、商品分类、推荐商品等功能
|
|
|
+ * 展示地区选择、商家列表等功能
|
|
|
+ * 支持下拉刷新和上拉加载更多
|
|
|
*/
|
|
|
-public class MainShopRuralViewHolder extends AbsMainViewHolder implements View.OnClickListener {
|
|
|
+public class MainShopRuralViewHolder extends AbsMainViewHolder implements View.OnClickListener, OnItemClickListener<ShopItem> {
|
|
|
|
|
|
// 顶部控件
|
|
|
private LinearLayout mLocationLayout;
|
|
|
@@ -33,41 +58,35 @@ public class MainShopRuralViewHolder extends AbsMainViewHolder implements View.O
|
|
|
// 地区标签相关
|
|
|
private HorizontalScrollView mRegionScrollView;
|
|
|
private LinearLayout mRegionContainer;
|
|
|
+ // 搜索框
|
|
|
+ private EditText mEditShopName;
|
|
|
+ private String mSearchKeyword = "";
|
|
|
private List<RegionTabItem> mRegionTabItems;
|
|
|
private int mCurrentSelectedRegion = 0;
|
|
|
|
|
|
// 地区数据
|
|
|
private List<String> mRegionList;
|
|
|
|
|
|
- // 导航栏和ViewPager
|
|
|
- private TextView mNavHome;
|
|
|
- private TextView mNavAllGoods;
|
|
|
- private TextView mNavReview;
|
|
|
- private ViewPager2 mViewPager;
|
|
|
- private ShopContentAdapter mContentAdapter;
|
|
|
- private int mCurrentNavIndex = 0;
|
|
|
+ // 商家列表相关
|
|
|
+ private CommonRefreshView mRefreshView;
|
|
|
+ private ShopListAdapter mShopAdapter;
|
|
|
+ private String mCurrentRegionName = "";
|
|
|
|
|
|
- /**
|
|
|
- * 地区标签项数据类
|
|
|
- */
|
|
|
- private static class RegionTabItem {
|
|
|
- LinearLayout container;
|
|
|
- TextView textView;
|
|
|
- View underline;
|
|
|
- String regionName;
|
|
|
- int index;
|
|
|
-
|
|
|
- RegionTabItem(LinearLayout container, TextView textView, View underline, String regionName, int index) {
|
|
|
- this.container = container;
|
|
|
- this.textView = textView;
|
|
|
- this.underline = underline;
|
|
|
- this.regionName = regionName;
|
|
|
- this.index = index;
|
|
|
- }
|
|
|
- }
|
|
|
+ private int mShopType; // 店铺类型:1乡村云店 2都市云店 3一线云店
|
|
|
+
|
|
|
+ private InputMethodManager mInputMethodManager;
|
|
|
+
|
|
|
+ // 添加请求序列号,用于防止请求竞态
|
|
|
+ private volatile long mCurrentRequestId = 0;
|
|
|
+ private final Object mRequestLock = new Object();
|
|
|
+
|
|
|
+ private Handler mDebounceHandler = new Handler(Looper.getMainLooper());
|
|
|
+ private Runnable mDebounceRunnable;
|
|
|
+ private static final int DEBOUNCE_DELAY = 300;
|
|
|
|
|
|
- public MainShopRuralViewHolder(Context context, ViewGroup parentView) {
|
|
|
+ public MainShopRuralViewHolder(Context context, ViewGroup parentView, int shopType) {
|
|
|
super(context, parentView);
|
|
|
+ this.mShopType = shopType;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -86,469 +105,479 @@ public class MainShopRuralViewHolder extends AbsMainViewHolder implements View.O
|
|
|
mRegionContainer = findViewById(R.id.ll_region_container);
|
|
|
mRegionTabItems = new ArrayList<>();
|
|
|
|
|
|
- // 初始化地区数据
|
|
|
- initRegionData();
|
|
|
-
|
|
|
- // 动态创建地区标签
|
|
|
- createRegionTabs();
|
|
|
-
|
|
|
- // 初始化导航栏和ViewPager
|
|
|
- initNavigationTabs();
|
|
|
-
|
|
|
- // 初始化商品分类
|
|
|
- initCategoryItems();
|
|
|
+ // 初始化商家列表刷新视图
|
|
|
+ mRefreshView = findViewById(R.id.refresh_view);
|
|
|
+
|
|
|
+ // 初始化搜索框
|
|
|
+ mEditShopName = findViewById(R.id.edit_shop_name);
|
|
|
+ mInputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
+ initSearchBox();
|
|
|
|
|
|
+ // 初始化商家列表
|
|
|
+ initShopRefreshView();
|
|
|
+
|
|
|
// 设置点击事件
|
|
|
setClickListeners();
|
|
|
|
|
|
- // 设置示例数据
|
|
|
- setupSampleData();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 初始化地区数据
|
|
|
- */
|
|
|
- private void initRegionData() {
|
|
|
- // 示例地区数据,实际项目中可以从服务器获取或配置文件读取
|
|
|
- mRegionList = Arrays.asList(
|
|
|
- "山东", "北京", "湖北", "上海", "河南",
|
|
|
- "陕西", "四川", "广东", "江苏", "浙江",
|
|
|
- "湖南", "安徽", "河北", "山西", "辽宁"
|
|
|
- );
|
|
|
+ // 注册EventBus监听定位事件
|
|
|
+ if (!EventBus.getDefault().isRegistered(this)) {
|
|
|
+ EventBus.getDefault().register(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 先启动定位,定位成功后再请求地址列表
|
|
|
+ initLocationInfo();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 动态创建地区标签
|
|
|
+ * 初始化商家列表刷新视图
|
|
|
*/
|
|
|
- private void createRegionTabs() {
|
|
|
- if (mRegionContainer == null || mRegionList == null) {
|
|
|
+ private void initShopRefreshView() {
|
|
|
+ if (mRefreshView == null) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- // 清空现有标签
|
|
|
- mRegionContainer.removeAllViews();
|
|
|
- mRegionTabItems.clear();
|
|
|
-
|
|
|
- for (int i = 0; i < mRegionList.size(); i++) {
|
|
|
- String regionName = mRegionList.get(i);
|
|
|
- RegionTabItem tabItem = createRegionTabItem(regionName, i);
|
|
|
- mRegionTabItems.add(tabItem);
|
|
|
- mRegionContainer.addView(tabItem.container);
|
|
|
- }
|
|
|
-
|
|
|
- // 设置默认选中第一个
|
|
|
- if (!mRegionTabItems.isEmpty()) {
|
|
|
- updateRegionSelection(0);
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 创建单个地区标签项
|
|
|
- */
|
|
|
- private RegionTabItem createRegionTabItem(String regionName, int index) {
|
|
|
- // 创建容器
|
|
|
- LinearLayout container = new LinearLayout(mContext);
|
|
|
- LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams(
|
|
|
- LinearLayout.LayoutParams.WRAP_CONTENT,
|
|
|
- LinearLayout.LayoutParams.WRAP_CONTENT
|
|
|
- );
|
|
|
- containerParams.rightMargin = dpToPx(24);
|
|
|
- container.setLayoutParams(containerParams);
|
|
|
- container.setOrientation(LinearLayout.VERTICAL);
|
|
|
- container.setGravity(Gravity.CENTER);
|
|
|
- container.setPadding(dpToPx(12), dpToPx(6), dpToPx(12), dpToPx(6));
|
|
|
- container.setTag(index); // 设置标签用于点击识别
|
|
|
-
|
|
|
- // 创建文本
|
|
|
- TextView textView = new TextView(mContext);
|
|
|
- LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
|
|
|
- LinearLayout.LayoutParams.WRAP_CONTENT,
|
|
|
- LinearLayout.LayoutParams.WRAP_CONTENT
|
|
|
- );
|
|
|
- textView.setLayoutParams(textParams);
|
|
|
- textView.setText(regionName);
|
|
|
- textView.setTextSize(14);
|
|
|
- textView.setTextColor(0xFF999999); // 默认未选中颜色
|
|
|
+ // 设置布局管理器
|
|
|
+ mRefreshView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
|
|
|
|
|
|
- // 创建下划线
|
|
|
- View underline = new View(mContext);
|
|
|
- LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(
|
|
|
- dpToPx(20),
|
|
|
- dpToPx(2)
|
|
|
- );
|
|
|
- lineParams.topMargin = dpToPx(4);
|
|
|
- underline.setLayoutParams(lineParams);
|
|
|
- underline.setBackgroundColor(0xFF333333);
|
|
|
- underline.setVisibility(View.INVISIBLE); // 默认隐藏
|
|
|
-
|
|
|
- // 添加到容器
|
|
|
- container.addView(textView);
|
|
|
- container.addView(underline);
|
|
|
-
|
|
|
- // 设置点击事件
|
|
|
- container.setOnClickListener(this);
|
|
|
-
|
|
|
- return new RegionTabItem(container, textView, underline, regionName, index);
|
|
|
- }
|
|
|
+ // 设置数据助手
|
|
|
+ mRefreshView.setDataHelper(new CommonRefreshView.DataHelper<ShopItem>() {
|
|
|
+ @Override
|
|
|
+ public com.yunbao.common.adapter.RefreshAdapter<ShopItem> getAdapter() {
|
|
|
+ if (mShopAdapter == null) {
|
|
|
+ mShopAdapter = new ShopListAdapter(mContext);
|
|
|
+ mShopAdapter.setOnItemClickListener(MainShopRuralViewHolder.this);
|
|
|
+ }
|
|
|
+ return mShopAdapter;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * dp转px
|
|
|
- */
|
|
|
- private int dpToPx(int dp) {
|
|
|
- float density = mContext.getResources().getDisplayMetrics().density;
|
|
|
- return (int) (dp * density + 0.5f);
|
|
|
+ @Override
|
|
|
+ public void loadData(int page, HttpCallback callback) {
|
|
|
+ // 获取当前请求ID
|
|
|
+ final long currentRequestId;
|
|
|
+ synchronized (mRequestLock) {
|
|
|
+ currentRequestId = mCurrentRequestId;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加日志追踪loadData方法的调用
|
|
|
+ android.util.Log.d("MainShopRuralViewHolder", "loadData - 开始加载数据,页码: " + page + ", 请求ID: " + currentRequestId);
|
|
|
+
|
|
|
+ // 获取当前省份信息
|
|
|
+ String province = CommonAppConfig.getInstance().getProvince();
|
|
|
+ if (TextUtils.isEmpty(province)) {
|
|
|
+ province = "";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用当前选中的地区名称作为搜索条件
|
|
|
+ String searchProvince = TextUtils.isEmpty(mCurrentRegionName) ? "" : mCurrentRegionName;
|
|
|
+
|
|
|
+ android.util.Log.d("MainShopRuralViewHolder", "loadData - 请求参数: shopType=" + mShopType + ", province=" + province + ", searchProvince=" + searchProvince);
|
|
|
+
|
|
|
+ // 调用接口,传入搜索关键词
|
|
|
+ MainHttpUtil.getClassidShopList(mShopType, mSearchKeyword, mCurrentRegionName, page, new HttpCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(int code, String msg, String[] info) {
|
|
|
+ // 检查请求是否已过期
|
|
|
+ synchronized (mRequestLock) {
|
|
|
+ if (currentRequestId != mCurrentRequestId) {
|
|
|
+ android.util.Log.d("MainShopRuralViewHolder", "请求已过期,忽略结果。当前ID: " + mCurrentRequestId + ", 请求ID: " + currentRequestId);
|
|
|
+ return; // 请求已过期,忽略结果
|
|
|
+ }
|
|
|
+ }
|
|
|
+ callback.onSuccess(code, msg, info);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError() {
|
|
|
+ // 检查请求是否已过期
|
|
|
+ synchronized (mRequestLock) {
|
|
|
+ if (currentRequestId != mCurrentRequestId) {
|
|
|
+ android.util.Log.d("MainShopRuralViewHolder", "请求已过期,忽略错误。当前ID: " + mCurrentRequestId + ", 请求ID: " + currentRequestId);
|
|
|
+ return; // 请求已过期,忽略错误
|
|
|
+ }
|
|
|
+ }
|
|
|
+ callback.onError();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFinish() {
|
|
|
+ // 检查请求是否已过期
|
|
|
+ synchronized (mRequestLock) {
|
|
|
+ if (currentRequestId != mCurrentRequestId) {
|
|
|
+ android.util.Log.d("MainShopRuralViewHolder", "请求已过期,忽略完成回调。当前ID: " + mCurrentRequestId + ", 请求ID: " + currentRequestId);
|
|
|
+ return; // 请求已过期,忽略完成回调
|
|
|
+ }
|
|
|
+ }
|
|
|
+ callback.onFinish();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ShopItem> processData(String[] info) {
|
|
|
+ List<ShopItem> list = JSON.parseArray(Arrays.toString(info), ShopItem.class);
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRefreshSuccess(List<ShopItem> list, int listCount) {
|
|
|
+ // 下拉刷新成功回调
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRefreshFailure() {
|
|
|
+ // 下拉刷新失败回调
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onLoadMoreSuccess(List<ShopItem> loadItemList, int loadItemCount) {
|
|
|
+ // 上拉加载更多成功回调
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onLoadMoreFailure() {
|
|
|
+ // 上拉加载更多失败回调
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 更新地区选中状态
|
|
|
+ * 初始化搜索框
|
|
|
*/
|
|
|
- private void updateRegionSelection(int selectedIndex) {
|
|
|
- if (selectedIndex < 0 || selectedIndex >= mRegionTabItems.size()) {
|
|
|
+ private void initSearchBox() {
|
|
|
+ if (mEditShopName == null) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- mCurrentSelectedRegion = selectedIndex;
|
|
|
-
|
|
|
- // 更新所有标签状态
|
|
|
- for (int i = 0; i < mRegionTabItems.size(); i++) {
|
|
|
- RegionTabItem item = mRegionTabItems.get(i);
|
|
|
- if (i == selectedIndex) {
|
|
|
- // 选中状态
|
|
|
- item.textView.setTextColor(0xFF333333);
|
|
|
- item.textView.setTypeface(null, Typeface.BOLD);
|
|
|
- item.underline.setVisibility(View.VISIBLE);
|
|
|
- } else {
|
|
|
- // 未选中状态
|
|
|
- item.textView.setTextColor(0xFF999999);
|
|
|
- item.textView.setTypeface(null, Typeface.NORMAL);
|
|
|
- item.underline.setVisibility(View.INVISIBLE);
|
|
|
+
|
|
|
+ // 设置提示文字
|
|
|
+ mEditShopName.setHint("输入商家名");
|
|
|
+
|
|
|
+ // 设置键盘回车键为搜索键
|
|
|
+ mEditShopName.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
|
|
|
+ mEditShopName.setSingleLine(true);
|
|
|
+
|
|
|
+ // 监听回车键点击事件
|
|
|
+ mEditShopName.setOnEditorActionListener((v, actionId, event) -> {
|
|
|
+ if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
|
|
+ performSearch();
|
|
|
+ return true;
|
|
|
}
|
|
|
- }
|
|
|
+ return false;
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化导航栏和ViewPager
|
|
|
+ * 执行搜索
|
|
|
*/
|
|
|
- private void initNavigationTabs() {
|
|
|
- // 获取导航栏控件
|
|
|
- mNavHome = findViewById(R.id.tv_nav_home);
|
|
|
- mNavAllGoods = findViewById(R.id.tv_nav_all_goods);
|
|
|
- mNavReview = findViewById(R.id.tv_nav_review);
|
|
|
-
|
|
|
- // 获取ViewPager
|
|
|
- mViewPager = findViewById(R.id.vp_content);
|
|
|
-
|
|
|
- // 设置ViewPager适配器
|
|
|
- if (mContext instanceof FragmentActivity) {
|
|
|
- mContentAdapter = new ShopContentAdapter((FragmentActivity) mContext);
|
|
|
- mViewPager.setAdapter(mContentAdapter);
|
|
|
-
|
|
|
- // 设置ViewPager页面切换监听
|
|
|
- mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
|
|
- @Override
|
|
|
- public void onPageSelected(int position) {
|
|
|
- super.onPageSelected(position);
|
|
|
- updateNavigationSelection(position);
|
|
|
- }
|
|
|
- });
|
|
|
+ private void performSearch() {
|
|
|
+ if (mEditShopName == null) {
|
|
|
+ return;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 初始化商品分类项
|
|
|
- */
|
|
|
- private void initCategoryItems() {
|
|
|
- // 商品分类项在布局中已经设置好了
|
|
|
- // 可以在这里添加分类点击逻辑
|
|
|
+ String keyword = mEditShopName.getText().toString().trim();
|
|
|
+ mSearchKeyword = keyword;
|
|
|
+
|
|
|
+ // 显示loading状态
|
|
|
+ if (mRefreshView != null) {
|
|
|
+ mRefreshView.showLoading();
|
|
|
+ mRefreshView.initData();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 隐藏软键盘
|
|
|
+ if (mInputMethodManager != null && mEditShopName != null) {
|
|
|
+ mInputMethodManager.hideSoftInputFromWindow(mEditShopName.getWindowToken(), 0);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置点击事件监听器
|
|
|
+ * 初始化定位信息
|
|
|
*/
|
|
|
- private void setClickListeners() {
|
|
|
- if (mLocationLayout != null) {
|
|
|
- mLocationLayout.setOnClickListener(this);
|
|
|
+ private void initLocationInfo() {
|
|
|
+ // 先尝试从缓存获取省份信息
|
|
|
+ String province = CommonAppConfig.getInstance().getProvince();
|
|
|
+ if (!TextUtils.isEmpty(province)) {
|
|
|
+ initRegionData();
|
|
|
+ } else {
|
|
|
+ // 如果没有缓存的定位信息,则启动定位
|
|
|
+ startLocationIfNeeded();
|
|
|
}
|
|
|
-
|
|
|
- // 设置导航栏点击事件
|
|
|
- if (mNavHome != null) mNavHome.setOnClickListener(this);
|
|
|
- if (mNavAllGoods != null) mNavAllGoods.setOnClickListener(this);
|
|
|
- if (mNavReview != null) mNavReview.setOnClickListener(this);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * 设置示例数据
|
|
|
+ * 启动定位(如果需要)
|
|
|
*/
|
|
|
- private void setupSampleData() {
|
|
|
- // 设置地区文本
|
|
|
- if (mLocationText != null) {
|
|
|
- mLocationText.setText("北京市");
|
|
|
+ private void startLocationIfNeeded() {
|
|
|
+ if (mContext instanceof AbsActivity) {
|
|
|
+ AbsActivity activity = (AbsActivity) mContext;
|
|
|
+ if (activity.hasLocationPermission()) {
|
|
|
+ // 有权限直接启动定位
|
|
|
+ LocationUtil.getInstance().startLocation();
|
|
|
+ } else {
|
|
|
+ // 没有权限则请求权限
|
|
|
+ activity.checkLocationPermission(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ LocationUtil.getInstance().startLocation();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- // 设置默认导航选中状态
|
|
|
- updateNavigationSelection(0);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * 设置地区数据(外部调用)
|
|
|
+ * 更新定位文本显示
|
|
|
*/
|
|
|
- public void setRegionData(List<String> regionList) {
|
|
|
- if (regionList != null && !regionList.isEmpty()) {
|
|
|
- mRegionList = new ArrayList<>(regionList);
|
|
|
- createRegionTabs();
|
|
|
+ private void updateLocationText(String province) {
|
|
|
+ if (mLocationText != null && !TextUtils.isEmpty(province)) {
|
|
|
+ mLocationText.setText(province);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * 获取当前选中的地区
|
|
|
+ * 监听定位事件
|
|
|
*/
|
|
|
- public String getCurrentSelectedRegion() {
|
|
|
- if (mCurrentSelectedRegion >= 0 && mCurrentSelectedRegion < mRegionList.size()) {
|
|
|
- return mRegionList.get(mCurrentSelectedRegion);
|
|
|
- }
|
|
|
- return null;
|
|
|
+ @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
+ public void onLocationEvent(LocationEvent event) {
|
|
|
+ // 定位成功后会触发LocationCityEvent,这里可以不处理
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * 设置选中的地区
|
|
|
+ * 监听定位城市事件
|
|
|
*/
|
|
|
- public void setSelectedRegion(int index) {
|
|
|
- updateRegionSelection(index);
|
|
|
+ @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
+ public void onLocationCityEvent(LocationCityEvent event) {
|
|
|
+ // 定位成功后请求地址列表
|
|
|
+ initRegionData();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置选中的地区
|
|
|
+ * 初始化地区数据
|
|
|
*/
|
|
|
- public void setSelectedRegion(String regionName) {
|
|
|
- if (mRegionList != null) {
|
|
|
- int index = mRegionList.indexOf(regionName);
|
|
|
- if (index >= 0) {
|
|
|
- updateRegionSelection(index);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void loadData() {
|
|
|
- if (isFirstLoadData()) {
|
|
|
- // 首次加载数据
|
|
|
- loadRuralShopData();
|
|
|
- }
|
|
|
+ private void initRegionData() {
|
|
|
+ // 调用getShopAddressList接口获取店铺地址列表
|
|
|
+ loadShopAddressListFromServer();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * 加载农村电商数据
|
|
|
+ * 从服务器加载店铺地址列表
|
|
|
*/
|
|
|
- private void loadRuralShopData() {
|
|
|
- // TODO: 实现农村电商数据加载逻辑
|
|
|
- // 1. 加载地区列表
|
|
|
- // 2. 加载推荐商品
|
|
|
- // 3. 加载商品分类
|
|
|
- // 4. 加载广告横幅
|
|
|
-
|
|
|
- // 示例:模拟数据加载完成
|
|
|
- setupSampleData();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onClick(View v) {
|
|
|
- int id = v.getId();
|
|
|
- if (id == R.id.ll_location) {
|
|
|
- // 点击地区选择
|
|
|
- onLocationClick();
|
|
|
- } else if (id == R.id.tv_nav_home) {
|
|
|
- // 点击首页导航
|
|
|
- switchToPage(0);
|
|
|
- } else if (id == R.id.tv_nav_all_goods) {
|
|
|
- // 点击全部商品导航
|
|
|
- switchToPage(1);
|
|
|
- } else if (id == R.id.tv_nav_review) {
|
|
|
- // 点击评价导航
|
|
|
- switchToPage(2);
|
|
|
- } else {
|
|
|
- // 检查是否是地区标签点击
|
|
|
- Object tag = v.getTag();
|
|
|
- if (tag instanceof Integer) {
|
|
|
- int regionIndex = (Integer) tag;
|
|
|
- updateRegionSelection(regionIndex);
|
|
|
- onRegionClick(mRegionList.get(regionIndex));
|
|
|
+ private void loadShopAddressListFromServer() {
|
|
|
+ MainHttpUtil.getShopAddressList(new HttpCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(int code, String msg, String[] info) {
|
|
|
+ try {
|
|
|
+ if (info != null && info.length > 0) {
|
|
|
+ // 解析返回的地址数据
|
|
|
+ List<AddressBean> addressList = JSON.parseArray(Arrays.toString(info), AddressBean.class);
|
|
|
+
|
|
|
+ // 获取当前定位的省份
|
|
|
+ String currentProvince = CommonAppConfig.getInstance().getProvince();
|
|
|
+
|
|
|
+ // 构建地区列表(不包含"全部"选项)
|
|
|
+ List<String> regionList = new ArrayList<>();
|
|
|
+
|
|
|
+ // 用于记录定位地址是否在列表中
|
|
|
+ boolean hasLocationProvince = false;
|
|
|
+ int locationProvinceIndex = -1;
|
|
|
+
|
|
|
+ // 从AddressBean中提取province字段
|
|
|
+ List<String> provinceList = new ArrayList<>();
|
|
|
+ for (AddressBean addressBean : addressList) {
|
|
|
+ if (addressBean != null && !TextUtils.isEmpty(addressBean.getProvince())) {
|
|
|
+ String province = addressBean.getProvince();
|
|
|
+ provinceList.add(province);
|
|
|
+
|
|
|
+ // 检查是否包含定位地址
|
|
|
+ if (!TextUtils.isEmpty(currentProvince) && currentProvince.equals(province)) {
|
|
|
+ hasLocationProvince = true;
|
|
|
+ locationProvinceIndex = provinceList.size() - 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果包含定位地址,将其置为第一个
|
|
|
+ if (hasLocationProvince && locationProvinceIndex >= 0) {
|
|
|
+ String locationProvince = provinceList.get(locationProvinceIndex);
|
|
|
+ provinceList.remove(locationProvinceIndex);
|
|
|
+ regionList.add(locationProvince); // 添加到第一个位置
|
|
|
+ regionList.addAll(provinceList); // 添加其余地址
|
|
|
+ } else {
|
|
|
+ // 如果不包含定位地址,按原顺序添加
|
|
|
+ regionList.addAll(provinceList);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新地区列表
|
|
|
+ mRegionList = regionList;
|
|
|
+ // 重新创建地区标签
|
|
|
+ createRegionTabs();
|
|
|
+ selectRegionTab(mCurrentSelectedRegion);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ // 如果解析失败,创建空列表
|
|
|
+ mRegionList = new ArrayList<>();
|
|
|
+ createRegionTabs();
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError() {
|
|
|
+ // 网络请求失败时,创建空列表
|
|
|
+ mRegionList = new ArrayList<>();
|
|
|
+ createRegionTabs();
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 处理地区选择点击
|
|
|
+ * 动态创建地区标签
|
|
|
*/
|
|
|
- private void onLocationClick() {
|
|
|
- // TODO: 打开地区选择页面
|
|
|
- // 例如:启动城市选择Activity
|
|
|
- }
|
|
|
+ private void createRegionTabs() {
|
|
|
+ if (mRegionContainer == null || mRegionList == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 处理地区标签点击
|
|
|
- */
|
|
|
- private void onRegionClick(String regionName) {
|
|
|
- // TODO: 根据选中的地区加载对应数据
|
|
|
- // 例如:刷新商品列表、更新推荐内容等
|
|
|
- }
|
|
|
+ mRegionContainer.removeAllViews();
|
|
|
+ mRegionTabItems.clear();
|
|
|
|
|
|
- /**
|
|
|
- * 切换到指定页面
|
|
|
- */
|
|
|
- private void switchToPage(int pageIndex) {
|
|
|
- if (mViewPager != null && pageIndex >= 0 && pageIndex < 3) {
|
|
|
- mViewPager.setCurrentItem(pageIndex, true);
|
|
|
+ for (int i = 0; i < mRegionList.size(); i++) {
|
|
|
+ String regionName = mRegionList.get(i);
|
|
|
+ RegionTabItem tabItem = createRegionTabItem(regionName, i);
|
|
|
+ mRegionTabItems.add(tabItem);
|
|
|
+ mRegionContainer.addView(tabItem.textView);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 更新导航栏选中状态
|
|
|
+ * 创建地区标签项
|
|
|
*/
|
|
|
- private void updateNavigationSelection(int selectedIndex) {
|
|
|
- mCurrentNavIndex = selectedIndex;
|
|
|
+ private RegionTabItem createRegionTabItem(String regionName, int index) {
|
|
|
+ TextView textView = new TextView(mContext);
|
|
|
+ textView.setText(regionName);
|
|
|
+ textView.setTextSize(14);
|
|
|
+ textView.setPadding(24, 12, 24, 12);
|
|
|
+ textView.setGravity(Gravity.CENTER);
|
|
|
|
|
|
- // 重置所有导航项状态
|
|
|
- resetNavigationStates();
|
|
|
+ // 设置点击事件
|
|
|
+ textView.setOnClickListener(v -> selectRegionTab(index));
|
|
|
|
|
|
- // 设置选中状态
|
|
|
- switch (selectedIndex) {
|
|
|
- case 0: // 首页
|
|
|
- setNavigationSelected(mNavHome);
|
|
|
- break;
|
|
|
- case 1: // 全部商品
|
|
|
- setNavigationSelected(mNavAllGoods);
|
|
|
- break;
|
|
|
- case 2: // 评价
|
|
|
- setNavigationSelected(mNavReview);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 重置所有导航项状态
|
|
|
- */
|
|
|
- private void resetNavigationStates() {
|
|
|
- setNavigationUnselected(mNavHome);
|
|
|
- setNavigationUnselected(mNavAllGoods);
|
|
|
- setNavigationUnselected(mNavReview);
|
|
|
+ // 设置布局参数
|
|
|
+ LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
|
|
|
+ LinearLayout.LayoutParams.WRAP_CONTENT,
|
|
|
+ LinearLayout.LayoutParams.WRAP_CONTENT
|
|
|
+ );
|
|
|
+ params.setMargins(0, 0, 16, 0);
|
|
|
+ textView.setLayoutParams(params);
|
|
|
+
|
|
|
+ return new RegionTabItem(textView, regionName, index);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置导航项为选中状态
|
|
|
+ * 选中地区标签
|
|
|
*/
|
|
|
- private void setNavigationSelected(TextView textView) {
|
|
|
- if (textView != null) {
|
|
|
- textView.setTextSize(18); // 18sp
|
|
|
- textView.setTypeface(null, Typeface.BOLD);
|
|
|
+ private void selectRegionTab(int index) {
|
|
|
+ if (index < 0 || index >= mRegionTabItems.size()) {
|
|
|
+ return;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置导航项为未选中状态
|
|
|
- */
|
|
|
- private void setNavigationUnselected(TextView textView) {
|
|
|
- if (textView != null) {
|
|
|
- textView.setTextSize(14); // 14sp
|
|
|
- textView.setTypeface(null, Typeface.NORMAL);
|
|
|
+
|
|
|
+ // 重置所有标签状态
|
|
|
+ for (RegionTabItem item : mRegionTabItems) {
|
|
|
+ item.textView.setTextColor(0xFF666666);
|
|
|
+ item.textView.setBackgroundResource(android.R.color.transparent);
|
|
|
+ item.textView.setTypeface(null, Typeface.NORMAL);
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * ViewPager适配器
|
|
|
- */
|
|
|
- private static class ShopContentAdapter extends FragmentStateAdapter {
|
|
|
+
|
|
|
+ // 设置选中状态
|
|
|
+ RegionTabItem selectedItem = mRegionTabItems.get(index);
|
|
|
+ selectedItem.textView.setTextColor(0xFF333333);
|
|
|
+ selectedItem.textView.setTypeface(null, Typeface.BOLD);
|
|
|
|
|
|
- public ShopContentAdapter(FragmentActivity fragmentActivity) {
|
|
|
- super(fragmentActivity);
|
|
|
+ mCurrentSelectedRegion = index;
|
|
|
+ mCurrentRegionName = selectedItem.regionName;
|
|
|
+ updateLocationText(mCurrentRegionName);
|
|
|
+
|
|
|
+ // 取消之前的防抖任务
|
|
|
+ if (mDebounceRunnable != null) {
|
|
|
+ mDebounceHandler.removeCallbacks(mDebounceRunnable);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public Fragment createFragment(int position) {
|
|
|
- // 根据位置创建对应的Fragment
|
|
|
- switch (position) {
|
|
|
- case 0:
|
|
|
- return new ShopHomeFragment(); // 首页Fragment
|
|
|
- case 1:
|
|
|
- return new ShopAllGoodsFragment(); // 全部商品Fragment
|
|
|
- case 2:
|
|
|
- return new ShopReviewFragment(); // 评价Fragment
|
|
|
- default:
|
|
|
- return new ShopHomeFragment();
|
|
|
+ // 创建新的防抖任务
|
|
|
+ mDebounceRunnable = new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ // 生成新的请求ID
|
|
|
+ synchronized (mRequestLock) {
|
|
|
+ mCurrentRequestId++;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 刷新商家数据
|
|
|
+ if (mRefreshView != null) {
|
|
|
+ mRefreshView.initData();
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
- @Override
|
|
|
- public int getItemCount() {
|
|
|
- return 3; // 三个页面
|
|
|
- }
|
|
|
+ // 延迟执行
|
|
|
+ mDebounceHandler.postDelayed(mDebounceRunnable, DEBOUNCE_DELAY);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 首页Fragment(示例)
|
|
|
+ * 设置点击事件监听器
|
|
|
*/
|
|
|
- public static class ShopHomeFragment extends Fragment {
|
|
|
- @Override
|
|
|
- public View onCreateView(android.view.LayoutInflater inflater, ViewGroup container, android.os.Bundle savedInstanceState) {
|
|
|
- return inflater.inflate(R.layout.fragment_shop_home, container, false);
|
|
|
+ private void setClickListeners() {
|
|
|
+ if (mLocationLayout != null) {
|
|
|
+ mLocationLayout.setOnClickListener(this);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 全部商品Fragment(示例)
|
|
|
- */
|
|
|
- public static class ShopAllGoodsFragment extends Fragment {
|
|
|
- @Override
|
|
|
- public View onCreateView(android.view.LayoutInflater inflater, ViewGroup container, android.os.Bundle savedInstanceState) {
|
|
|
- TextView textView = new TextView(getContext());
|
|
|
- textView.setText("暂无商品");
|
|
|
- textView.setGravity(Gravity.CENTER);
|
|
|
- textView.setPadding(50, 50, 50, 50);
|
|
|
- return textView;
|
|
|
+ @Override
|
|
|
+ public void onItemClick(ShopItem shopItem, int position) {
|
|
|
+ if (shopItem != null && shopItem.getUid() != null) {
|
|
|
+ // 跳转到商家详情页面
|
|
|
+ ShopInfoActivity.forward(mContext, shopItem.getUid());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 评价Fragment(示例)
|
|
|
- */
|
|
|
- public static class ShopReviewFragment extends Fragment {
|
|
|
- @Override
|
|
|
- public View onCreateView(android.view.LayoutInflater inflater, ViewGroup container, android.os.Bundle savedInstanceState) {
|
|
|
- TextView textView = new TextView(getContext());
|
|
|
- textView.setText("暂无评价");
|
|
|
- textView.setGravity(Gravity.CENTER);
|
|
|
- textView.setPadding(50, 50, 50, 50);
|
|
|
- return textView;
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ int id = v.getId();
|
|
|
+ if (id == R.id.ll_location) {
|
|
|
+ // 处理地区选择点击事件 - 重新定位
|
|
|
+ startLocationIfNeeded();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void loadData() {
|
|
|
+ if (mRefreshView != null) {
|
|
|
+ mRefreshView.showLoading(); // 显示loading状态
|
|
|
+ mRefreshView.initData();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void release() {
|
|
|
super.release();
|
|
|
- // 释放资源
|
|
|
- mLocationLayout = null;
|
|
|
- mLocationText = null;
|
|
|
- mRegionContainer = null;
|
|
|
-
|
|
|
- // 释放导航栏和ViewPager资源
|
|
|
- mNavHome = null;
|
|
|
- mNavAllGoods = null;
|
|
|
- mNavReview = null;
|
|
|
-
|
|
|
- if (mViewPager != null) {
|
|
|
- mViewPager.setAdapter(null);
|
|
|
- mViewPager = null;
|
|
|
+ // 取消EventBus注册
|
|
|
+ if (EventBus.getDefault().isRegistered(this)) {
|
|
|
+ EventBus.getDefault().unregister(this);
|
|
|
}
|
|
|
-
|
|
|
- mContentAdapter = null;
|
|
|
-
|
|
|
- if (mRegionTabItems != null) {
|
|
|
- mRegionTabItems.clear();
|
|
|
- mRegionTabItems = null;
|
|
|
+ if (mShopAdapter != null) {
|
|
|
+ mShopAdapter = null;
|
|
|
}
|
|
|
-
|
|
|
- if (mRegionList != null) {
|
|
|
- mRegionList.clear();
|
|
|
- mRegionList = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 地区标签项数据类
|
|
|
+ */
|
|
|
+ private static class RegionTabItem {
|
|
|
+ TextView textView;
|
|
|
+ String regionName;
|
|
|
+ int index;
|
|
|
+
|
|
|
+ RegionTabItem(TextView textView, String regionName, int index) {
|
|
|
+ this.textView = textView;
|
|
|
+ this.regionName = regionName;
|
|
|
+ this.index = index;
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}
|