ToolBar点击实现PopuWindow窗口效果

<LinearLayout 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"
    tools:context="com.example.zidingyi.MainActivity"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#ffff4c06">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="周边"
            android:gravity="center"
            android:textColor="#ffffff"
            android:textSize="20sp" />
    </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/ic_ic"/>
        </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/ic_ic"/>
        </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/ic_ic"/>
        </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:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>


</LinearLayout>
///
/**
* popuwidow下拉列表,属于组合式控件
* Butterknife依赖 :compile 'com.jakewharton:butterknife:7.0.1'依赖
* 去掉ActionBar,使用ToolBar代替.去掉ActionBar只需要在AndroidManifest android :theme= "@style/Theme.AppCompat.Light.NoActionBar"换成这样既可
* 完成整体布局,初始化控件,设置点击监听
* 初始化popuwindow索要显示数据
* 初始化popuwindow控件设置
* popuwindow与listview相关联
*/
public class MainActivity extends AppCompatActivity {
//调取控件id
@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;
//三个容器分别用来装三个ToolBar下listview的数据
private ArrayList<Map<String,String>> mMenuData;
private ArrayList<Map<String,String>> mMenuData2;
private ArrayList<Map<String,String>> mMenuData3;
private View popview;
private PopupWindow popupWindow;
private View list_bottom;
private ListView mpoplistview;
//三个listview适配器
private SimpleAdapter simpleAdapter;
private SimpleAdapter simpleAdapter2;
private SimpleAdapter simpleAdapter3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//初始化popuwindow索要显示数据
initData();
initPopMenu();
}
//初始化数据,popuwindow所需,一共三个,所要我要封装好三个数据,这里假数据真实数据从网上获得
private void initData() {
//创建一个popuwindow加载数据的盒子,map集合
mMenuData = new ArrayList<>();
//存放string字符串数组
String[] menuStr1 = new String[]{"全部", "粮油", "衣服", "图书", "电子产品",
"酒水饮料", "水果"};
//创建一个小盒子,放编号之
Map<String,String> map1;
for (int i = 0; i <menuStr1.length ; i++) {
map1 = new HashMap<>();
map1.put("name",menuStr1[i]);
mMenuData.add(map1);
}
mMenuData2 = new ArrayList<>();
//存放string字符串数组
String[] menuStr2 = new String[]{"综合排序", "配送费最低"};
//创建一个小盒子,放编号之
Map<String,String> map2;
for (int i = 0; i <menuStr2.length ; i++) {
map2 = new HashMap<>();
map2.put("name",menuStr2[i]);
mMenuData2.add(map2);
}
mMenuData3 = new ArrayList<>();
//存放string字符串数组
String[] menuStr3 = new String[]{"优惠活动", "特价活动", "免配送费",
"可在线支付"};
//创建一个小盒子,放编号之
Map<String,String> map3;
for (int i = 0; i <menuStr3.length ; i++) {
map3 = new HashMap<>();
map3.put("name",menuStr3[i]);
mMenuData3.add(map3);
}
}
private void initPopMenu(){
//把包裹listview布局的Xml文件转换为view对象
popview = LayoutInflater.from(this).inflate(R.layout.popwin_list, null);
//参数1popuwindow要显示布局,参数2,3宽和高
popupWindow = new PopupWindow(popview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//设置popuwindow外部可以点击
popupWindow.setOutsideTouchable(true);
//设置popuwindow里面的listview有焦点
popupWindow.setFocusable(true);
//如果想让popuwindow有动画效果,必须有以下代码
popupWindow.setBackgroundDrawable(new ColorDrawable());
//设置结束监听
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
mProductTv.setTextColor(Color.parseColor("#5a5959"));
mSortTv.setTextColor(Color.parseColor("#5a5959"));
mActivityTv.setTextColor(Color.parseColor("#5a5959"));
}
});
//设置点击popuwindow以外地方,使popuwindow消失
list_bottom = popview.findViewById(R.id.popwin_supplier_list_bottom);
list_bottom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//当点击到灰色时popuwindow消失
popupWindow.dismiss();
}
});
//获取listview对象
mpoplistview = (ListView)popview.findViewById(R.id.popwin_supplier_list_lv);
//創建simpleAdapter,一個listview安卓原生封裝適配器布局可以用系统的也可用自己的
simpleAdapter = new SimpleAdapter(this, mMenuData, R.layout.item_listview, new String[]{"name"}, new int[]{R.id.listview_popwind_tv});
simpleAdapter2 = new SimpleAdapter(this, mMenuData2, R.layout.item_listview, new String[]{"name"}, new int[]{R.id.listview_popwind_tv});
simpleAdapter3 = new SimpleAdapter(this, mMenuData3, R.layout.item_listview, new String[]{"name"}, new int[]{R.id.listview_popwind_tv});
//設置popuwindow里的listview點擊事件,當點擊listview里的一個item時,把這個item數據顯示到最上方
mpoplistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
popupWindow.dismiss();
switch (menuindex){
case 0:
String currentproduct = mMenuData.get(i).get("name");
mProductTv.setText(currentproduct);
break;
case 1:
String currentproduct2 = mMenuData2.get(i).get("name");
mSortTv.setText(currentproduct2);
break;
case 2:
String currentproduct3 = mMenuData3.get(i).get("name");
mActivityTv.setText(currentproduct3);
break;
}
//首先讓popuwindow消失
// popupWindow.dismiss();
// String currentproduct = mMenuData.get(i).get("name");
// mProductTv.setText(currentproduct);
}
});
}
//設置一個標記,方便對點擊不同的linerlayout做判斷
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里的listview適配器
mpoplistview.setAdapter(simpleAdapter);
//讓popuwindow顯示出來 參數1.view對象決定popuwindow在那個空間下顯示
//參數2,3決定popuwindow的坐標x軸y軸
popupWindow.showAsDropDown(mProduct,0,2);
menuindex=0;
break;
//
case R.id.supplier_list_sort:
//設置其TextView點擊是綠色
mSortTv.setTextColor(Color.parseColor("#39ac69"));
//設置popuwindow里的listview適配器
mpoplistview.setAdapter(simpleAdapter2);
//讓popuwindow顯示出來 參數1.view對象決定popuwindow在那個空間下顯示
//參數2,3決定popuwindow的坐標x軸y軸
popupWindow.showAsDropDown(mProduct,0,2);
menuindex=1;
break;
case R.id.supplier_list_activity:
//設置其TextView點擊是綠色
mActivityTv.setTextColor(Color.parseColor("#39ac69"));
//設置popuwindow里的listview適配器
mpoplistview.setAdapter(simpleAdapter3);
//讓popuwindow顯示出來 參數1.view對象決定popuwindow在那個空間下顯示
//參數2,3決定popuwindow的坐標x軸y軸
popupWindow.showAsDropDown(mProduct,0,2);
menuindex=2;
break;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值