今天在使用PopUpWindow 做一个侧滑的菜单
xml 如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/topliner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="5dip" >
<Button
android:id="@+id/dj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击"
/>
</LinearLayout>
</LinearLayout>
style.xml
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/dync_out_to_left</item>
<item name="android:windowExitAnimation">@anim/dync_in_from_right</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="-100%p"
android:toXDelta="0%p" />
anim/dync_in_from_right.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="0%p"
android:toXDelta="-100%p" />
java 代码如下
package com.sime.pop;
import android.app.Activity;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.sime.volley.R;
public class PopAct extends Activity {
Button btn;
private PopupWindow mPopupWindow;
private Boolean isExtend = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_pop);
btn = (Button) findViewById(R.id.dj);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!isExtend) {
isExtend = true;
getPopupWindowInstance();
mPopupWindow.showAsDropDown(
PopAct.this.findViewById(R.id.topliner), 0, 0);
} else {
mPopupWindow.showAsDropDown(
PopAct.this.findViewById(R.id.topliner), 0, 0);
isExtend = false;
}
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mPopupWindow != null && mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
mPopupWindow = null;
isExtend = false;
}
return super.onTouchEvent(event);
}
private void initPopuptWindow() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View popupWindow = layoutInflater.inflate(R.layout.pop_item, null);
ListView lisview = (ListView) popupWindow.findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1);
adapter.add("栏目1");
adapter.add("栏目2");
adapter.add("栏目3");
lisview.setAdapter(adapter);
lisview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long arg3) {
Toast.makeText(PopAct.this, "点击事件", 2000).show();
}
});
mPopupWindow = new PopupWindow(popupWindow, LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new PaintDrawable());
mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
}
/*
* 获取PopupWindow实例
*/
private void getPopupWindowInstance() {
if (null != mPopupWindow) {
mPopupWindow.dismiss();
return;
} else {
initPopuptWindow();
}
}
}
mPopupWindow.setFocusable(true);
如果没有这一句我无法监听到,popupWindow布局中的listview 无法监听其事件。
加上了之后,点击PopWindow之外的区域,不能dismiss(),点击按钮也不行。百度了一下解决方案加上了
mPopupWindow.setBackgroundDrawable(new PaintDrawable());