Ver Fonte

搜索界面与逻辑

詹子聪 há 5 anos atrás
pai
commit
6214698923

+ 8 - 0
app/src/main/java/com/itant/shibei/base/ApiService.java

@@ -55,6 +55,14 @@ public interface ApiService {
     @GET("/shibei/api/getGoodsList")
     Observable<BaseResponse<List<GoodsBean>>> getGoodsList(@Query("pageNum") int pageNum, @Query("pageSize") int pageSize);
 
+    /**根据关键字分页查询京东商品*/
+    @GET("/shibei/api/getGoodsListByKeyword")
+    Observable<BaseResponse<List<GoodsBean>>> getGoodsListByKeyword(@Query("keyword") String keyword, @Query("pageNum") int pageNum, @Query("pageSize") int pageSize);
+
+    /**根据链接查询京东商品*/
+    //@GET("/shibei/api/getGoodsByUrl")
+    //Observable<BaseResponse<GoodsBean>> getGoodsByUrl(@Query("url") String url, @Query("pageNum") int pageNum, @Query("pageSize") int pageSize);
+
     /*----------------------------------------优惠券开始----------------------------------------*/
     /**新增优惠券*/
     @POST("/shibei/api/addCoupon")

+ 16 - 0
app/src/main/java/com/itant/shibei/ui/home/search/ISearchView.java

@@ -0,0 +1,16 @@
+package com.itant.shibei.ui.home.search;
+
+import com.itant.shibei.ui.home.goods.GoodsBean;
+import com.miekir.mvp.view.IView;
+
+import java.util.List;
+
+/**
+ *
+ *
+ * @author Miekir
+ * @date 2020/7/8 19:44
+ */
+public interface ISearchView extends IView {
+    void onSearchKeywordResult(boolean success, String message, List<GoodsBean> goodsList);
+}

+ 125 - 7
app/src/main/java/com/itant/shibei/ui/home/search/SearchActivity.java

@@ -1,12 +1,26 @@
 package com.itant.shibei.ui.home.search;
 
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.view.View;
+import android.widget.Button;
 import android.widget.EditText;
 
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
+
 import com.itant.shibei.R;
 import com.itant.shibei.base.BaseBeiActivity;
+import com.itant.shibei.ui.home.goods.GoodsAdapter;
+import com.itant.shibei.ui.home.goods.GoodsBean;
+import com.itant.shibei.widget.DividerItemDecoration;
+import com.itant.shibei.widget.bottomlistener.OnRcvScrollListener;
+import com.miekir.common.utils.ToastTool;
 import com.miekir.common.utils.ViewTool;
+import com.miekir.mvp.presenter.InjectPresenter;
+
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Copyright (C), 2019-2020, Miekir
@@ -15,29 +29,133 @@ import com.miekir.common.utils.ViewTool;
  * @date 2020/8/12 19:40
  * Description: 输入文字搜索
  */
-public class SearchActivity extends BaseBeiActivity implements View.OnClickListener {
+public class SearchActivity extends BaseBeiActivity implements View.OnClickListener, ISearchView, View.OnFocusChangeListener {
+    private static final int PAGE_START = 0;
+    private static final int PAGE_SIZE = 20;
+    private int mCurrentPage = PAGE_START;
+
     private EditText et_search;
+    private Button btn_search;
+
+    private RecyclerView rv_search_result;
+    private List<GoodsBean> mGoodsList = new ArrayList<>();
+    private GoodsAdapter mAdapter;
+    private boolean mIsRefresh = true;
+
+    private String mKeyword;
+
+    @InjectPresenter
+    SearchPresenter searchPresenter;
+
+    @Override
+    public int getLayoutID() {
+        return R.layout.activity_search;
+    }
+
+    @Override
+    public void initViews(Bundle savedInstanceState) {
+        setTitle("搜索");
+        et_search = findViewById(R.id.et_search);
+        et_search.setOnFocusChangeListener(this);
+        btn_search = findViewById(R.id.btn_search);
+
+        rv_search_result = findViewById(R.id.rv_search_result);
+        // 必须要设置LayoutManager,否则RecyclerView不知道要使用什么布局,从而在界面上不显示
+        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
+        rv_search_result.setLayoutManager(layoutManager);
+        int dividerWidth = (int) getResources().getDimension(R.dimen.margin_s);
+        DividerItemDecoration decoration = new DividerItemDecoration(dividerWidth);
+        rv_search_result.addItemDecoration(decoration);
+        mAdapter = new GoodsAdapter(this, mGoodsList);
+        mAdapter.setEmptyView(R.layout.view_empty, rv_search_result);
+        // 加载更多
+        rv_search_result.addOnScrollListener(new OnRcvScrollListener(){
+            @Override
+            public void onBottom() {
+                super.onBottom();
+                if (mKeyword.startsWith("http")) {
+                    // 搜索链接不要加载更多功能
+                    return;
+                }
+                // 如果到底部了,而且不是正在加载状态,就变为正在加载状态,并及时去加载数据
+                showLoading();
+                mIsRefresh = false;
+                searchPresenter.searchByKeyword(mKeyword, mCurrentPage, PAGE_SIZE);
+            }
+        });
+
+        ViewTool.requestInputFocus(this, et_search);
+        ViewTool.setOnClickListener(this, this, new int[]{R.id.ll_search, R.id.btn_search, R.id.fl_clear});
+    }
 
     @Override
     public void onClick(View v) {
+
         switch (v.getId()) {
+            case R.id.btn_search:
+                et_search.clearFocus();
+                mKeyword = et_search.getText().toString();
+                if (TextUtils.isEmpty(mKeyword)) {
+                    ToastTool.showShort("请输入关键字或链接");
+                    return;
+                }
+                showLoading();
+                mCurrentPage = PAGE_START;
+                mIsRefresh = true;
+                searchPresenter.searchByKeyword(mKeyword, mCurrentPage, PAGE_SIZE);
+                break;
             case R.id.ll_search:
                 et_search.clearFocus();
                 break;
+            case R.id.fl_clear:
+                et_search.setText("");
+                ViewTool.requestInputFocus(this, et_search);
+                break;
             default:
                 break;
         }
     }
 
     @Override
-    public int getLayoutID() {
-        return R.layout.activity_search;
+    public void onSearchKeywordResult(boolean success, String message, List<GoodsBean> goodsList) {
+        dismissLoading();
+
+        if (!success) {
+            ToastTool.showShort(message);
+            return;
+        }
+
+        if (mCurrentPage == PAGE_START) {
+            mGoodsList.clear();
+            mGoodsList.addAll(goodsList);
+            // 本地写死的数据
+            mAdapter.notifyDataSetChanged();
+            // 如果是下拉刷新获取的第0页数据,item不要自动滚动一段距离
+            // 或者用这个mLayoutManager.scrollToPositionWithOffset(0, 0);
+            if (mIsRefresh) {
+                rv_search_result.smoothScrollToPosition(0);
+            }
+        } else {
+            mGoodsList.addAll(goodsList);
+            mAdapter.notifyDataSetChanged();
+        }
+
+        // 请求到数据了,页数自增
+        if (goodsList != null && goodsList.size() > 0) {
+            mCurrentPage++;
+        }
     }
 
     @Override
-    public void initViews(Bundle savedInstanceState) {
-        setTitle("搜索");
-        et_search = findViewById(R.id.et_search);
-        ViewTool.setOnClickListener(this, this, new int[]{R.id.ll_search});
+    public void onFocusChange(View v, boolean hasFocus) {
+        if (v.getId() == R.id.et_search) {
+            if (hasFocus) {
+                rv_search_result.setVisibility(View.GONE);
+                btn_search.setVisibility(View.VISIBLE);
+            } else {
+                rv_search_result.setVisibility(View.VISIBLE);
+                btn_search.setVisibility(View.GONE);
+            }
+        }
     }
 }

+ 47 - 0
app/src/main/java/com/itant/shibei/ui/home/search/SearchPresenter.java

@@ -0,0 +1,47 @@
+package com.itant.shibei.ui.home.search;
+
+import android.text.TextUtils;
+
+import com.itant.shibei.base.ApiService;
+import com.itant.shibei.net.RetrofitHelper;
+import com.itant.shibei.ui.home.goods.GoodsBean;
+import com.miekir.mvp.presenter.BasePresenter;
+import com.miekir.network.core.base.BaseObserver;
+
+import java.util.List;
+
+import io.reactivex.android.schedulers.AndroidSchedulers;
+import io.reactivex.schedulers.Schedulers;
+
+/**
+ * Copyright (C), 2019-2020, Miekir
+ *
+ * @author Miekir
+ * @date 2020/8/16 9:34
+ * Description: 搜索
+ */
+public class SearchPresenter extends BasePresenter<ISearchView> {
+
+    public void searchByKeyword(String keyword, int pageNum, int pageSize) {
+        RetrofitHelper.getInstance()
+                .getRequestApi(ApiService.class)
+                .getGoodsListByKeyword(keyword, pageNum, pageSize)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe(new BaseObserver<List<GoodsBean>>() {
+                    @Override
+                    public void onSuccess(List<GoodsBean> result) {
+                        if (getView() != null) {
+                            getView().onSearchKeywordResult(true, "", result);
+                        }
+                    }
+
+                    @Override
+                    public void onFailure(Throwable e, String errMsg) {
+                        if (getView() != null) {
+                            getView().onSearchKeywordResult(false, TextUtils.isEmpty(errMsg) ? "获取失败:"+e.getMessage() : errMsg, null);
+                        }
+                    }
+                });
+    }
+}

+ 60 - 29
app/src/main/res/layout/activity_search.xml

@@ -12,38 +12,69 @@
 
     <include layout="@layout/view_toolbar"/>
 
-
-    <EditText
-        android:id="@+id/et_search"
+    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:minHeight="@dimen/height_tab_bar"
-        android:background="@color/white"
-        android:singleLine="true"
-        android:paddingStart="@dimen/margin_default"
-        android:paddingEnd="@dimen/margin_default"
-        android:paddingTop="@dimen/margin_ss"
-        android:paddingBottom="@dimen/margin_ss"
-        android:textSize="@dimen/text_normal_p"
-        android:hint="请输入关键字或链接"
-        android:textColor="@color/black_text_comfortable"
-        android:textColorHint="@color/gray_text_hint"
-        android:maxLength="1024"/>
+        android:orientation="horizontal"
+        android:gravity="center_vertical"
+        android:background="@color/white">
+        <EditText
+            android:id="@+id/et_search"
+            android:layout_width="0dp"
+            android:layout_weight="1"
+            android:layout_height="wrap_content"
+            android:minHeight="@dimen/height_tab_bar"
+            android:background="@color/white"
+            android:singleLine="true"
+            android:paddingStart="@dimen/margin_default"
+            android:paddingEnd="@dimen/margin_default"
+            android:paddingTop="@dimen/margin_ss"
+            android:paddingBottom="@dimen/margin_ss"
+            android:textSize="@dimen/text_normal_p"
+            android:hint="请输入关键字或链接"
+            android:textColor="@color/black_text_comfortable"
+            android:textColorHint="@color/gray_text_hint"
+            android:maxLength="512" />
+        <FrameLayout
+            android:id="@+id/fl_clear"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:paddingStart="@dimen/margin_default"
+            android:paddingEnd="@dimen/margin_default">
+            <ImageView
+                android:layout_width="16dp"
+                android:layout_height="16dp"
+                android:scaleType="fitXY"
+                android:src="@mipmap/search_clear" />
+        </FrameLayout>
+    </LinearLayout>
+
     <include layout="@layout/view_divider_common"/>
 
-    <android.widget.Button
-        android:id="@+id/btn_search"
+    <FrameLayout
         android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:minHeight="0dp"
-        android:layout_marginTop="@dimen/margin_default"
-        android:layout_marginStart="@dimen/margin_default"
-        android:layout_marginEnd="@dimen/margin_default"
-        android:paddingTop="@dimen/padding_full_width"
-        android:paddingBottom="@dimen/padding_full_width"
-        android:text="立即搜索"
-        android:textColor="@color/white"
-        android:textStyle="bold"
-        android:background="@drawable/selector_btn"
-        style="?android:attr/borderlessButtonStyle"/>
+        android:layout_height="match_parent">
+        <android.widget.Button
+            android:id="@+id/btn_search"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:minHeight="0dp"
+            android:layout_marginTop="@dimen/margin_default"
+            android:layout_marginStart="@dimen/margin_default"
+            android:layout_marginEnd="@dimen/margin_default"
+            android:paddingTop="@dimen/padding_full_width"
+            android:paddingBottom="@dimen/padding_full_width"
+            android:text="立即搜索"
+            android:textColor="@color/white"
+            android:textStyle="bold"
+            android:background="@drawable/selector_btn"
+            style="?android:attr/borderlessButtonStyle"/>
+
+        <androidx.recyclerview.widget.RecyclerView
+            android:id="@+id/rv_search_result"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:visibility="gone"/>
+    </FrameLayout>
+
 </LinearLayout>

BIN
app/src/main/res/mipmap-xxhdpi/search_clear.png


+ 24 - 0
common/src/main/java/com/miekir/common/utils/ViewTool.java

@@ -1,7 +1,11 @@
 package com.miekir.common.utils;
 
 import android.app.Activity;
+import android.os.SystemClock;
+import android.view.MotionEvent;
 import android.view.View;
+import android.view.ViewTreeObserver;
+import android.view.WindowManager;
 
 /**
  *
@@ -28,4 +32,24 @@ public class ViewTool {
             activity.findViewById(viewId).setOnClickListener(listener);
         }
     }
+
+    /**
+     * 获取焦点
+     * @param activity
+     * @param view
+     */
+    public static void requestInputFocus(Activity activity, final View view) {
+        // 进入页面弹出软键盘
+        view.requestFocus();
+        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+        // 或使用以下方法(view.post不行)
+        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
+            @Override
+            public void onGlobalLayout() {
+                view.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
+                view.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
+                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
+            }
+        });
+    }
 }