PopUpWindow 侧滑菜单

今天在使用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>


anim/dync_out_to_left.xml

<?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());



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值