一個搜尋和展示熱詞和歷史內容等篩選條件的類
阿新 • • 發佈:2019-01-16
package com.luxji.auction.app; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.PaintDrawable; importandroid.os.Bundle; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo;import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.HorizontalScrollView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow;import android.widget.RadioButton; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.google.gson.Gson; import com.luxji.auction.R; import com.luxji.auction.adapter.GoodsListAdapter; import com.luxji.auction.base.BaseActivity; import com.luxji.auction.base.BaseApplication; import com.luxji.auction.model.GoodsInfo; import com.luxji.auction.model.GoodsSearchResult; import com.luxji.auction.model.MaintenanceResult; import com.luxji.auction.util.Constant; import com.luxji.auction.util.SharedPreferencesUtil; import com.luxji.auction.widget.LoadFailView; import com.luxji.auction.widget.MyListView; import com.luxji.auction.widget.NetWorkView; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.Map; /** * Created by Clare on 2016/6/28. * Emali [email protected] */ public class GoodsSearchActivity extends BaseActivity { public static String CATID = "id"; public static String CATNAME = "cat_name"; //篩選條件選單 // private SlidingMenu mSlidingMenu; //搜尋按鈕 private TextView searchTextView; //返回按鈕 private LinearLayout backLayout; //搜尋文字框 private EditText searchEditText; private ImageView deleteImageview; //品牌 private TextView brandTextView; private ListView brandListView; private ListView serialListView; private int screenHeight=0; private int screenWidth=0; private LinkedList<String> brands; private LinkedList<String> serials; private ArrayAdapter<String> brandAdapter; private ArrayAdapter<String> serialAdapter; // private SupperLayout brandSupperLayout; // private SupperLayout serialSupperLayout; private View.OnClickListener mOnClickListener; private View.OnClickListener mSerialOnClickListener; private View.OnClickListener mFilterCityOnClickListener; private View.OnClickListener mFilterSexOnClickListener; private View.OnClickListener mFilterPriceOnClickListener; // private SupperLayout mCitySupperLayout; //// private FlowRadioGroup mSexRadioGroup; //// private FlowRadioGroup mPriceRadioGroup; // private SupperLayout mSexSupperLayout; // private SupperLayout mPriceSupperLayout; //價格 private LinearLayout priceLayout; private Boolean isUp = false; //帥選 private TextView filterTextView; //商品列表adapter private GoodsListAdapter goodsListAdapter; //商品資料list private LinkedList<GoodsInfo> goodsInfos; //商品列表控制元件listview private ListView goodsListView; private int skip = 0; //每次重新整理時把最後一行或最上面一行的商品編號傳遞過去 private String goodsId; private LayoutInflater inflater; private View footView; private LinearLayout loading; private TextView moreTextView; //儲存篩選條件 private LinkedList<TextView> selectedList; private LinkedList<TextView> selectedSexList; private LinkedList<TextView> selectedPriceList; private LinkedList<TextView> tempSelectedList = new LinkedList<TextView>(); private LinkedList<TextView> tempSelectedSexList = new LinkedList<TextView>(); private LinkedList<TextView> tempSelectedPriceList = new LinkedList<TextView>(); private Drawable checkedDrawable; private TextView sureTextview; private TextView cancelTextview; //查詢條件 private String condition = "" ; //分類id private String mCatId = ""; private String mCatName = ""; //價格排序 private String mPriceSort = "up" ; //查詢關鍵字 private String mKeywords = "" ; //男女或中性 private String mCrowd = "" ; //價格區間 private String mPrice = "" ; //地區 private String mArea = ""; //彈出視窗 private PopupWindow popupWindow; //熱詞 private HorizontalScrollView horizontalScrollView; private ArrayList<String> hotWordList; //搜尋歷史列表 private ListView hisListview; //歷史資料列表 private ArrayList<String> hisList; // private LinearLayout conditionLayout; private String his; //搜尋歷史頁 private ScrollView searchHisScrollView; //熱詞點選事件 private View.OnClickListener hotWordOnClickListener; //清除歷史按鈕 private TextView clearTextView; //歷史查詢記錄adapter private ArrayAdapter<String> mArrayAdapter; // //性別陣列 // private String[] sexArr = {"女性√","男性√","中性√"}; // //價格陣列 // private String[] priceArr = {"0-5000√","5001-10000√", // "10001-20000√","20001-30000√","30000以上√"}; // //城市 // private String[] cityArr = {"北京√","香港√","上海√","成都√","西安√"}; //popupwindow private View filterContentView; private View brandContentView; private MyListView hotWordListView; private ArrayAdapter<String> hotwordAdapter; private ImageView priceImageview; private Matrix matrix; private Bitmap bitmap; // private NetWorkView noNetworkView; // private LoadFailView loadFailView; // private NoDataView noDataView; private View noDataView;// 無資料時狀態 private NetWorkView noNetworkView;// 無網路時狀態 private View view; private TextView saveBrandTextView; //新 String externalKeyword;//外部傳過來的關鍵字 int page=1;//分頁第幾頁 int pagesize=10;//每頁個數 private LinkedList<GoodsSearchResult.DataBean> mDataInfo = new LinkedList<>();; //商品資料list private RecyclerView recyclerView; private MaintenanceAdatper adapter; public static void oepn(Context context,String value){ Intent intent=new Intent(context,GoodsSearchActivity.class); intent.putExtra("externalKeyword",value); context.startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); noNetworkView(); loadFailView(); //控制元件初始化 initWidget(); initSearchHis(); initScreenWidth(); doInitLoad(); GoodsItemClick(); intBundle(); // initFilterPopupWindow(); // initBrandPopupwindow(); } private void intBundle(){ //得到外部傳進來的搜尋關鍵字 Bundle bunlde = getIntent().getExtras(); if(bunlde != null){ externalKeyword = bunlde.getString("externalKeyword"); if(!TextUtils.isEmpty(externalKeyword)){ if (searchEditText!=null){ searchEditText.setText(externalKeyword); } search(getText()); } } } //初始化控制元件並且設定控制元件事件 private void initWidget(){ // checkedDrawable = getResources().getDrawable(R.drawable.check_box_true); searchTextView = (TextView)this.findViewById(R.id.search_textview); deleteImageview = (ImageView)this.findViewById(R.id.my_delete); backLayout = (LinearLayout)this.findViewById(R.id.title_back_layout); searchEditText = (EditText)this.findViewById(R.id.search_myedittext); //載入提示控制元件初始化 inflater = LayoutInflater.from(this); footView = inflater.inflate(R.layout.layout_loading_footer, null); loading = (LinearLayout) footView.findViewById(R.id.loading_layout); moreTextView = (TextView) footView.findViewById(R.id.more_texview); moreTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { autoRefresh(); } }); loading.setVisibility(View.GONE); moreTextView.setVisibility(View.GONE); goodsListView = (ListView)this.findViewById(R.id.goods_listview); goodsListView.addFooterView(footView); goodsInfos = new LinkedList<GoodsInfo>(); goodsListAdapter = new GoodsListAdapter(this,goodsInfos,R.color.common_ff0000); goodsListView.setAdapter(goodsListAdapter); //設定監聽,如果滑動到最後一行則進行資料載入 goodsListView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int i) { if (i == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { if (absListView.getLastVisiblePosition() == absListView.getCount() - 1) { autoRefresh(); } } } @Override public void onScroll(AbsListView absListView, int i, int i2, int i3) { } }); popupWindow = new PopupWindow(GoodsSearchActivity.this); //搜尋歷史頁顯示 searchHisScrollView = (ScrollView)this.findViewById(R.id.search_his_popupwindow); //遮罩層 view = (View)this.findViewById(R.id.shade_view); // //檢索按鈕 // searchTextView.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // search(getText()); // mArrayAdapter.notifyDataSetChanged(); // } // }); //刪除鍵監聽,點選刪除輸入框內容 deleteImageview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { searchEditText.setText(""); deleteImageview.setVisibility(View.GONE); } }); //輸入框內容監聽,內容為空,隱藏刪除鍵;反之,顯示刪除鍵 searchEditText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { if (s != null && !"".equals(s.toString().trim())) { deleteImageview.setVisibility(View.VISIBLE); searchHisScrollView.setVisibility(View.VISIBLE); view.setVisibility(View.VISIBLE); } else { deleteImageview.setVisibility(View.GONE); searchHisScrollView.setVisibility(View.GONE); view.setVisibility(View.GONE); } } }); //搜尋 searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { if (i == EditorInfo.IME_ACTION_SEARCH) { search(getText()); mArrayAdapter.notifyDataSetChanged(); } return false; } }); //返回按鈕 backLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); // //品牌選項 // brandTextView.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // brandTextView.setTextColor(getResources().getColor(R.color.common_ff0000)); // showBrandPopupWindow(brandTextView); // } // }); // //價格選項 // matrix = new Matrix(); // bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.default_1)).getBitmap(); // priceLayout.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // if(isUp){ // mPriceSort = "down"; // isUp = false; // rotateImg(180); // }else{ // mPriceSort = "up"; // isUp = true; // rotateImg(180); // } // skip = 0; // doInitLoad(); // } // }); //過濾選項 // filterTextView.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // //改變字顏色 // filterTextView.setTextColor(getResources().getColor(R.color.common_ff0000)); // showFilterPopupWindow(filterTextView); // } // }); //清除 clearTextView = (TextView)this.findViewById(R.id.his_clear_textview); clearTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //取得搜尋歷史資料 SharedPreferencesUtil.savePrivateInfoToSharedPreferences(GoodsSearchActivity.this, "searchwordhis",""); hisList.clear(); mArrayAdapter.notifyDataSetChanged(); } }); } //選裝圖片 private void rotateImg(int rotate){ // 設定旋轉角度 matrix.setRotate(rotate); // 重新繪製Bitmap bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true); priceImageview.setImageBitmap(bitmap); } //搜尋處理 private void search(String keyword){ his = SharedPreferencesUtil.getPrivateInfoFromSharedPreferences(this,"searchwordhis"); mKeywords = keyword; //先轉成陣列然後再進行比較是否已經儲存過,這樣比較相對來說準確一些 if(TextUtils.isEmpty(his)){ if(!TextUtils.isEmpty(mKeywords)){ SharedPreferencesUtil.savePrivateInfoToSharedPreferences(GoodsSearchActivity.this, "searchwordhis", mKeywords); hisList.add(mKeywords); } }else{ String[] array; if(!hisList.contains(mKeywords)){ if(!TextUtils.isEmpty(mKeywords)){ String saveStr = ""; if(!TextUtils.isEmpty(his)) saveStr = mKeywords + "," + his; else saveStr = mKeywords; SharedPreferencesUtil.savePrivateInfoToSharedPreferences(GoodsSearchActivity.this, "searchwordhis", saveStr); //因為排序問題所以需要重新再加入list array = saveStr.split(","); hisList.clear(); hisList.addAll(Arrays.asList(array)); } } } searchHisScrollView.setVisibility(View.GONE); view.setVisibility(View.GONE); //搜尋 skip = 0; doInitLoad(); } //獲得螢幕尺寸 private void initScreenWidth() { DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); screenHeight = dm.heightPixels; screenWidth = dm.widthPixels; } //獲取輸入框內容 public String getText() { if (searchEditText == null) { return null; } String text = searchEditText.getText().toString(); return text; // if (!TextUtils.isEmpty(text)){ // if (!"".equals(externalKeyword)&&null!=externalKeyword){ // return externalKeyword; // } // } } //顯示搜尋歷史和熱詞搜尋popupwindow public void initSearchHis() { //得到歷史資料 his = SharedPreferencesUtil.getPrivateInfoFromSharedPreferences(this,"searchwordhis"); //搜尋歷史 hisListview = (ListView)this.findViewById(R.id.his_listview); hisList = new ArrayList<String>(); if (!TextUtils.isEmpty(his)){ String[] hisArray = his.split(","); hisList.addAll(Arrays.asList(hisArray)); } mArrayAdapter = new ArrayAdapter<String>(this,R.layout.search_his_item,hisList); hisListview.setAdapter(mArrayAdapter); //熱詞橫向滾動 /*horizontalScrollView = (HorizontalScrollView)this.findViewById(R.id.hotword_horizontalscrollview); conditionLayout = (LinearLayout)this.findViewById(R.id.condition_layout);*/ /* hotWordOnClickListener = new View.OnClickListener() { @Override public void onClick(View view) { if (TextView.class.isInstance(view)) { //得到品牌對應的系列資料 String keyword = ((TextView) view).getText().toString(); search(keyword); mArrayAdapter.notifyDataSetChanged(); } } };*/ hisListview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { if (TextView.class.isInstance(view)) { //得到品牌對應的系列資料 String keyword = ((TextView) view).getText().toString(); search(keyword); } } }); hotWordList = new ArrayList<String>(); hotWordListView = (MyListView)this.findViewById(R.id.hotword_listview); hotwordAdapter = new ArrayAdapter<String>(this,R.layout.search_his_item,hotWordList); hotWordListView.setAdapter(hotwordAdapter); hotWordListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { if (TextView.class.isInstance(view)) { //得到品牌對應的系列資料 String keyword = ((TextView) view).getText().toString(); search(keyword); } } }); // doSearchHotWord(); } // //查詢熱詞 // private void doSearchHotWord() { // //檢索資料前首先檢查網路是否可用 // if(!BaseApplication.getInstance().isNetworkAvailable(this)){ // Toast.makeText(this, getString(R.string.no_network_toast), Toast.LENGTH_SHORT).show(); // return; // } // getStatusTip().showProgress(); // StringRequest postRequest = new StringRequest(Request.Method.POST, Constant.hotWordUrl, // new Response.Listener<String>(){ // @Override // public void onResponse(String response) { // //處理返回結果 // parseWordJson(response); // getStatusTip().hideProgress(); // } // }, // new Response.ErrorListener() { // @Override // public void onErrorResponse(VolleyError error) { // Toast.makeText(GoodsSearchActivity.this,getString(R.string.reload_txt), Toast.LENGTH_SHORT).show(); // getStatusTip().hideProgress(); // } // }); // BaseApplication.mRequestQueue.add(postRequest); // } // //解析json資料 // private void parseWordJson(String json){ // Gson gson = new Gson(); // HotWordResult hotWordResult = gson.fromJson(json, HotWordResult.class); // //有錯誤資訊返回 // if(hotWordResult != null // && "0".equals(hotWordResult.status.succeed)){ // Toast.makeText(GoodsSearchActivity.this,hotWordResult.status.error_desc, Toast.LENGTH_SHORT).show(); // return; // }else{ // if(hotWordResult.data != null&& hotWordResult.data.size() > 0 ){ // hotWordList.addAll(hotWordResult.data); // hotwordAdapter.notifyDataSetChanged(); // /* //成功返回品牌list // int i = 0; // for(String hotword:hotWordList){ // TextView tv = makeHotWordTextView(hotword,i, 16,3, // hotWordOnClickListener); // conditionLayout.addView(tv); // i++; // }*/ // } // } // } // // //顯示品牌popupwindow // public void showBrandPopupWindow(View anchor) { // popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { // @Override // public void onDismiss() { // //改變字顏色 // brandTextView.setTextColor(getResources().getColor(R.color.common_666666)); // view.setVisibility(View.GONE); // } // }); // popupWindow.setWidth(screenWidth); // popupWindow.setHeight(screenHeight*2/3); // popupWindow.setContentView(brandContentView); // popupWindow.setFocusable(true); // popupWindow.setOutsideTouchable(true); // popupWindow.setBackgroundDrawable(new PaintDrawable(getResources().getColor(R.color.homepage_bg))); // popupWindow.showAsDropDown(anchor); // view.setVisibility(View.VISIBLE); // } // private void initBrandPopupwindow(){ // brandContentView = LayoutInflater.from(GoodsSearchActivity.this).inflate(R.layout.brand_popupwindow, null); // brandSupperLayout = (SupperLayout) brandContentView.findViewById(R.id.brand_superlayout); // mOnClickListener = new View.OnClickListener() { // @Override // public void onClick(View v) { // if (TextView.class.isInstance(v)) { // //得到品牌對應的系列資料 // getSerial(((TextView) v).getId()); // if(saveBrandTextView != null) saveBrandTextView.setTextColor(getResources().getColor(R.color.item_light_gray)); // ((TextView) v).setTextColor(getResources().getColor(R.color.common_ff0000)); // saveBrandTextView = ((TextView) v); // } // } // }; // //系列 // serialListView = (ListView)brandContentView.findViewById(R.id.serial_listview); // mSerialList = new LinkedList<CategoryInfo>(); // mSerialArrayAdapter = new SerialListAdapter(this,mSerialList); // serialListView.setAdapter(mSerialArrayAdapter); // serialListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { // @Override // public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { // mCatId = mSerialList.get(i).cat_id; // //進行搜尋 // skip = 0; // doInitLoad(); // popupWindow.dismiss(); // } // }); // /* mSerialOnClickListener = new View.OnClickListener() { // @Override // public void onClick(View v) { // if (TextView.class.isInstance(v)) { // mCatId = String.valueOf(((TextView)v).getId()); // //進行搜尋 // skip = 0; // doInitLoad(); // popupWindow.dismiss(); // } // } // };*/ // doSearchBrand(); // } //顯示篩選popupwindow public void showFilterPopupWindow(View anchor) { // mSexRadioGroup = (FlowRadioGroup)contentView.findViewById(R.id.sex_radiogroup); // mPriceRadioGroup = (FlowRadioGroup)contentView.findViewById(R.id.price_radiogroup); // mSexRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { // @Override // public void onCheckedChanged(RadioGroup radioGroup, int i) { // RadioButton rb = (RadioButton)radioGroup.getChildAt(i); // mCrowd = rb.getText().toString(); // } // }); // mPriceRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { // @Override // public void onCheckedChanged(RadioGroup radioGroup, int i) { // RadioButton rb = (RadioButton)radioGroup.getChildAt(i); // mPrice = rb.getText().toString(); // } // }); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { //改變字顏色 // filterTextView.setTextColor(getResources().getColor(R.color.common_666666));//第一個報錯的控制元件,原因是沒有找到-------------------------------- view.setVisibility(View.GONE); } }); popupWindow.setWidth(screenWidth); popupWindow.setHeight(screenHeight*2/3); popupWindow.setContentView(filterContentView); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new PaintDrawable(getResources().getColor(R.color.common_f5f5f5))); popupWindow.showAsDropDown(anchor); view.setVisibility(View.VISIBLE); //保留狀態 saveFilterState(); //getFilterState(); } //初始化狀態list // private void initFilterPopupWindow(){ // selectedSexList = new LinkedList<TextView>(); // selectedList = new LinkedList<TextView>(); // selectedPriceList = new LinkedList<TextView>(); // filterContentView = LayoutInflater.from(GoodsSearchActivity.this).inflate( // R.layout.filter_popupwindow, null); // mSexSupperLayout = (SupperLayout) filterContentView.findViewById( // R.id.right_menu_sex_superlayout); // mFilterSexOnClickListener = new View.OnClickListener() { // @Override // public void onClick(View v) { //