From 12a19ad554e6f7c5c355856c12914bca7bce70e8 Mon Sep 17 00:00:00 2001
From: admin <2780501319@qq.com>
Date: 星期二, 30 三月 2021 01:39:47 +0800
Subject: [PATCH] 搜索与搜索结果页,白色背景兼容

---
 BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchActivity.java |   67 +++++++++++++++++++++++++--------
 1 files changed, 51 insertions(+), 16 deletions(-)

diff --git a/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchActivity.java b/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchActivity.java
index dc0cfe2..b5660b4 100644
--- a/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchActivity.java
+++ b/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchActivity.java
@@ -74,6 +74,8 @@
 
     private SearchSuggestFragment suggestFragment;
 
+    private FrameLayout fl_suggest;
+
     private String value = "";
 
     private boolean touchSearchInput = false;
@@ -87,6 +89,7 @@
         gv_history_search = findViewById(R.id.gv_history_search);
         tv_clear = findViewById(R.id.tv_search_clear_his);
         ll_search_history = findViewById(R.id.ll_search_history);
+        fl_suggest = findViewById(R.id.fl_suggest);
     }
 
     @Override
@@ -143,19 +146,14 @@
 
             @Override
             public void afterTextChanged(Editable s) {
-                if (!TextUtils.isEmpty(s.toString())
-                        || !TextUtils.isEmpty(et_search_key.getHint())) {
-
-
-                    suggestSearch(s.toString());
+                if (s != null && !TextUtils.isEmpty(s.toString())) {
                     et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                             R.drawable.ic_clear, 0);
-//					tv_search_cancel.setText(R.string.search);
                 } else {
                     et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                             0, 0);
-//					tv_search_cancel.setText(R.string.cancel);
                 }
+                suggestSearch(s != null ? s.toString() : null);
             }
         });
 
@@ -176,13 +174,6 @@
                 // getCompoundDrawables() 鍙互鑾峰彇涓�涓暱搴︿负4鐨勬暟缁勶紝
                 // 瀛樻斁drawableLeft锛孯ight锛孴op锛孊ottom鍥涗釜鍥剧墖璧勬簮瀵硅薄
                 // index=2 琛ㄧず鐨勬槸 drawableRight 鍥剧墖璧勬簮瀵硅薄
-                if (et_search_key.isFocused()) {
-                    findViewById(R.id.fl_suggest).setVisibility(View.VISIBLE);
-                    getSupportFragmentManager().beginTransaction().replace(R.id.fl_suggest, suggestFragment).commitAllowingStateLoss();
-                } else {
-                    findViewById(R.id.fl_suggest).setVisibility(View.GONE);
-                    getSupportFragmentManager().beginTransaction().remove(suggestFragment).commitAllowingStateLoss();
-                }
 
                 Drawable drawable = et_search_key.getCompoundDrawables()[2];
                 if (drawable == null)
@@ -262,7 +253,41 @@
         }
     }
 
+
+    private void hiddenSuggestFragment() {
+        fl_suggest.setVisibility(View.GONE);
+        getSupportFragmentManager().beginTransaction().remove(suggestFragment).commitAllowingStateLoss();
+    }
+
+    private void showSuggestFragment(String key, List<String> list) {
+        Bundle bundle = new Bundle();
+        bundle.putString("key", key);
+        bundle.putString("list", new Gson().toJson(list));
+        fl_suggest.setVisibility(View.VISIBLE);
+//        if (suggestFragment.isAdded()) {
+//            return;
+//        }
+        suggestFragment.setItemClickListener(new SearchSuggestFragment.OnItemClickListener() {
+            @Override
+            public void onClick(String st) {
+                Intent intent = new Intent(SearchActivity.this,
+                        SearchResultActivity.class);
+                intent.putExtra("key", st);
+                startActivity(intent);
+                hiddenSuggestFragment();
+            }
+        });
+
+        suggestFragment.setArguments(bundle);
+        getSupportFragmentManager().beginTransaction().replace(R.id.fl_suggest, suggestFragment).commitAllowingStateLoss();
+    }
+
     private void suggestSearch(String key) {
+        if (StringUtils.isEmpty(key)) {
+            hiddenSuggestFragment();
+        }
+
+
         SharedPreferences preferences = getSharedPreferences("user",
                 Context.MODE_PRIVATE);
         String uid = preferences.getString("uid", "");
@@ -273,19 +298,29 @@
                     public void onSuccessPerfect(int statusCode,
                                                  Header[] headers, JSONObject jsonObject)
                             throws Exception {
+                        List<String> results = null;
                         if (jsonObject.getBoolean("IsPost")) {
                             Gson gson = new GsonBuilder().setFieldNamingPolicy(
                                     FieldNamingPolicy.UPPER_CAMEL_CASE)
                                     .create();
-                            List<String> results = gson.fromJson(jsonObject
+                            results = gson.fromJson(jsonObject
                                     .getJSONObject("Data").getJSONArray("data")
                                     .toString(), new TypeToken<List<String>>() {
                             }.getType());
-                            suggestFragment.setData(key, results);
+
+
 //                            SuggestKeysAdapter adapter = new SuggestKeysAdapter(getApplicationContext(), results);
 //                            et_search_key.setAdapter(adapter);
 //                            adapter.notifyDataSetChanged();
                         }
+
+                        if (results != null && results.size() > 0) {
+                            showSuggestFragment(key, results);
+                            suggestFragment.setData(key, results);
+                        } else {
+                            hiddenSuggestFragment();
+                        }
+
                     }
                 });
     }

--
Gitblit v1.8.0