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.popwwindow与listivew相关联 * 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>
撸到这里就好了
阅读全文
0 0
- popuwindow升级版的更高层次
- 打破牢笼,展望更高层次的世界
- 大连寻求在更高层次上的对日合作
- 类似Popuwindow的实现
- PopuWindow的使用
- popuwindow的使用方法
- popuwindow的动画实现
- PopuWindow
- popuwindow
- popuwindow
- PopuWindow
- android的popuwindow的使用
- 关于PopuWindow的弹出方式
- 升级你的APP,让你的旗舰设备支持更高的长高比
- 升级你的APP,让你的旗舰设备支持更高的长宽比
- 59.56.97.66全新升级面对更好的你,更高防御更好的保护你
- 主板支持更高的处理器速度,升级处理器可获得性能提升。
- Ubuntu 10.04升级git 到1.7.2或更高的可行方法
- Linux下安装MySQL和卸载
- spirng整合redis (二)
- POJ 1177 线段树+扫描线
- 查询当前数据库的状态
- Overview of TensorFlow
- popuwindow升级版的更高层次
- 通过js实现单击或双击直接修改内容
- UOJ 手机的生产
- 中兴新支点上的终端与shell
- 以太坊智能合约安全监测工具 Oyente
- 成功解决vm虚拟机装上redhat…
- HDU 6127-Hard challenge(计算几何)
- 现代操作系统面试总结
- 栈应用之任意进制转换