效果图
一个类似于频道管理的效果
取消掉ActionBar,用Toolbar代替,布局Main xml文件
依赖:compile 'com.jakewharton:butterknife:7.0.1'
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="45dp" android:background="#ffff4c06"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="周边" android:textColor="#fff" android:textSize="20dp"/> </android.support.v7.widget.Toolbar> <!--从这里开始,就是自定义多条件筛选菜单控件的布局核心所在--> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#E2E2E2" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:background="#fff" android:orientation="horizontal" > <LinearLayout android:id="@+id/supplier_list_product" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="horizontal" > <TextView android:id="@+id/supplier_list_product_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全部" android:textSize="14dp" /> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:src="@drawable/icon_arrow_down"/> </LinearLayout> <LinearLayout android:id="@+id/supplier_list_sort" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="horizontal" > <TextView android:id="@+id/supplier_list_sort_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="综合排序" android:textSize="14dp" /> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:src="@drawable/icon_arrow_down"/> </LinearLayout> <LinearLayout android:id="@+id/supplier_list_activity" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="horizontal" > <TextView android:id="@+id/supplier_list_activity_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="优惠活动" android:textSize="14dp" /> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:src="@drawable/icon_arrow_down"/> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#E2E2E2" /> <ListView android:id="@+id/supplier_list_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:cacheColorHint="#00000000" android:divider="#f0f0f0" android:dividerHeight="10dp" android:fadingEdge="none" android:listSelector="#00000000" android:scrollbarStyle="outsideOverlay" android:scrollingCache="false" > </ListView> </LinearLayout>
主要的MainActivity代码
你可以用自己的方法实现找控件和点击效果
package com.example.pop; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.SimpleAdapter; import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import butterknife.Bind; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @Bind(R.id.supplier_list_product_tv) TextView mProductTv; // 可以修改名称 @Bind(R.id.supplier_list_product) LinearLayout mProduct; @Bind(R.id.supplier_list_sort_tv) TextView mSortTv; // 可以修改名称 @Bind(R.id.supplier_list_sort) LinearLayout mSort; @Bind(R.id.supplier_list_activity_tv) TextView mActivityTv; // 可以修改名称 @Bind(R.id.supplier_list_activity) LinearLayout mActivity; @Bind(R.id.supplier_list_lv) ListView mSupplierListLv; private ArrayList<Map<String, String>> mMenuDatal; private ArrayList<Map<String, String>> mMenuData2; private ArrayList<Map<String, String>> mMenuData3; private PopupWindow mPopWindow; private ListView mPopListview; private SimpleAdapter mMenuAdapter1; private SimpleAdapter mMenuAdapter2; private SimpleAdapter mMenuAdapter3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //初始化PopupWindow所需内容 initData(); //初始化PopupWindow事件 initPopMenu(); } private void initPopMenu() { //把包裹listview布局的xml转成对象 View popView = LayoutInflater.from(this).inflate(R.layout.popwin_supplier_list, null); //创建pop对象 布局,宽高 mPopWindow = new PopupWindow(popView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); //设置PopupWindow外部可以点击 mPopWindow.setOutsideTouchable(true); //设置PopupWindow里面的listview有焦点 mPopWindow.setFocusable(true); //设置动画要有一个背景色 mPopWindow.setBackgroundDrawable(new ColorDrawable()); //设置结束时监听 mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { //设置TextView的颜色,把所有LinearLayout的文本颜色该为灰色 mProductTv.setTextColor(Color.parseColor("#5a5959")); mSortTv.setTextColor(Color.parseColor("#5a5959")); mActivityTv.setTextColor(Color.parseColor("#5a5959")); } }); //设置PopupWindow点击以外的地方,使他消失 LinearLayout list_ovttom = (LinearLayout) popView.findViewById(R.id.popwin_supplier_list_bottom); list_ovttom.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mPopWindow.dismiss(); } }); //获取Listview对象 mPopListview = (ListView) popView.findViewById(R.id.popwin_supplier_list_lv); //创建SimpleAdater,一个listview安卓原生封装的适配器 mMenuAdapter1 = new SimpleAdapter(this, mMenuDatal, R.layout.item_listview_powin, new String[]{"name"}, new int[]{R.id.listview_popwind_tv}); mMenuAdapter2 = new SimpleAdapter(this, mMenuData2, R.layout.item_listview_powin, new String[]{"name"}, new int[]{R.id.listview_popwind_tv}); mMenuAdapter3 = new SimpleAdapter(this, mMenuData3, R.layout.item_listview_powin, new String[]{"name"}, new int[]{R.id.listview_popwind_tv}); //设置一个标记,对点击的不同做出相应的操作 //设置PopupWindow里面的listview点击事件 mPopListview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { //首先让PopupWindow消失 mPopWindow.dismiss(); switch (menuIndex){ case 0: String currentProduct = mMenuDatal.get(i).get("name"); mProductTv.setText(currentProduct); break; case 1: String currentSort = mMenuData2.get(i).get("name"); mSortTv.setText(currentSort); break; case 2: String currentActivity = mMenuData3.get(i).get("name"); mActivityTv.setText(currentActivity); break; } } }); } //初始化数据 private void initData() { //创建加载布局的容器 mMenuDatal = new ArrayList<>(); //存放的数据 String[] menuStr1 = new String[]{"全部", "粮油", "衣服", "图书", "电子产品", "酒水饮料", "水果"}; //创建小盒子,放标号和值 Map<String, String> map1; for (int x = 0; x < menuStr1.length; x++) { map1 = new HashMap<String, String>(); map1.put("name", menuStr1[x]); mMenuDatal.add(map1); } //创建加载布局的容器 mMenuData2 = new ArrayList<>(); String[] menuStr2 = new String[]{"综合排序", "配送费最低"}; //创建小盒子,放标号和值 Map<String, String> map2; for (int x = 0; x < menuStr2.length; x++) { map2 = new HashMap<String, String>(); map2.put("name", menuStr2[x]); mMenuData2.add(map2); } //创建加载布局的容器 mMenuData3 = new ArrayList<>(); String[] menuStr3 = new String[]{"优惠活动", "特价活动", "免配送费", "可在线支付"}; //创建小盒子,放标号和值 Map<String, String> map3; for (int x = 0; x < menuStr3.length; x++) { map3 = new HashMap<String, String>(); map3.put("name", menuStr3[x]); mMenuData3.add(map3); } } //设置一个标记 private int menuIndex = 0; @OnClick({R.id.supplier_list_product, R.id.supplier_list_sort, R.id.supplier_list_activity}) public void onClick(View view) { switch (view.getId()) { //第一个PopupWindow点击后的逻辑 case R.id.supplier_list_product: //设置点击变色 mProductTv.setTextColor(Color.parseColor("#39ac69")); //设置PopupWindow里的适配器 mPopListview.setAdapter(mMenuAdapter1); //让PopupWindow显示出来 view对象,决定了PopupWindow在那个控件下显示,坐标,xy mPopWindow.showAsDropDown(mProduct,0,2); menuIndex = 0; break; case R.id.supplier_list_sort: //设置点击变色 mSortTv.setTextColor(Color.parseColor("#39ac69")); //设置PopupWindow里的适配器 mPopListview.setAdapter(mMenuAdapter2); //让PopupWindow显示出来 view对象,决定了PopupWindow在那个控件下显示,坐标,xy mPopWindow.showAsDropDown(mSort,0,2); menuIndex = 1; break; case R.id.supplier_list_activity: //设置点击变色 mActivityTv.setTextColor(Color.parseColor("#39ac69")); //设置PopupWindow里的适配器 mPopListview.setAdapter(mMenuAdapter3); //让PopupWindow显示出来 view对象,决定了PopupWindow在那个控件下显示,坐标,xy mPopWindow.showAsDropDown(mSupplierListLv,2,2); menuIndex = 2; break; } } }
每一条listview的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" > <TextView android:id="@+id/listview_popwind_tv" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:gravity="center_vertical" android:text="地点" android:textColor="#5a5959" android:textSize="18dp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:background="#E2E2E2" /> </LinearLayout>
下拉出来的listview布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/popwin_supplier_list_lv" android:layout_width="wrap_content" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:divider="#0000" android:dividerHeight="0dp" android:fadingEdge="none" android:listSelector="#00000000" android:scrollbarStyle="outsideOverlay" android:scrollbars="none" android:scrollingCache="false"></ListView> <LinearLayout android:id="@+id/popwin_supplier_list_bottom" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical"></LinearLayout> </LinearLayout>