package com.example.testtwolistview;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
@SuppressLint("NewApi")
public class MainActivity extends Activity implements
OnClickListener, OnItemClickListener {
private ListView lv_category_item;
private ListView lv_category_subitem;
private List items;
private List subitems;
private CategoryAdapter itemCategoryAdapter;
private CategoryAdapter subitemCategoryAdapter;
private Button btn_category_click;
private String TAG="MainActivity";
private View mainView;
private int itemNum=0;//类目代号,默认为0
private int subitemNum=0;//子类目代号,默认为0
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainView=LayoutInflater.from(this).inflate(R.layout.activity_main,
null);
setContentView(R.layout.activity_main);
btn_category_click=(Button)
findViewById(R.id.btn_category_click);
btn_category_click.setOnClickListener(this);
items=new ArrayList();
items.add("All");
items.add("Apple");
items.add("Banana");
items.add("Watermelon");
items.add("Grape");
items.add("Strawberry");
subitems=new ArrayList();
subitems.add("All");
subitems.add("Friends");
subitems.add("Classmate");
subitems.add("Relative");
subitems.add("Colleague");
subitems.add("Other");
itemCategoryAdapter=new CategoryAdapter(MainActivity.this,
items,0);
Log.e(TAG, "itemCategoryAdapter");
subitemCategoryAdapter=new CategoryAdapter(MainActivity.this,
subitems,1);
Log.e(TAG, "subitemCategoryAdapter");
}
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_category_click:
LayoutInflater inflater=(LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.popupwindow, null);
lv_category_item=(ListView)
view.findViewById(R.id.lv_category_item);
lv_category_subitem=(ListView)
view.findViewById(R.id.lv_category_subitem);
lv_category_item.setCacheColorHint(Color.TRANSPARENT);//删除滑动背景
lv_category_subitem.setCacheColorHint(Color.TRANSPARENT);
//设置适配器
lv_category_item.setAdapter(itemCategoryAdapter);
lv_category_subitem.setAdapter(subitemCategoryAdapter);
//设置tag,方便在onItemClick()方法中判断点击了哪个listview
lv_category_item.setTag("0");
lv_category_subitem.setTag("1");
//popupwindow的尺寸
int
popWidth=(getWindowManager().getDefaultDisplay().getWidth()/5)*4;
int
popHeight=(getWindowManager().getDefaultDisplay().getHeight()/5)*4;
//popupwindow距离顶部的距离
int height=getStatusBarAndTitleBarHeight();
popupWindow = new PopupWindow(view, popWidth,
popHeight);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);//使popupwindow能响应点击等事件
popupWindow.showAtLocation(mainView, Gravity.NO_GRAVITY, 0,
height);
lv_category_item.setOnItemClickListener(this);
lv_category_subitem.setOnItemClickListener(this);
break;
}
}
@SuppressLint("NewApi")
private int getStatusBarAndTitleBarHeight(){
//获取actionbar(标题栏)的高度
int actionbarHeight=getActionBar().getHeight();
//获取状态栏(通知栏)高度
Rect frame = new Rect();
Window win = MainActivity.this.getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
return (actionbarHeight+statusBarHeight);
}
@Override
public void onItemClick(AdapterView