轉載請注明出處王亟亟的大牛之路
美團和大眾點評的搜索和篩選欄,不能說好看(審美flag),但是滿足了我們的搜索還篩選的需求,所以今天就實現了下這樣的一個小Demo
項目結構:運行效果就是下圖了,就不再截圖了
初始化的樣子:
具體實現就是起初3個LinearLayout,點擊之后出現一個ListView然后會根據對這個LisvView的點擊可能會再出現下一級別的子菜單。
廢話不多說,直接上代碼
MainActivity
public class MainActivity extends Activity implements OnClickListener,
OnDismissListener{private ImageView icon1, icon2, icon3;
private TextView fenlei, diqu, paixu;
private LinearLayout ll_top;
private LinearLayout ll_fenlei, ll_diqu, ll_paixu;
private ListView lv1, lv2,lv3, lv4;
private LinearLayout ll2;
private PopupWindow popLeft,popMid,popRight;
private List<father> fatlist;
private List<son> sonlist,sonlist1,sonlist2,sonlist3,sonlist4;
private List<son> right;
private int screenWidth;
private int screenHeight;
private MyAdapterLeft adapterleft;
private MyAdapterSon adapterleftson;
private MyAdapterRight adapterRight;
private int imaPosition;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initScreenWidth();intifatherlist(); intiView();}
private void intiView() {ll_top =(LinearLayout) findViewById(R.id.ll_layout);ll_fenlei = (LinearLayout) findViewById(R.id.ll_quyu);ll_diqu = (LinearLayout) findViewById(R.id.ll_jiage);ll_paixu = (LinearLayout) findViewById(R.id.ll_huxing);fenlei = (TextView) findViewById(R.id.fenlei);diqu = (TextView) findViewById(R.id.diqu);paixu = (TextView) findViewById(R.id.paixu);icon1 = (ImageView) findViewById(R.id.icon1);icon2 = (ImageView) findViewById(R.id.icon2);icon3 = (ImageView) findViewById(R.id.icon3);ll_fenlei.setOnClickListener(
this);ll_diqu.setOnClickListener(
this);ll_paixu.setOnClickListener(
this); getPopLeft();getPopRight();}
/**初始化父類和子類*/private void intifatherlist() {fatlist =
new ArrayList<father>();sonlist =
new ArrayList<son>();sonlist1 =
new ArrayList<son>();sonlist2 =
new ArrayList<son>();sonlist3 =
new ArrayList<son>();sonlist4 =
new ArrayList<son>();
for (
int i =
0; i <
5; i++) {son s =
new son();s.setId(i+
"");s.setName(
"子菜單"+i);sonlist.add(s);}
for (
int i =
0; i <
15; i++) {son s =
new son();s.setId(i+
"");s.setName(
"子菜單"+i);sonlist1.add(s);}
for (
int i =
0; i <
5; i++) {son s =
new son();s.setId(i+
"");s.setName(
"子菜單"+i);sonlist2.add(s);}
for (
int i =
0; i <
6; i++) {son s =
new son();s.setId(i+
"");s.setName(
"子菜單"+i);sonlist3.add(s);}
for (
int i =
0; i <
13; i++) {son s =
new son();s.setId(i+
"");s.setName(
"子菜單"+i);sonlist4.add(s);}
/**初始化父類*/for (
int i =
0; i <
5; i++) {father father =
new father();father.setId(i+
"");father.setName(
"主菜單"+i);
if (i==
0) {father.setImage(R.drawable.ic_category_all);father.setSonList(
null);}
if (i==
1) {father.setImage(R.drawable.ic_category_entertainment);father.setSonList(sonlist1);}
if (i==
2) {father.setImage(R.drawable.ic_category_food);father.setSonList(sonlist2);}
if (i==
3) {father.setImage(R.drawable.ic_category_health); father.setSonList(sonlist3);}
if (i==
4) {father.setImage(R.drawable.ic_category_hot);father.setSonList(sonlist4);}fatlist.add(father);}}
/*** @Title: initScreenWidth* @Description: 查看自身的寬高* @author yimei* @return void 返回類型*/private void initScreenWidth() {DisplayMetrics dm =
new DisplayMetrics();dm = getResources().getDisplayMetrics();screenHeight = dm.heightPixels;screenWidth = dm.widthPixels;}
@Overridepublic void onClick(View v) {
switch (v.getId()) {
case R.id.ll_quyu:getPopLeft();popLeft.showAsDropDown(ll_top);
break;
case R.id.ll_jiage:
break;
case R.id.ll_huxing:getPopRight();popRight.showAsDropDown(ll_top);
break;}}
@Overridepublic void onDismiss() {}
/***獲取PopupWindow實例 .分類*/ private void getPopLeft() {
if (
null != popLeft) { popLeft.dismiss();
return ; }
else {initPopLeft();}
}
/***獲取PopupWindow實例 .分類*/ private void getPopRight() {
if (
null != popRight) { popRight.dismiss();
return ; }
else {initPopRight();}
}
/** * 創建分類彈出PopupWindow */ protected void initPopLeft() { View left_view = getLayoutInflater().inflate(R.layout.popleft,
null,
false); left_view.setFocusable(
true); left_view.setFocusableInTouchMode(
true);popLeft =
new PopupWindow(left_view,screenWidth, screenHeight,
true);popLeft.setFocusable(
true);left_view.setOnKeyListener(
new OnKeyListener() {
@Overridepublic boolean onKey(View v,
int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {popLeft.dismiss();
return true;}
return false;}});
left_view.setOnTouchListener(
new OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
if (popLeft !=
null && popLeft.isShowing()) { popLeft.dismiss(); }
return false; }}); lv1 = (ListView) left_view.findViewById(R.id.lv1); lv2 = (ListView) left_view.findViewById(R.id.lv2); ll2 = (LinearLayout) left_view.findViewById(R.id.ll2);adapterleft =
new MyAdapterLeft(MainActivity.
this, fatlist);lv1.setAdapter(adapterleft);lv1.setOnItemClickListener(
new OnItemClickListener() {
@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,
int position,
long arg3) {adapterleft.setSelectItem(position);imaPosition = position;adapterleft.notifyDataSetChanged();adapterleftson =
new MyAdapterSon(MainActivity.
this,fatlist.get(position).getSonList());lv2.setAdapter(adapterleftson);
if (fatlist.get(position).getSonList()!=
null) {lv2.setVisibility(View.VISIBLE);lv2.setOnItemClickListener(
new OnItemClickListener() {
@Overridepublic void onItemClick(AdapterView<?> parent,View view,
int position,
long id) {adapterleftson.setSelectItem(position);String name =adapterleftson.getItem(position).getName();setHeadText(
1, name,fatlist.get(imaPosition).getImage());popLeft.dismiss();}});}
else {String name = fatlist.get(position).getName();
int img = fatlist.get(position).getImage();setHeadText(
1, name,img);popLeft.dismiss();}}});}
/**最右邊的彈窗,排序*/protected void initPopRight() { View left_right = getLayoutInflater().inflate(R.layout.popright,
null,
false); left_right.setFocusable(
true); left_right.setFocusableInTouchMode(
true);popRight =
new PopupWindow(left_right,screenWidth, screenHeight,
true);popRight.setFocusable(
true);left_right.setOnKeyListener(
new OnKeyListener() {
@Overridepublic boolean onKey(View v,
int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {popRight.dismiss();
return true;}
return false;}});
left_right.setOnTouchListener(
new OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
if (popRight !=
null && popRight.isShowing()) { popRight.dismiss(); }
return false; }}); lv3 = (ListView) left_right.findViewById(R.id.lv3); right =
new ArrayList<son>();
for (
int i =
0; i <
4; i++) {son s =
new son();s.setId(i+
"");
if (i==
0) {s.setName(
"智能排序");}
if (i==
1) {s.setName(
"好評優先");}
if (i==
2) {s.setName(
"離我最近");}
if (i==
3) {s.setName(
"人均最低");}right.add(s);}adapterRight =
new MyAdapterRight(MainActivity.
this, right);lv3.setAdapter(adapterRight);lv3.setOnItemClickListener(
new OnItemClickListener() {
@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,
int position,
long arg3) {adapterRight.setSelectItem(position);String name = right.get(position).getName();setHeadText(
3, name,
0);popRight.dismiss();}});}
/*** @Title: setHeadText* @Description: 點擊之后設置在上邊的TextView里* @author yimei* @return void 返回類型*/private void setHeadText(
int idx, String text,
int image) {
switch (idx) {
case 1:fenlei.setText(text);icon1.setImageResource(image);
break;
case 2:diqu.setText(text);
break;
case 3:paixu.setText(text);
break;}} }
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"tools:context=".MainActivity" ><LinearLayout
android:id="@+id/ll_layout"android:layout_width="match_parent"android:layout_height="40dip"android:background="#FFFFFF"android:orientation="horizontal" ><LinearLayout
android:id="@+id/ll_quyu"android:layout_width="0dip"android:layout_height="40dip"android:layout_weight="1"android:gravity="center_vertical|center_horizontal"android:orientation="horizontal"android:paddingLeft="10dip" ><ImageView
android:id="@+id/icon1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/documents" /><TextView
android:id="@+id/fenlei"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_vertical"android:text="@string/quyu"android:textColor="#4d4d4d" /></LinearLayout><View
android:layout_width="1dip"android:layout_height="20dip"android:layout_gravity="center_vertical"android:background="#e6e6e6" /><LinearLayout
android:id="@+id/ll_jiage"android:layout_width="0dip"android:layout_height="40dip"android:layout_weight="1"android:gravity="center_vertical|center_horizontal"android:orientation="horizontal"android:paddingLeft="10dip" ><ImageView
android:id="@+id/icon2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/location_pin" /><TextView
android:id="@+id/diqu"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_vertical"android:text="@string/jiage"android:textColor="#4d4d4d" /></LinearLayout><View
android:layout_width="1dip"android:layout_height="20dip"android:layout_gravity="center_vertical"android:background="#e6e6e6" /><LinearLayout
android:id="@+id/ll_huxing"android:layout_width="0dip"android:layout_height="40dip"android:layout_weight="1"android:gravity="center_vertical|center_horizontal"android:orientation="horizontal"android:paddingLeft="10dip" ><ImageView
android:id="@+id/icon3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/resize_100" /><TextView
android:id="@+id/paixu"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_vertical"android:text="@string/huxing"android:textColor="#4d4d4d" /></LinearLayout></LinearLayout><View
android:layout_width="match_parent"android:layout_height="1dip"android:layout_below="@id/ll_layout"android:background="#e6e6e6" /></RelativeLayout>
參數的適配器(就貼一個了,這部分沒什么,看源碼更清楚)
public class MyAdapterLeft extends BaseAdapter {private List<father> l;
private Context context;
private int selectItem = -
1;
public MyAdapterLeft(Context context, List<father> l) {
this.context = context;
this.l = l;}
@Overridepublic int getCount() {
return l.size();}
@Overridepublic Object
getItem(
int position) {
return l.get(position);}
@Overridepublic long getItemId(
int position) {
return position;}
@Overridepublic View
getView(
int position, View convertView, ViewGroup parent) {Holder holder =
null;
if (convertView ==
null) {holder =
new Holder();convertView = LayoutInflater.from(context).inflate(R.layout.item,
null);holder.name = (TextView) convertView.findViewById(R.id.name);holder.img = (ImageView) convertView.findViewById(R.id.img);convertView.setTag(holder);}
else {holder = (Holder) convertView.getTag();}holder.name.setText(l.get(position).getName());holder.img.setImageResource(l.get(position).getImage());
if (position == selectItem) {convertView.setBackgroundColor(context.getResources().getColor(R.color.click));holder.name.setTextColor(Color.BLUE);}
else {convertView.setBackgroundColor(context.getResources().getColor(R.color.defult));holder.name.setTextColor(Color.BLACK);}
return convertView;}
public void setSelectItem(
int selectItem) {
this.selectItem = selectItem;}class Holder {TextView name;ImageView img;}
private void show(String str) {Toast.makeText(context, str,
0).show();}
}
一些比較重要的內容就這些了,子層級的item啊,father son的對象聲明啊,看源碼吧,只是用于填充數據的容器而已
源碼:http://yunpan.cn/cmwJbWZ3FdpdZ 訪問密碼 493b
部分內容參考互聯網,如有重復請見諒
總結
以上是生活随笔為你收集整理的android 粗暴简单仿美团/大众点评搜索方式,详细标注的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。