ソースを参照

sample: #198 自动搜索节流改为分页列表

drake 1 年間 前
コミット
a78d461186

+ 29 - 4
sample/src/main/java/com/drake/net/sample/ui/fragment/EditDebounceFragment.kt

@@ -1,13 +1,16 @@
 package com.drake.net.sample.ui.fragment
 
+import com.drake.brv.utils.models
+import com.drake.brv.utils.setup
 import com.drake.engine.base.EngineFragment
 import com.drake.net.Get
 import com.drake.net.sample.R
 import com.drake.net.sample.constants.Api
 import com.drake.net.sample.databinding.FragmentEditDebounceBinding
+import com.drake.net.sample.model.GameModel
 import com.drake.net.utils.debounce
 import com.drake.net.utils.launchIn
-import com.drake.net.utils.scopeNetLife
+import com.drake.net.utils.scope
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.cancel
 import kotlinx.coroutines.flow.distinctUntilChanged
@@ -18,15 +21,37 @@ class EditDebounceFragment :
     override fun initData() {
     }
 
+
     override fun initView() {
+        var searchText = ""
         var scope: CoroutineScope? = null
 
+        // 配置列表
+        binding.rv.setup {
+            addType<GameModel.Data>(R.layout.item_game)
+        }
+
+        // 监听分页
+        binding.page.onRefresh {
+            scope = scope {
+                val data = Get<GameModel>(Api.GAME) {
+                    param("search", searchText)
+                    param("page", index)
+                }.await()
+                addData(data.list) {
+                    itemCount < data.total
+                }
+            }
+        }
+
         // distinctUntilChanged 表示过滤掉重复结果
         binding.etInput.debounce().distinctUntilChanged().launchIn(this) {
             scope?.cancel() // 发起新的请求前取消旧的请求, 避免旧数据覆盖新数据
-            scope = scopeNetLife { // 保存旧的请求到一个变量中
-                binding.tvFragment.text = "请求中"
-                binding.tvFragment.text = Get<String>(Api.TIME).await()
+            searchText = it
+            if (it.isBlank()) {
+                binding.rv.models = null
+            } else {
+                binding.page.showLoading()
             }
         }
     }

+ 1 - 1
sample/src/main/java/com/drake/net/sample/ui/fragment/PullRefreshFragment.kt

@@ -23,7 +23,7 @@ class PullRefreshFragment :
             scope {
                 val response = Get<GameModel>(String.format(Api.GAME, index)).await()
                 addData(response.list) {
-                    index < response.total
+                    itemCount < response.total
                 }
             }
         }.autoRefresh()

+ 1 - 1
sample/src/main/java/com/drake/net/sample/ui/fragment/PushRefreshFragment.kt

@@ -17,7 +17,7 @@ class PushRefreshFragment :
 
     override fun initView() {
         binding.rv.linear().setup {
-            addType<GameModel.Data>(R.layout.item_list)
+            addType<GameModel.Data>(R.layout.item_game)
         }
 
         binding.page.onRefresh {

+ 13 - 5
sample/src/main/res/layout/fragment_edit_debounce.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <layout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools">
 
 
@@ -15,15 +16,22 @@
             android:layout_height="50dp"
             android:layout_marginHorizontal="32dp"
             android:layout_marginTop="20dp"
-            android:hint="输入内容将自动查询当前时间" />
+            android:hint="输入内容自动搜索" />
 
 
-        <TextView
-            android:id="@+id/tvFragment"
+        <com.drake.brv.PageRefreshLayout
+            android:id="@+id/page"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
-            android:layout_margin="32dp"
-            android:textStyle="bold" />
+            app:srlEnableRefresh="false">
+
+            <androidx.recyclerview.widget.RecyclerView
+                android:id="@+id/rv"
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
+
+        </com.drake.brv.PageRefreshLayout>
 
     </LinearLayout>
 </layout>

+ 0 - 0
sample/src/main/res/layout/item_list.xml → sample/src/main/res/layout/item_game.xml


+ 1 - 1
sample/src/main/res/raw/game.json

@@ -1,5 +1,5 @@
 {
-  "total": 3,
+  "total": 60,
   "list": [
     {
       "id": 1,