完整APP项目源码(含服务)-APP搜索功能
TPshop完整APP项目(含服务器源码)第三课 - 商城搜索功能.
看图如下:
功能描述: 点击顶部文本输入框弹出软键盘, 输入关键字后, 点击键盘上的"搜索"键后请求后台搜索API
并将结果展示在列表中.
结构分析: 自上而下, 顶部是一个自定义View的searchView ,接下来是一个后台设置的热词列表, 下面是一个搜索记录的listview
顶部SPSearchView
public class SPSearchView extends LinearLayout { private ImageView backImgv; private EditText searchEdtv; private SPSearchViewListener searchListener; /** * @param context * @param attrs */ public SPSearchView(Context context, AttributeSet attrs) { super(context, attrs); View view = LayoutInflater.from(context).inflate(R.layout.search_heard_view, this); searchEdtv = (EditText)view.findViewById(R.id.search_edtv); backImgv = (ImageView)view.findViewById(R.id.back_imgv); searchEdtv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (searchListener != null) { //searchListener.onSearchBoxClick(searchEdtv.getText().toString()); } } }); backImgv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (searchListener != null) searchListener.onBackClick(); } }); searchEdtv.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) { //响应键盘"搜索"键 if (actionId == EditorInfo.IME_ACTION_SEARCH) { notifyStartSearching(textView.getText().toString()); } return true; } }); } /** * 通知监听者 进行搜索操作 * @param text */ private void notifyStartSearching(String text){ if (searchListener != null) { searchListener.onSearchBoxClick(text); } } public void setSearchKey(String searchKey){ if (this.searchEdtv!=null && !SPStringUtils.isEmpty(searchKey)){ this.searchEdtv.setText(searchKey); } } public EditText getSearchEditText(){ return this.searchEdtv; } public void setSearchViewListener(SPSearchViewListener listener){ this.searchListener = listener; } public interface SPSearchViewListener{ public void onBackClick(); public void onSearchBoxClick(String keyword); } }
搜索界面Activity的实现:
布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.soubao.tpshop.view.SPSearchView android:id="@+id/search_view" android:layout_width="match_parent" android:layout_height="50dip" /> <ListView android:id="@+id/search_key_listv" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@color/separator_line" android:dividerHeight="1px" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:layout_marginTop="50dip" android:layout_marginBottom="50dip" android:scrollbars="none" android:smoothScrollbar="true" /> <Button android:id="@+id/search_delete_btn" android:layout_width="match_parent" android:layout_height="@dimen/height_button" android:layout_alignParentBottom="true" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginBottom="10dip" style="@style/textStyle.Normal.black" android:text="@string/delete_history" android:background="@drawable/tag_button_bg_unchecked" android:layout_centerHorizontal="true"/> </RelativeLayout>
onCreate中设置自定义标题
@Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); }
搜索记录缓存到SharedPrefreences中
public void loadKey(){ mSearchkeys = new ArrayList<String>(); String searchKey = SPSaveData.getString(this, SPMobileConstants.KEY_SEARCH_KEY); if (!SPStringUtils.isEmpty(searchKey)){ String[] keys = searchKey.split(","); if (keys !=null) for(int i=0; i< keys.length; i++){ if (!SPStringUtils.isEmpty(keys[i])){ mSearchkeys.add(keys[i]); } } } } public void saveKey(String key){ String searchKey = SPSaveData.getString(this, SPMobileConstants.KEY_SEARCH_KEY); if (!SPStringUtils.isEmpty(searchKey) && !searchKey.contains(key)) { searchKey+=","+key; }else{ searchKey = key; } SPSaveData.putValue(this, SPMobileConstants.KEY_SEARCH_KEY, searchKey); }
完整源码下载地址: demo tpshop安卓源码下载
相关推荐
瓜牛呱呱 2020-11-12
柳木木的IT 2020-11-04
yifouhu 2020-11-02
lei0 2020-11-02
源码zanqunet 2020-10-28
源码zanqunet 2020-10-26
一叶梧桐 2020-10-14
码代码的陈同学 2020-10-14
lukezhong 2020-10-14
lzzyok 2020-10-10
anchongnanzi 2020-09-21
clh0 2020-09-18
changcongying 2020-09-17
星辰大海的路上 2020-09-13
abfdada 2020-08-26
mzy000 2020-08-24
shenlanse 2020-08-18
zhujiangtaotaise 2020-08-18
xiemanR 2020-08-17