完整APP项目源码(含服务)-APP搜索功能

TPshop完整APP项目(含服务器源码)第三课 - 商城搜索功能.

看图如下:

完整APP项目源码(含服务)-APP搜索功能_第1张图片

完整APP项目源码(含服务)-APP搜索功能_第2张图片

功能描述: 点击顶部文本输入框弹出软键盘, 输入关键字后, 点击键盘上的"搜索"键后请求后台搜索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的实现:

布局文件:

 




    

    

    

onCreate中设置自定义标题

 

@Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
    }

搜索记录缓存到SharedPrefreences中

 

public void loadKey(){
        mSearchkeys = new ArrayList();
        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安卓源码下载

 

你可能感兴趣的:(完整APP项目源码(含服务)-APP搜索功能)