From 12a19ad554e6f7c5c355856c12914bca7bce70e8 Mon Sep 17 00:00:00 2001 From: admin <2780501319@qq.com> Date: 星期二, 30 三月 2021 01:39:47 +0800 Subject: [PATCH] 搜索与搜索结果页,白色背景兼容 --- BuWanVideo/res/layout/search_result_activity.xml | 128 ++++++++++------- BuWanVideo/res/layout/search_actvity.xml | 3 BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchSuggestFragment.java | 37 ++++- BuWanVideo/res/layout/search_result_top_bar.xml | 2 BuWanVideo/res/layout/suggestion_grid_item.xml | 2 BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchActivity.java | 67 +++++++-- BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java | 119 +++++++++++----- BuWanVideo/res/layout/item_search_result_top_bar_content.xml | 4 BuWanVideo/res/drawable/shape_search_keyword_bg.xml | 2 9 files changed, 237 insertions(+), 127 deletions(-) diff --git a/BuWanVideo/res/drawable/shape_search_keyword_bg.xml b/BuWanVideo/res/drawable/shape_search_keyword_bg.xml index 401e2a3..ee449d6 100644 --- a/BuWanVideo/res/drawable/shape_search_keyword_bg.xml +++ b/BuWanVideo/res/drawable/shape_search_keyword_bg.xml @@ -2,5 +2,5 @@ <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="40dp" /> - <solid android:color="#3D3D3D" /> + <solid android:color="@color/search_item_bg_color" /> </shape> \ No newline at end of file diff --git a/BuWanVideo/res/layout/item_search_result_top_bar_content.xml b/BuWanVideo/res/layout/item_search_result_top_bar_content.xml index 74d66ef..43bc37e 100644 --- a/BuWanVideo/res/layout/item_search_result_top_bar_content.xml +++ b/BuWanVideo/res/layout/item_search_result_top_bar_content.xml @@ -8,11 +8,11 @@ android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:background="@drawable/vpi__tab_selected_focused_holo" + android:background="@null" android:text="鐢佃鍓�" android:paddingBottom="15dp" android:gravity="center" - android:textColor="@color/white" + android:textColor="@color/nav_text_color" android:textSize="17sp"></TextView> diff --git a/BuWanVideo/res/layout/search_actvity.xml b/BuWanVideo/res/layout/search_actvity.xml index 87fe3fd..718ac9c 100644 --- a/BuWanVideo/res/layout/search_actvity.xml +++ b/BuWanVideo/res/layout/search_actvity.xml @@ -77,8 +77,7 @@ <ScrollView android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_weight="1"> + android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" diff --git a/BuWanVideo/res/layout/search_result_activity.xml b/BuWanVideo/res/layout/search_result_activity.xml index 64ba009..d455413 100644 --- a/BuWanVideo/res/layout/search_result_activity.xml +++ b/BuWanVideo/res/layout/search_result_activity.xml @@ -2,7 +2,7 @@ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" - android:background="@color/theme" + android:background="@color/page_content_bg_color" android:orientation="vertical"> <include layout="@layout/view_status_bar" /> @@ -33,22 +33,22 @@ android:scaleType="fitCenter" android:src="@drawable/ic_activity_main_search"></ImageView> - <AutoCompleteTextView + <EditText android:id="@+id/et_search_key" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@null" - android:completionThreshold="1" android:hint="璇疯緭鍏ユ悳绱㈠唴瀹�" android:imeOptions="actionSearch" android:inputType="text" android:padding="5dp" android:textAppearance="?android:attr/textAppearance" - android:textColor="@color/white" + android:textColor="@color/search_input_text_color" + android:textColorHint="@color/search_input_hint_text_color" android:textCursorDrawable="@null" - android:textSize="15sp" /> + android:textSize="14sp" /> </LinearLayout> @@ -60,62 +60,80 @@ android:paddingLeft="17dp" android:paddingRight="18dp" android:text="鍙栨秷" - android:textColor="#ffffffff" - android:textSize="12sp"></TextView> - - </LinearLayout> - - - <include layout="@layout/search_result_top_bar" /> - - <android.support.v4.widget.SwipeRefreshLayout - android:id="@+id/rl_search_result" - android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_weight="1"> - - <android.support.v7.widget.RecyclerView - android:id="@+id/rv_video_search" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:paddingLeft="10dp" - android:paddingRight="10dp"></android.support.v7.widget.RecyclerView> - </android.support.v4.widget.SwipeRefreshLayout> - - <LinearLayout - android:id="@+id/ll_empty" - android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_weight="1" - android:gravity="center" - android:orientation="vertical" - android:visibility="gone"> - - <ImageView - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:src="@drawable/ic_empty"></ImageView> - - <TextView - android:layout_marginTop="12dp" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:text="鎶辨瓑锛屾殏鏈悳绱㈠埌鐩稿叧鍐呭..." - android:textColor="@color/white"></TextView> - + android:textColor="@color/item_title_text_color" + android:textSize="14sp"></TextView> </LinearLayout> <FrameLayout - android:id="@+id/fl_advertisement" android:layout_width="match_parent" - android:layout_height="wrap_content" - android:visibility="gone"> + android:layout_height="0dp" + android:layout_weight="1"> - <ImageView - android:id="@+id/iv_search_result_ad" + <LinearLayout android:layout_width="match_parent" - android:layout_height="wrap_content" /> - </FrameLayout> + android:layout_height="match_parent" + android:orientation="vertical"> + <include layout="@layout/search_result_top_bar" /> + + <android.support.v4.widget.SwipeRefreshLayout + android:id="@+id/rl_search_result" + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="1"> + + <android.support.v7.widget.RecyclerView + android:id="@+id/rv_video_search" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:paddingLeft="10dp" + android:paddingRight="10dp"></android.support.v7.widget.RecyclerView> + </android.support.v4.widget.SwipeRefreshLayout> + + <LinearLayout + android:id="@+id/ll_empty" + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="1" + android:gravity="center" + android:orientation="vertical" + android:visibility="gone"> + + <ImageView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:src="@drawable/ic_empty"></ImageView> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="12dp" + android:text="鎶辨瓑锛屾殏鏈悳绱㈠埌鐩稿叧鍐呭..." + android:textColor="@color/white"></TextView> + + + </LinearLayout> + + <FrameLayout + android:id="@+id/fl_advertisement" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:visibility="gone"> + + <ImageView + android:id="@+id/iv_search_result_ad" + android:layout_width="match_parent" + android:layout_height="wrap_content" /> + </FrameLayout> + </LinearLayout> + + <FrameLayout + android:id="@+id/fl_suggest" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:background="@color/page_content_bg_color" + android:visibility="gone"></FrameLayout> + + </FrameLayout> </LinearLayout> \ No newline at end of file diff --git a/BuWanVideo/res/layout/search_result_top_bar.xml b/BuWanVideo/res/layout/search_result_top_bar.xml index 43706e3..6d654a3 100644 --- a/BuWanVideo/res/layout/search_result_top_bar.xml +++ b/BuWanVideo/res/layout/search_result_top_bar.xml @@ -2,7 +2,7 @@ <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" - android:background="@color/theme" + android:background="@color/page_content_bg_color" android:orientation="horizontal" android:paddingLeft="8dp" android:paddingRight="8dp" diff --git a/BuWanVideo/res/layout/suggestion_grid_item.xml b/BuWanVideo/res/layout/suggestion_grid_item.xml index 6616bdb..fae7ef7 100644 --- a/BuWanVideo/res/layout/suggestion_grid_item.xml +++ b/BuWanVideo/res/layout/suggestion_grid_item.xml @@ -19,7 +19,7 @@ android:singleLine="true" android:text="123123" android:textAppearance="?android:attr/textAppearanceMedium" - android:textColor="#DDDDDD" + android:textColor="@color/search_item_text_color" android:textSize="12sp" /> </LinearLayout> \ No newline at end of file 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(); + } + } }); } diff --git a/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java b/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java index 9019582..2d3d08c 100644 --- a/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java +++ b/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java @@ -26,8 +26,10 @@ import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.BaseAdapter; +import android.widget.EditText; import android.widget.Filter; import android.widget.Filterable; +import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; @@ -68,7 +70,7 @@ public class SearchResultActivity extends BaseActivity implements OnClickListener { - private AutoCompleteTextView et_search_key; + private EditText et_search_key; private TextView tv_search_cancel; @@ -101,11 +103,17 @@ private LinearLayout ll_empty; + private SearchSuggestFragment suggestFragment; + + private FrameLayout fl_suggest; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search_result_activity); initStatusBar(); + + suggestFragment = new SearchSuggestFragment(); rl_search_result = findViewById(R.id.rl_search_result); et_search_key = findViewById(R.id.et_search_key); @@ -113,6 +121,7 @@ rv_video_search = findViewById(R.id.rv_video_search); ll_top_bar = findViewById(R.id.ll_top_bar); ll_empty = findViewById(R.id.ll_empty); + fl_suggest = findViewById(R.id.fl_suggest); tv_search_cancel.setOnClickListener(this); et_search_key.addTextChangedListener(new TextWatcher() { @@ -131,33 +140,18 @@ @Override public void afterTextChanged(Editable s) { - if (!TextUtils.isEmpty(s.toString()) - || !TextUtils.isEmpty(et_search_key.getText())) { - if (mFirst) { - mFirst = false; - } else { - suggestSearch(s.toString()); - } + if (!TextUtils.isEmpty(s.toString())) { et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_clear, 0); } else { et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); } - } - }); - et_search_key.setOnItemClickListener(new OnItemClickListener() { - - @Override - public void onItemClick(AdapterView<?> parent, View view, - int position, long id) { - mFirst = true; - mCurrentKey = parent.getItemAtPosition(position).toString(); - SoftKeyboardUtils.hideSoftInput(SearchResultActivity.this); - mCurrentPage = 1; - //娓呴櫎椤堕儴鍒嗙被 - typeList = null; - search(mCurrentKey, mCurrentType); + if (mFirst) { + mFirst = false; + } else { + suggestSearch(s != null ? s.toString() : null); + } } }); @@ -190,18 +184,7 @@ @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { - if (StringUtils.isEmpty(et_search_key.getEditableText() - .toString())) { - mCurrentKey = et_search_key.getText().toString(); - } else { - mCurrentKey = et_search_key.getEditableText().toString(); - } - SoftKeyboardUtils.hideSoftInput(SearchResultActivity.this); - mCurrentPage = 1; - rl_search_result.setRefreshing(true); - //娓呴櫎椤堕儴鍒嗙被 - typeList = null; - search(mCurrentKey, mCurrentType); + startSearch(); return true; } return false; @@ -268,6 +251,21 @@ // loadAd(); } + private void startSearch() { + if (StringUtils.isEmpty(et_search_key.getEditableText() + .toString())) { + mCurrentKey = et_search_key.getText().toString(); + } else { + mCurrentKey = et_search_key.getEditableText().toString(); + } + SoftKeyboardUtils.hideSoftInput(SearchResultActivity.this); + mCurrentPage = 1; + rl_search_result.setRefreshing(true); + //娓呴櫎椤堕儴鍒嗙被 + typeList = null; + search(mCurrentKey, mCurrentType); + } + private void initTopBar(List<VideoType> videoTypeList) { int p = 0; ll_top_bar.removeAllViews(); @@ -304,12 +302,12 @@ if (selected) { tv_name.setBackgroundResource(R.drawable.vpi__tab_selected_focused_holo); tv_name.setTextSize(17); - tv_name.setTextColor(Color.parseColor("#FFFFFF")); + tv_name.setTextColor(getResources().getColor(R.color.nav_highloght_text_color)); tv_name.setPadding(0, 0, 0, DimenUtils.dip2px(getApplicationContext(), 5)); } else { tv_name.setBackground(null); tv_name.setTextSize(12); - tv_name.setTextColor(Color.parseColor("#999999")); + tv_name.setTextColor(getResources().getColor(R.color.nav_text_color)); tv_name.setPadding(0, 0, 0, 0); } } @@ -321,7 +319,37 @@ } } + + 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); + suggestFragment.setItemClickListener(new SearchSuggestFragment.OnItemClickListener() { + @Override + public void onClick(String st) { + mFirst = true; + hiddenSuggestFragment(); + et_search_key.setText(st); + startSearch(); + } + }); + + 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", ""); @@ -332,18 +360,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()); - SuggestKeysAdapter adapter = new SuggestKeysAdapter(getApplicationContext(), results); - et_search_key.setAdapter(adapter); - adapter.notifyDataSetChanged(); + + +// 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(); + } + } }); } diff --git a/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchSuggestFragment.java b/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchSuggestFragment.java index 4787d2e..0ff0fba 100644 --- a/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchSuggestFragment.java +++ b/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchSuggestFragment.java @@ -15,11 +15,14 @@ import android.widget.RelativeLayout; import android.widget.TextView; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import com.lcjian.library.RetainViewFragment; import com.weikou.beibeivideo.R; import com.weikou.beibeivideo.util.DimenUtils; import com.weikou.beibeivideo.util.ui.DividerItemDecoration; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; @@ -31,7 +34,7 @@ private RecyclerView rv_suggest; - private List<String> mList = null; + private List<String> mList = new ArrayList<>(); private String key; private SearchSuggestAdapter adapter; @@ -46,20 +49,31 @@ } private void initData() { - mList = new ArrayList<>(); adapter = new SearchSuggestAdapter(getContext(), mList); rv_suggest.setAdapter(adapter); + Bundle bundle = getArguments(); + if (bundle != null) { + key = bundle.getString("key"); + String list = bundle.getString("list"); + Type type = new TypeToken<List<String>>() { + }.getType(); + List<String> mList1 = new Gson().fromJson(list, type); + setData(key, mList1); + } } public void setData(String key, List<String> list) { this.key = key; - if (mList == null) { - initData(); - } mList.clear(); mList.addAll(list); adapter.notifyDataSetChanged(); + } + + private OnItemClickListener clickListener; + + public void setItemClickListener(OnItemClickListener clickListener) { + this.clickListener = clickListener; } @Override @@ -112,10 +126,9 @@ viewHolder.view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Intent intent = new Intent(mContext, - SearchResultActivity.class); - intent.putExtra("key", st); - startActivity(intent); + if (clickListener != null) { + clickListener.onClick(st); + } } }); @@ -142,4 +155,10 @@ } } + interface OnItemClickListener { + + public void onClick(String st); + + } + } -- Gitblit v1.8.0