popuwindow升级版的更高层次

来源:互联网 发布:python布尔值 编辑:程序博客网 时间:2024/06/12 20:26

今天我在这里为大家阐述一下关于PopuWindow的更高一级的层次,话不多说,开撸 :

思路总成 :** * popuwindow 下拉列表属于组合式控件 * <>:环境搭建: * 1.添加ButterKnife 的依赖,取消掉ActionBar,使用ToBar代替(compile 'com.jakewharton:butterknife:7.0.1') * 2.完成整体数据,初始化控件,设置点击事件 * 3.初始化popuwindow索要显示的数据 * 4.初始化popuwindow控件的设置, * 5.popwwindowlistivew相关联 * 6.三个popuwindow所要依附的linearLayout,加点击事件,做对应的逻辑判断(改变TextView,显示效果) *  * */MainActivity 中代码:
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>> mMuData1;    private ArrayList<Map<String, String>> mMuData2;    private ArrayList<Map<String, String>> mMuData3;    private PopupWindow mPopupWindow;    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);        // 初始化Popuwindow所要显示的数据        initData();        // 初始化Popuwindow        initpoMenu();    }    // 初始化Popuwindow    private void initpoMenu() {        // 把包裹ListView布局的XML文件转换为View对象        View popview = LayoutInflater.from(this).inflate(R.layout.popwin_supplier_list, null);        // 创建popuwindow对象,参数1:popuwindow要显示的布局,参数2/3:定义popuwindow的宽和高        mPopupWindow = new PopupWindow(popview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);        // 设置popuwindow外部可以点击        mPopupWindow.setOutsideTouchable(true);        // 设置popuwindow里面的listview有焦点        mPopupWindow.setFocusable(true);        // 如果想让popuwindow有动画效果,就必须有这段代码;        mPopupWindow.setBackgroundDrawable(new ColorDrawable());        // 这只监听事件        mPopupWindow.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"));            }        });        // 设置点击popuwindow以外的地方,使popwwindow消失        LinearLayout list_bottem = (LinearLayout) popview.findViewById(R.id.popwin_supplier_list_bottom);        list_bottem.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                // 当点击到popuwindow以外的地方时,popuwindow消失                mPopupWindow.dismiss();            }        });        // 获取 Listview 的对象        mPopListview = (ListView) popview.findViewById(R.id.popwin_supplier_list_lv);        // 创建SimpleAdapter,一个ListView安卓原生的适配器        mMenuAdapter1 = new SimpleAdapter(this, mMuData1, R.layout.item_listview_popwin, new String[]{"name"}, new int[]{R.id.listview_popwind_tv});        mMenuAdapter2 = new SimpleAdapter(this, mMuData2, R.layout.item_listview_popwin, new String[]{"name"}, new int[]{R.id.listview_popwind_tv});        mMenuAdApter3 = new SimpleAdapter(this, mMuData3, R.layout.item_listview_popwin, new String[]{"name"}, new int[]{R.id.listview_popwind_tv});        // 设置PopupWindow里的Listview点击事件时,当点击Listview里的Item,把这个item数据显示到最上方.        mPopListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                // 首先 让PopupWindow消失                mPopupWindow.dismiss();                switch (menuIndex){                    case 0:                        String currentProduct1 = mMuData1.get(i).get("name");                        mProductTv.setText(currentProduct1);                        break;                    case 1:                        String currentProduct2 = mMuData2.get(i).get("name");                        mSortTv.setText(currentProduct2);                        break;                    case 2:                        String currentProduct3 = mMuData3.get(i).get("name");                        mActivityTv.setText(currentProduct3);                        break;                }            }        });    }    // 初始胡数据,popuwindow所需,一共有三个,所以我要封装三个数据,这里是假数据,没有做网络请求,真实数据从网络获取    private void initData() {        // 创建第一个创建popuwindow加载数据的大盒子,Map集合(,);        mMuData1 = new ArrayList<>();        // 存放String的字符串数组;        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]);            mMuData1.add(map1);        }        String name = mMuData1.get(0).get("name");        System.out.println("name111"+name);        // 创建第二个创建popuwindow加载数据的大盒子,Map集合(,);        mMuData2 = new ArrayList<>();        // 存放String的字符串数组;        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]);            mMuData2.add(map2);        }        String name2 = mMuData2.get(0).get("name");        System.out.println("name000"+name);        // 创建第三个创建popuwindow加载数据的大盒子,Map集合(,);        mMuData3 = new ArrayList<>();        // 存放String的字符串数组;        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]);            mMuData3.add(map3);        }        String name3 = mMuData1.get(0).get("name");        System.out.println("name333"+name);    }    // 设置一个标记,方便对点击不同LinearLayouut做对应操作    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()) {            // 第一个PopuWindow所执行点击后的逻辑            case R.id.supplier_list_product:                // 设置其TextView点击时绿色                mProductTv.setTextColor(Color.parseColor("#39ac69"));                // 设置PopuWindow里的适配器                mPopListview.setAdapter(mMenuAdapter1);                // PopuWindow显示出来                // 参数1:View对象,决定了PopuWindow在哪个控件下显示, 参数2/3决定PopuWindow的坐标,X,Y                mPopupWindow.showAsDropDown(mProduct,0,2);                menuIndex = 0;                break;            case R.id.supplier_list_sort:                // 设置其TextView点击时绿色                mSortTv.setTextColor(Color.parseColor("#39ac69"));                // 设置PopuWindow里的适配器                mPopListview.setAdapter(mMenuAdapter2);                // PopuWindow显示出来                // 参数1:View对象,决定了PopuWindow在哪个控件下显示, 参数2/3决定PopuWindow的坐标,X,Y                mPopupWindow.showAsDropDown(mProduct,0,2);                menuIndex = 1;                break;            case R.id.supplier_list_activity:                // 设置其TextView点击时绿色                mActivityTv.setTextColor(Color.parseColor("#39ac69"));                // 设置PopuWindow里的适配器                mPopListview.setAdapter(mMenuAdApter3);                // PopuWindow显示出来                // 参数1:View对象,决定了PopuWindow在哪个控件下显示, 参数2/3决定PopuWindow的坐标,X,Y                mPopupWindow.showAsDropDown(mProduct,0,2);                menuIndex = 2;                break;        }    }}


activity_main :



<?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">   <android.support.v7.widget.Toolbar       android:background="#28f"       android:layout_width="match_parent"       android:layout_height="45dp">       <TextView           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="周边"           android:textColor="@android:color/holo_green_dark"           android:layout_gravity="center"           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:orientation="horizontal"        >        <LinearLayout            android:id="@+id/supplier_list_product"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:orientation="horizontal"            android:gravity="center"            >            <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_weight="1"            android:layout_height="match_parent"            android:orientation="horizontal"            android:gravity="center"            >            <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_weight="1"            android:layout_height="match_parent"            android:orientation="horizontal"            android:gravity="center"            >            <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"        />    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <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>    </RelativeLayout>    易总监(851483652)  11:11:00    <!--从这里开始,就是自定义多条件筛选菜单控件的布局核心所在-->    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#E2E2E2"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="40dp"        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="1dp"        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>

item_listview_popwin :

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent">    <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="#28f" /></LinearLayout>


popwin_supplier_list :


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent"><ListView    android:id="@+id/popwin_supplier_list_lv"    android:layout_width="match_parent"    android:layout_height="wrap_content"    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_weight="1"    android:layout_height="0dp"    android:orientation="vertical"    ></LinearLayout></LinearLayout>

撸到这里就好了
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 租房合同没有到期违约了怎么办 租房户到期不搬怎么办 公租房摇号摇到了又怎么办 公租房被清退会怎么办 公租房摇不到号怎么办 公租房到期不搬怎么办 租房到期租客不搬怎么办 房产证面积与实际不符怎么办 社保晚交了1天怎么办 个人社保忘交了怎么办 个人社保晚交了怎么办 医保晚交了几天怎么办 辞职后转为灵活就业养老怎么办 公司名称变更提取不了公积金怎么办 五险合一软件已经减员怎么办 法人社保不在投标单位怎么办 换工作单位后社保怎么办 在北京孩子没有一老一小怎么办 深户小孩怎么办社保卡 社保卡没办下来去医院住院怎么办 老年社保卡丢了怎么办 外墙掉瓷砖伤车伤人怎么办 医保卡姓名弄错了怎么办 走工伤和走社保怎么办 公司在朝阳社保在海淀怎么办 公司没缴纳个税怎么办 报个税工资报少了怎么办 医院预约单丢了怎么办 肛瘘手术后太疼怎么办 低位保肛手术后吻合口瘘怎么办 做完痔疮手术后大便困难怎么办 20岁长痔疮了怎么办 孕妇痔疮痒的难受怎么办 痔疮术后伤口不愈合怎么办 剖腹产液化伤口长的慢怎么办 内痔斑痕怎么办了能消化 油条面和稀了怎么办 解脲支原体感染怀孕怎么办 怀孕了检查出解脲支原体感染怎么办 大便是黑色的要怎么办 大人直肠给药不好意思怎么办