MainActivity.java
package tech.androidstudio.threelevelpopupwindowsimpledemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView mClickMe;
private ArrayList<Item> mItemsList;
private int firstItemPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClickMe = (TextView)findViewById(R.id.main_clickMe);
mClickMe.setOnClickListener(this);
mItemsList = new ArrayList<Item>();
ArrayList<String> list_HangZhou = new ArrayList<>();
list_HangZhou.add("哈里哈里配送中心");
list_HangZhou.add("冷冻配送中心");
list_HangZhou.add("饮料配送中心");
Item item_HangZhou = new Item("杭州市场",list_HangZhou);
mItemsList.add(item_HangZhou);
ArrayList<String> list_XiaoShan = new ArrayList<>();
list_XiaoShan.add("萧山配送中心");
list_XiaoShan.add("萧山冷冻配送中心");
list_XiaoShan.add("萧山配送中心");
Item item_XiaoShan = new Item("萧山滨江站市场",list_XiaoShan);
mItemsList.add(item_XiaoShan);
}
/**处理点击的事件,弹出第一级的popupWindow*/
@Override
public void onClick(View v) {
PopupWindow firstPopupWindow = new PopupWindow(this);
ListView firstListView = new ListView(this);
firstListView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//获取第一级别的Item的名字的列表
final ArrayList<String> firstItemNameList = new ArrayList<String>();
for (int i = 0; i < mItemsList.size(); i++) {
firstItemNameList.add(mItemsList.get(i).getName());
}
ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(this,R.layout.item_layout,firstItemNameList);
firstListView.setAdapter(firstAdapter);
//设置fristListView 到popupWindow里面
firstPopupWindow.setContentView(firstListView);
firstPopupWindow.setOutsideTouchable(true);
firstPopupWindow.setTouchable(true);
firstPopupWindow.setFocusable(true);
//一定要设置这个宽和高,不然没有宽度和高度就显示不出来的
firstPopupWindow.setHeight(v.getHeight()*mItemsList.size());
firstPopupWindow.setWidth(v.getWidth());
//获取点击的控件的在屏幕中的位置
int[] location = new int[2];
v.getLocationOnScreen(location);
firstPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] +v.getWidth(),location[1]);
//给这个firstListView 添加点击事件,弹出二级菜单
firstListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
firstItemPosition =position;
PopupWindow secondPopupWindow = new PopupWindow(MainActivity.this);//注意这里是用的MainActivity.this
ListView secondListView = new ListView(MainActivity.this);
secondListView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
final ArrayList<String> secondItemList = mItemsList.get(position).getSubItemList();
ArrayAdapter<String> secondAdapter = new ArrayAdapter<String>(MainActivity.this,R.layout.item_layout,secondItemList);
secondListView.setAdapter(secondAdapter);
secondPopupWindow.setContentView(secondListView);
secondPopupWindow.setFocusable(true);
secondPopupWindow.setTouchable(true);
secondPopupWindow.setOutsideTouchable(true);
secondPopupWindow.setHeight(view.getHeight() * secondItemList.size());
secondPopupWindow.setWidth(view.getWidth());
int[] secondLocation = new int[2];
view.getLocationOnScreen(secondLocation);
//注意这里的不要使用view,而要使用mClickMe,不然会如下的报错
//android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@41947150 is not valid; is your activity running?
secondPopupWindow.showAtLocation(mClickMe,Gravity.NO_GRAVITY,secondLocation[0]+view.getWidth(),secondLocation[1]);
//最后给secondListView 添加一个点击事件的
secondListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String secondItemName = secondItemList.get(position);
String firstItemName = firstItemNameList.get(firstItemPosition);
String toastText = "Clicked 第一级:" + firstItemName + " 第二级别:" + secondItemName;
Toast.makeText(MainActivity.this, toastText,Toast.LENGTH_SHORT).show();
Log.d("kodulf",toastText);
}
});
}
});
}
//为了实现二级菜单需要有一个类Item,里面有一个list是subItem
public class Item {
private String name;
private ArrayList<String> subItemList;
public Item(String name, ArrayList<String> subItemList) {
this.name = name;
this.subItemList = subItemList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getSubItemList() {
return subItemList;
}
public void setSubItemList(ArrayList<String> subItemList) {
this.subItemList = subItemList;
}
@Override
public String toString() {
return "Item{" +
"name='" + name + '\'' +
", subItemList=" + subItemList +
'}';
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<TextView
android:id="@+id/main_clickMe"
android:text="切换市场"
android:gravity="center"
android:background="@android:color/holo_blue_light"
android:layout_width="100dp"
android:layout_height="50dp"
android:textSize="12sp"
android:clickable="true"/>
</RelativeLayout>
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/text_item"
android:textSize="12sp"
android:gravity="center"
android:background="@android:color/holo_green_light">
</TextView>