Android PopupWindow实现从顶部弹出下拉菜单左、中、右

          本实例的自定义下拉菜单主要是继承PopupWindow类来实现的弹出窗体,各种布局效果可以根据自己定义设计。弹出的动画效果主要用到了translate、alpha、scale,具体实现步骤如下:

         先上效果图如下:左边下拉菜单、中间下拉菜单、右边下拉菜单

     Android PopupWindow实现从顶部弹出下拉菜单左、中、右_第1张图片     Android PopupWindow实现从顶部弹出下拉菜单左、中、右_第2张图片       Android PopupWindow实现从顶部弹出下拉菜单左、中、右_第3张图片

1.主界面布局 activity_main.xml:



    

    

    

        

        

        
    

    

    


2.主界面测试类 MainActivity.java

package com.popuptest;

import java.util.ArrayList;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener {

	public static int screenW, screenH;

	private ImageButton backBtn, createBtn;
	private Button confirmBtn;
	private TextView topTv;
	private LinearLayout topll;
	private ImageView topIv;
	private TextView topLineTv;

	private TopMiddlePopup middlePopup;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		getScreenPixels();
		initWidget();
	}

	/**
	 * 初始化控件
	 */
	private void initWidget() {
		backBtn = (ImageButton) findViewById(R.id.urm_back_btn);
		createBtn = (ImageButton) findViewById(R.id.urm_create_btn);
		confirmBtn = (Button) findViewById(R.id.urm_confirm_btn);

		topll = (LinearLayout) findViewById(R.id.urm_top_ll);
		topIv = (ImageView) findViewById(R.id.urm_top_iv);

		topLineTv = (TextView) findViewById(R.id.rule_line_tv);

		topTv = (TextView) findViewById(R.id.urm_top_tv);
		topTv.setText("企业客户");

		backBtn.setOnClickListener(this);
		createBtn.setOnClickListener(this);
		confirmBtn.setOnClickListener(this);
		topll.setOnClickListener(this);

	}

	/**
	 * 设置弹窗
	 * 
	 * @param type
	 */
	private void setPopup(int type) {
		middlePopup = new TopMiddlePopup(MainActivity.this, screenW, screenH,
				onItemClickListener, getItemsName(), type);
	}

	/**
	 * 设置弹窗内容
	 * 
	 * @return
	 */
	private ArrayList getItemsName() {
		ArrayList items = new ArrayList();
		items.add("企业客户");
		items.add("集团客户");
		items.add("公海客户");
		return items;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.urm_back_btn:
			setPopup(1);
			middlePopup.show(topLineTv);
			break;
		case R.id.urm_create_btn:
			setPopup(2);
			middlePopup.show(topLineTv);
			break;
		case R.id.urm_confirm_btn:

			break;
		case R.id.urm_top_ll:
			setPopup(0);
			middlePopup.show(topLineTv);
			break;
		}
	}

	/**
	 * 弹窗点击事件
	 */
	private OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView parent, View view, int position,
				long id) {
			System.out.println("--onItemClickListener--:");
			middlePopup.dismiss();
		}
	};

	/**
	 * 获取屏幕的宽和高
	 */
	public void getScreenPixels() {
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		screenW = metrics.widthPixels;
		screenH = metrics.heightPixels;
	}

}

3.自定义弹窗类 TopMiddlePopup.java

package com.popuptest;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.AdapterView.OnItemClickListener;

public class TopMiddlePopup extends PopupWindow {

	private Context myContext;
	private ListView myLv;
	private OnItemClickListener myOnItemClickListener;
	private ArrayList myItems;
	private int myWidth;
	private int myHeight;
	private int myType;

	// 判断是否需要添加或更新列表子类项
	private boolean myIsDirty = true;

	private LayoutInflater inflater = null;
	private View myMenuView;

	private LinearLayout popupLL;

	private PopupAdapter adapter;

	public TopMiddlePopup(Context context) {
		// TODO Auto-generated constructor stub
	}

	public TopMiddlePopup(Context context, int width, int height,
			OnItemClickListener onItemClickListener, ArrayList items,
			int type) {

		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		myMenuView = inflater.inflate(R.layout.top_popup, null);

		this.myContext = context;
		this.myItems = items;
		this.myOnItemClickListener = onItemClickListener;
		this.myType = type;

		this.myWidth = width;
		this.myHeight = height;

		System.out.println("--myWidth--:" + myWidth + "--myHeight--:"
				+ myHeight);
		initWidget();
		setPopup();
	}

	/**
	 * 初始化控件
	 */
	private void initWidget() {
		myLv = (ListView) myMenuView.findViewById(R.id.popup_lv);
		popupLL = (LinearLayout) myMenuView.findViewById(R.id.popup_layout);
		myLv.setOnItemClickListener(myOnItemClickListener);

		if (myType == 1) {
			android.widget.RelativeLayout.LayoutParams lpPopup = (android.widget.RelativeLayout.LayoutParams) popupLL
					.getLayoutParams();
			lpPopup.width = (int) (myWidth * 1.0 / 4);
			lpPopup.setMargins(0, 0, (int) (myWidth * 3.0 / 4), 0);
			popupLL.setLayoutParams(lpPopup);
		} else if (myType == 2) {
			android.widget.RelativeLayout.LayoutParams lpPopup = (android.widget.RelativeLayout.LayoutParams) popupLL
					.getLayoutParams();
			lpPopup.width = (int) (myWidth * 1.0 / 4);
			lpPopup.setMargins((int) (myWidth * 3.0 / 4), 0, 0, 0);
			popupLL.setLayoutParams(lpPopup);
		}
	}

	/**
	 * 设置popup的样式
	 */
	private void setPopup() {
		// 设置AccessoryPopup的view
		this.setContentView(myMenuView);
		// 设置AccessoryPopup弹出窗体的宽度
		this.setWidth(LayoutParams.MATCH_PARENT);
		// 设置AccessoryPopup弹出窗体的高度
		this.setHeight(LayoutParams.MATCH_PARENT);
		// 设置AccessoryPopup弹出窗体可点击
		this.setFocusable(true);
		// 设置AccessoryPopup弹出窗体的动画效果
		if (myType == 1) {
			this.setAnimationStyle(R.style.AnimTopLeft);
		} else if (myType == 2) {
			this.setAnimationStyle(R.style.AnimTopRight);
		} else {
			//this.setAnimationStyle(R.style.AnimTop);
			this.setAnimationStyle(R.style.AnimTopMiddle);
		}
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0x33000000);
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);

		myMenuView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				int height = popupLL.getBottom();
				int left = popupLL.getLeft();
				int right = popupLL.getRight();
				System.out.println("--popupLL.getBottom()--:"
						+ popupLL.getBottom());
				int y = (int) event.getY();
				int x = (int) event.getX();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y > height || x < left || x > right) {
						System.out.println("---点击位置在列表下方--");
						dismiss();
					}
				}
				return true;
			}
		});
	}

	/**
	 * 显示弹窗界面
	 * 
	 * @param view
	 */
	public void show(View view) {
		if (myIsDirty) {
			myIsDirty = false;
			adapter = new PopupAdapter(myContext, myItems, myType);
			myLv.setAdapter(adapter);
		}

		showAsDropDown(view, 0, 0);
	}

}

4.自定义弹窗布局 top_popup.xml




    

        
        

        
    


5.弹窗类表适配器类 PopupAdapter

package com.popuptest;

import java.util.ArrayList;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class PopupAdapter extends BaseAdapter {
	private Context myContext;
	private LayoutInflater inflater;
	private ArrayList myItems;
	private int myType;

	public PopupAdapter(Context context, ArrayList items, int type) {
		this.myContext = context;
		this.myItems = items;
		this.myType = type;

		inflater = LayoutInflater.from(myContext);

	}

	@Override
	public int getCount() {
		return myItems.size();
	}

	@Override
	public String getItem(int position) {
		return myItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PopupHolder holder = null;
		if (convertView == null) {
			holder = new PopupHolder();
			convertView = inflater.inflate(R.layout.top_popup_item, null);
			holder.itemNameTv = (TextView) convertView
					.findViewById(R.id.popup_tv);
			if (myType == 0) {
				holder.itemNameTv.setGravity(Gravity.CENTER);
			} else if (myType == 1) {
				holder.itemNameTv.setGravity(Gravity.LEFT);
			} else if (myType == 2) {
				holder.itemNameTv.setGravity(Gravity.RIGHT);
			}
			convertView.setTag(holder);
		} else {
			holder = (PopupHolder) convertView.getTag();
		}
		String itemName = getItem(position);
		holder.itemNameTv.setText(itemName);
		return convertView;
	}

	private class PopupHolder {
		TextView itemNameTv;
	}

}

6.子item布局 top_popup_item.xml




    


7.主界面顶部布局 urm_top.xml




    

    

        

        
    

    

        

        

8.styles.xml文件



    
    

    
    
    
    
    
     
    
     
    
     
    
    

    


9.各种动画效果

push_top_in.xml





    

    


push_top_out.xml





    

    


top_left_in.xml




    


top_left_out.xml




    


top_middle_in.xml




    


top_middle_out.xml




    


top_right_in.xml




    


top_right_out.xml




    


运行项目即可搞定!

你可能感兴趣的:(Android,Android项目实战基地)