首页 > 代码库 > 两种方式实现类似qq搜索的切换

两种方式实现类似qq搜索的切换

qq的搜索功能在点击搜索框时整个页面上移,出现透明布局。该效果是模仿iOS实现的,但是在Android也是很容易实现的。于是就花了点时间仿照效果写了一个demo。可能实现方式并不是完全相同。

具体请看效果图:



详细说明略。第一种是利用隐藏的透明LinearLayout实现效果。第二、三种是通过PopupWindow实现的,只有一处区别,即搜索框的两种状态。第二种采用的是ListView的addHeaderView实现的。

不多说直接上代码

第一种

/********************************************************** 
 * @文件名称:MainActivity.java 
 * @创建时间:2014年11月21日 下午8:12:24
 * @修改历史:2014年11月21日
 **********************************************************/
package com.jinlin.searchview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

/**
 * @author J!nl!n
 * @date 2014年11月21日
 * @time 下午8:12:24
 * @type MainActivity.java
 * @todo
 */
public class Activity1 extends Activity implements OnClickListener{
	protected static final String TAG = Activity1.class.getSimpleName();

	// 容器布局
	private LinearLayout container;

	// 标题栏布局
	private View titleView;

	private View searchView;

	private ListView listView;

	// 输入框是否获取到焦点
	private static boolean isFocused = false;

	// 图标居中输入框
	private EditText et_search;

	// 真是输入框
	private EditText et_input;

	// 取消按钮
	private Button btn_cancel;

	// 显示或隐藏软键盘
	private enum KeyBoardType {
		show, hide;
	}
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity1);
		initView();
	}

	private List<String> getData() {
		List<String> data = http://www.mamicode.com/new ArrayList();>

第二种

/********************************************************** 
 * @文件名称:Activity2.java 
 * @创建时间:2014年11月22日 下午9:54:31
 * @修改历史:2014年11月22日
 **********************************************************/
package com.jinlin.searchview;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author J!nl!n
 * @date 2014年11月22日
 * @time 下午9:54:31
 * @type Activity2.java
 * @todo
 */
public class Activity2 extends Activity implements OnClickListener, OnItemClickListener,
		PopupWindow.OnDismissListener {
	private TextView tv_top_title;
	
	private ListView listView;
	private View headerView;
	private TextView tv_search;

	// show and hide
	private RelativeLayout mainLayout;
	private RelativeLayout titleBarLayout;
	private int moveHeight;
	private int statusBarHeight;

	// search popupwindow
	private PopupWindow popupWindow;
	private View searchView;
	private EditText searchEditText;
	private TextView cancelTextView;
	private ListView filterListView;
	private View alphaView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity2);

		tv_top_title = (TextView) findViewById(R.id.tv_top_title);
		tv_top_title.setText("我是第二种");
		initCtrl();
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_search:
			showSearchBar();
			break;
		case R.id.popup_window_tv_cancel:
			dismissPopupWindow();
			break;
		case R.id.popup_window_v_alpha:
			dismissPopupWindow();
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> viewGroup, View view, int position, long arg3) {
		switch (viewGroup.getId()) {
		case R.id.lv:
			if (position == 0) {
				showSearchBar();
			}
			break;
		case R.id.popup_window_lv:
			Toast.makeText(Activity2.this, "click-" + position, Toast.LENGTH_LONG).show();
			break;
		}
	}

	@Override
	public void onDismiss() {
		resetUI();
	}

	private void initCtrl() {
		listView = (ListView) findViewById(R.id.lv);
		LayoutInflater mInflater = LayoutInflater.from(this);
		headerView = mInflater.inflate(R.layout.activity2_3_search, null);
		listView.addHeaderView(headerView);
		listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData()));
		listView.setOnItemClickListener(this);
		tv_search = (TextView) headerView.findViewById(R.id.tv_search);
		tv_search.setOnClickListener(this);

		mainLayout = (RelativeLayout) findViewById(R.id.main);
		titleBarLayout = (RelativeLayout) findViewById(R.id.title_bar_layout);

		searchView = mInflater.inflate(R.layout.popup_window_search, null);
		searchEditText = (EditText) searchView.findViewById(R.id.popup_window_et_search);
		searchEditText.setFocusable(true);
		searchEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.toString().equals("")) {
					alphaView.setVisibility(View.VISIBLE);
					filterListView.setVisibility(View.GONE);
				} else {
					alphaView.setVisibility(View.GONE);
					filterListView.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		cancelTextView = (TextView) searchView.findViewById(R.id.popup_window_tv_cancel);
		cancelTextView.setOnClickListener(this);
		filterListView = (ListView) searchView.findViewById(R.id.popup_window_lv);
		filterListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData2()));
		filterListView.setOnItemClickListener(this);
		alphaView = searchView.findViewById(R.id.popup_window_v_alpha);
		alphaView.setOnClickListener(this);

		popupWindow = new PopupWindow(searchView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setOnDismissListener(this);
	}

	private List<String> getData() {
		List<String> data = http://www.mamicode.com/new ArrayList();>

第三种

/********************************************************** 
 * @文件名称:Activity3.java 
 * @创建时间:2014年11月22日 下午9:54:53
 * @修改历史:2014年11月22日
 **********************************************************/
package com.jinlin.searchview;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author J!nl!n
 * @date 2014年11月22日
 * @time 下午9:54:53
 * @type Activity3.java
 * @todo
 */
public class Activity3 extends Activity implements OnClickListener, OnItemClickListener, PopupWindow.OnDismissListener {
	private TextView tv_top_title;
	
	private ListView listView;
	private TextView tv_search;

	// show and hide
	private LinearLayout mainLayout;
	private RelativeLayout titleBarLayout;
	private int moveHeight;
	private int statusBarHeight;

	// search popupwindow
	private PopupWindow popupWindow;
	private View searchView;
	private EditText searchEditText;
	private TextView cancelTextView;
	private ListView filterListView;
	private View alphaView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity3);

		tv_top_title = (TextView) findViewById(R.id.tv_top_title);
		tv_top_title.setText("我是第三种");
		initCtrl();
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_search:
			showSearchBar();
			break;
		case R.id.popup_window_tv_cancel:
			dismissPopupWindow();
			break;
		case R.id.popup_window_v_alpha:
			dismissPopupWindow();
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> viewGroup, View view, int position, long arg3) {
		switch (viewGroup.getId()) {
		case R.id.lv:
			if (position == 0) {
				showSearchBar();
			}
			break;
		case R.id.popup_window_lv:
			Toast.makeText(Activity3.this, "click-" + position, Toast.LENGTH_LONG).show();
			break;
		}
	}

	@Override
	public void onDismiss() {
		resetUI();
	}

	private void initCtrl() {
		listView = (ListView) findViewById(R.id.lv);

		LayoutInflater mInflater = LayoutInflater.from(this);

		listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData()));
		listView.setOnItemClickListener(this);

		tv_search = (TextView) findViewById(R.id.tv_search);
		tv_search.setOnClickListener(this);

		mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
		titleBarLayout = (RelativeLayout) findViewById(R.id.title_bar_layout);

		searchView = mInflater.inflate(R.layout.popup_window_search, null);
		searchEditText = (EditText) searchView.findViewById(R.id.popup_window_et_search);
		searchEditText.setFocusable(true);
		searchEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.toString().equals("")) {
					alphaView.setVisibility(View.VISIBLE);
					filterListView.setVisibility(View.GONE);
				} else {
					alphaView.setVisibility(View.GONE);
					filterListView.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		cancelTextView = (TextView) searchView.findViewById(R.id.popup_window_tv_cancel);
		cancelTextView.setOnClickListener(this);
		filterListView = (ListView) searchView.findViewById(R.id.popup_window_lv);
		filterListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData2()));
		filterListView.setOnItemClickListener(this);
		alphaView = searchView.findViewById(R.id.popup_window_v_alpha);
		alphaView.setOnClickListener(this);

		popupWindow = new PopupWindow(searchView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setOnDismissListener(this);
	}

	private List<String> getData() {
		List<String> data = http://www.mamicode.com/new ArrayList();>githib地址:https://github.com/5peak2me/QQSearchView

两种方式实现类似qq搜索的切换