popuwindow的使用方法

来源:互联网 发布:社会化新媒体矩阵 编辑:程序博客网 时间:2024/05/17 03:24
popuwindow的使用方法
//popwindow下拉列表,属于组合式控件
/*
1.首先添加butterknife的依赖,取消掉ActionBar,使用toolbar代替
2.完成整体的布局,初始化控件,设置点击事件
3.初始化popwindow所要显示的数据
4.初始化popwindow控件的设置
5.popwindow与listview相关联
6.三个popwindow所依附的linearlayout,根据点击事件,做对应逻辑处理(改变textview的颜色,显示效果)
*/
MainActivity主方法
public classMainActivityextendsAppCompatActivity {

@Bind(R.id.supplier_list_product_tv)
TextViewmProductTv;// 可以修改名称
@Bind(R.id.supplier_list_product)
LinearLayoutmProduct;
@Bind(R.id.supplier_list_sort_tv)
TextViewmSortTv;// 可以修改名称
@Bind(R.id.supplier_list_sort)
LinearLayoutmSort;
@Bind(R.id.supplier_list_activity_tv)
TextViewmActivityTv;// 可以修改名称
@Bind(R.id.supplier_list_activity)
LinearLayoutmActivity;
@Bind(R.id.supplier_list_lv)
ListViewmSupplierListLv;
privateArrayList<Map<String, String>>menuData1;
privateArrayList<Map<String, String>>menuData2;
privateArrayList<Map<String, String>>menuData3;
privatePopupWindowmPopMenu;
privateListView mpoplistview;
privateSimpleAdaptermMenuAdapter1;
privateSimpleAdaptermMenuAdapter2;
privateSimpleAdaptermMenuAdapter3;


@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//初始化popwindow所要显示的数据
initData();
//初始化popwindow控件
initPopMenu();
}
//初始化popwindow
private voidinitPopMenu() {
//把包裹listview布局的xml文件转换为view对象
View popview = LayoutInflater.from(this).inflate(R.layout.popwin_list,null);
//创建popwindow对象,参数1 popwindow要显示的布局 参数2 3 定义popwindow宽和高
mPopMenu= newPopupWindow(popview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//设置popwindow外部可以点击
mPopMenu.setOutsideTouchable(true);
//设置popwindow里面的listview有焦点
mPopMenu.setFocusable(true);
//如果想让popwindow有动画效果,就必须有这行代码
mPopMenu.setBackgroundDrawable(newColorDrawable());
//设置popwindow结束时的监听
mPopMenu.setOnDismissListener(newPopupWindow.OnDismissListener() {
@Override
public voidonDismiss() {
//设置TextView的颜色,把所有LinearLayout的文本颜色该为灰色
mProductTv.setTextColor(Color.parseColor("#5a5959"));
mSortTv.setTextColor(Color.parseColor("#5a5959"));
mActivityTv.setTextColor(Color.parseColor("#5a5959"));
}
});
//设置点击popwindow以外的地方,使popwindow消失
LinearLayout list_bottom = (LinearLayout) popview.findViewById(R.id.popwin_supplier_list_bottom);
list_bottom.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View view) {
//当点击到灰色区域时 popwindow消失
mPopMenu.dismiss();
}
});
//获取listview对象
mpoplistview= (ListView) popview.findViewById(R.id.popwin_supplier_list_lv);
//创建simpleadapter,一个listview安卓原生封装的适配器
mMenuAdapter1= newSimpleAdapter(this,menuData1, R.layout.item_listview_popwindow,newString[]{"name"},new int[]{R.id.listview_popwind_tv});
mMenuAdapter2= newSimpleAdapter(this,menuData2, R.layout.item_listview_popwindow,newString[]{"name"},new int[]{R.id.listview_popwind_tv});
mMenuAdapter3= newSimpleAdapter(this,menuData3, R.layout.item_listview_popwindow,newString[]{"name"},new int[]{R.id.listview_popwind_tv});
//设置popwindow里的listview点击事件,当点击listview里的一个item时,把这个item数据显示到最上方
mpoplistview.setOnItemClickListener(newAdapterView.OnItemClickListener() {
@Override
public voidonItemClick(AdapterView<?> adapterView, View view,inti, longl) {
//首先让popwindow消失
mPopMenu.dismiss();
switch(menuIndex){
case0:
String currentProduct =menuData1.get(i).get("name");
mProductTv.setText(currentProduct);
break;
case1:
String currentsort =menuData2.get(i).get("name");
mSortTv.setText(currentsort);
break;
case2:
String currentActivity =menuData3.get(i).get("name");
mActivityTv.setText(currentActivity);
break;
}

}
});
}
//设置一个标记,方便对点击不同linearlayout做相应操作
private intmenuIndex=0;
//初始化数据,popwindow所需,一共有三个,所以我要封装好三个数据,这里是假数据,真实数据从网上获取
private voidinitData() {
//创建一个存放popwindow加载数据的大盒子,map集合(键,值)
menuData1= newArrayList<>();
//存放String的字符串数组
String[] menuStr1 =newString[]{"全部","粮油","衣服","图书","电子产品",
"酒水饮料","水果"};
//创建一个小盒子,放编号和值
Map<String,String> map1;
for(intx=0;x<menuStr1.length;x++){
map1=newHashMap<String, String>();
map1.put("name",menuStr1[x]);
menuData1.add(map1);
}
//创建一个存放popwindow加载数据的大盒子,map集合(键,值)
menuData2= newArrayList<>();
//存放String的字符串数组
String[] menuStr2 =newString[]{"综合排序","配送费最低"};
//创建一个小盒子,放编号和值
Map<String,String> map2;
for(intx=0;x<menuStr2.length;x++){
map2=newHashMap<String, String>();
map2.put("name",menuStr2[x]);
menuData2.add(map2);
}
//创建一个存放popwindow加载数据的大盒子,map集合(键,值)
menuData3= newArrayList<>();
//存放String的字符串数组
String[] menuStr3 =newString[]{"优惠活动","特价活动","免配送费",
"可在线支付"};
//创建一个小盒子,放编号和值
Map<String,String> map3;
for(intx=0;x<menuStr3.length;x++){
map3=newHashMap<String, String>();
map3.put("name",menuStr3[x]);
menuData3.add(map3);
}
//测试是否有问题
String name =menuData3.get(0).get("name");
System.out.println("name----"+name);
}

@OnClick({R.id.supplier_list_product, R.id.supplier_list_sort, R.id.supplier_list_activity})
public voidonClick(View view) {
switch(view.getId()) {
//第一个popwindow所执行的点击后的逻辑
caseR.id.supplier_list_product:
//设置其textview点击是红色
mProductTv.setTextColor(Color.RED);
//设置popwindow里的lisyview适配器
mpoplistview.setAdapter(mMenuAdapter1);
//让popwindow显示出来 参数1.view对象 决定了popwindow在哪个控件下显示
//参数2 3 决定了popwindow的坐标 x轴 y轴
mPopMenu.showAsDropDown(mProduct,0,2);
menuIndex=0;
break;
caseR.id.supplier_list_sort:
mSortTv.setTextColor(Color.RED);
mpoplistview.setAdapter(mMenuAdapter2);
mPopMenu.showAsDropDown(mProduct,0,2);
menuIndex=1;
break;
caseR.id.supplier_list_activity:
mActivityTv.setTextColor(Color.RED);
mpoplistview.setAdapter(mMenuAdapter3);
mPopMenu.showAsDropDown(mProduct,0,2);
menuIndex=2;
break;
}
}

}
activity_main布局
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="45dp">
<TextView
android:textSize="20sp"
android:text="周边"
android:layout_gravity="center_horizontal"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
<!--从这里开始,就是自定义多条件筛选菜单控件的核心所在-->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#E2E2E2"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/white"
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/t1"/>
</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/t1"/>
</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/t1"/>
</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_popwindow布局
<?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>
popwin_list
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="match_parent"
android:background="#5000"
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>
原创粉丝点击