Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Spinner 用PopupWindow實現

Android Spinner 用PopupWindow實現

編輯:關於Android編程

先看效果圖:

\

 

控件類:

 

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LocationSpinner extends Button implements OnClickListener {


	public LocationSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		setOnClickListener(this);
	}

	public LocationSpinner(Context context, AttributeSet attrs) {
		this(context, attrs,0);
		// TODO Auto-generated constructor stub
	}

	public LocationSpinner(Context context) {
		this(context,null);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(listener!=null){
			listener.onShow();
		}
	}
	
	private OnSpinnerListener listener;

	public OnSpinnerListener getListener() {
		return listener;
	}

	public void setListener(OnSpinnerListener listener) {
		this.listener = listener;
	}

	
	public interface OnSpinnerListener{
		void onShow();
		void onCheckChange(LocationBean bean);
	}

}

適配器:

 

 

import java.util.List;

import android.content.Context;
import android.view.View;

import com.lanyan.adapter.ListAdapter;
import com.lanyan.adapter.ViewHolder;
import com.lanyan.floatdialot.R;
import com.lanyan.interfaces.OnListViewListener;

public class LocationSpinnerAdapter extends ListAdapter{

	public LocationSpinnerAdapter(Context context, List list) {
		super(context, list);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int setContentView() {
		// TODO Auto-generated method stub
		return R.layout.item;
	}

	@Override
	public void initial(View view, LocationBean arg1,
			OnListViewListener arg2) {
		// TODO Auto-generated method stub
		ViewHolder.setTextView(view, R.id.head, arg1.getName());
	}

}
PopupWindow:

 

 

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

public class SpinnerPopupWindow {

	private int mScreenWidth;
	private int mScreenHeight;
	public SpinnerPopupWindow(){
		
	}
	public static SpinnerPopupWindow getInstance(){
		return new SpinnerPopupWindow();
	}
	
	public PopupWindow onCreateSpinnerPopupWindow(Activity mActivity,View mView){
		initScreen(mActivity);
		PopupWindow mPopupWindow=new PopupWindow(mView,mScreenWidth/3*2,mScreenHeight/3*2);
		
		mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x000000));
       
        mPopupWindow.setOutsideTouchable(false);
		return mPopupWindow;
	}
	public void initScreen(Activity mActivity){

		WindowManager manager=(WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics=new DisplayMetrics();
		manager.getDefaultDisplay().getMetrics(outMetrics);

		mScreenWidth=outMetrics.widthPixels;
		mScreenHeight=outMetrics.heightPixels;
	}
}
PopupWindow的寬高可根據自己的需求定義,ousideTouch事件 手動修改即可,最好是自定義style popouWindow公開方法。這裡的傳入View使用的個人比較喜歡的一種方式:

 

 

import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.lanyan.floatdialot.R;
import com.lanyan.widget.LocationSpinner.OnSpinnerListener;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

@SuppressLint(InflateParams)
public class SpinnerPopupView {

	@ViewInject(R.id.spinner_listview)
	private ListView mListView;

	private int mPosition=-1;
	private View mView;

	private LocationSpinnerAdapter mAdapter;

	public SpinnerPopupView(){

	}
	public static SpinnerPopupView getInstance(){
		return new SpinnerPopupView();
	}

	public View getCurrentView(Activity mActivity,List mList,final OnSpinnerListener mListener){

		mView=LayoutInflater.from(mActivity).inflate(R.layout.spinner, null);
		ViewUtils.inject(this,mView);
		mAdapter=new LocationSpinnerAdapter(mActivity, mList);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if(mPosition!=position){
					mPosition=position;
					mListener.onCheckChange(mAdapter.getItem(position));
				}else{
					mListener.onCheckChange(null);
				}
			}
		});
		return mView;
	}
}

下面是測試類(個人有強迫症必用BaseActivity):BaseActivity.MainActivity

 

 

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.lanyan.widget.LocationBean;
import com.lanyan.widget.SpinnerPopupView;
import com.lanyan.widget.SpinnerPopupWindow;
import com.lanyan.widget.LocationSpinner.OnSpinnerListener;
import com.lidroid.xutils.ViewUtils;

public abstract class BaseActivity extends Activity implements OnSpinnerListener{

	protected PopupWindow mPopupWindow=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(getContentView());
		ViewUtils.inject(this);
		initView();

	}

	public abstract int getContentView();
	protected void initView() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onShow() {
		// TODO Auto-generated method stub
		if(mPopupWindow!=null&&mPopupWindow.isShowing()){
			return;
		}else if(mPopupWindow==null){
			mPopupWindow=SpinnerPopupWindow.getInstance().onCreateSpinnerPopupWindow(this, getPopupWindowView());
		}
		if(mPopupWindow!=null&&!mPopupWindow.isShowing()){
			mPopupWindow.showAsDropDown(getDropDownView());
			WindowManager.LayoutParams lp=getWindow().getAttributes();
			lp.alpha = 0.4f;
			getWindow().setAttributes(lp);
		}
	}

	protected View getDropDownView() {
		// TODO Auto-generated method stub
		return null;
	}

	protected View getPopupWindowView() {
		// TODO Auto-generated method stub
		return SpinnerPopupView.getInstance().getCurrentView(this, LocationBean.getData(), this);
	}

	@Override
	public void onCheckChange(LocationBean bean) {
		// TODO Auto-generated method stub
		if(mPopupWindow!=null&&mPopupWindow.isShowing()){
			mPopupWindow.dismiss();
			WindowManager.LayoutParams lp=getWindow().getAttributes();
			lp.alpha = 1f;
			getWindow().setAttributes(lp);
		}
	}

	protected void toastShow(String message){
		Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
	}
}

import android.view.View;

import com.lanyan.widget.LocationBean;
import com.lanyan.widget.LocationSpinner;
import com.lidroid.xutils.view.annotation.ViewInject;


public class MainActivity extends BaseActivity{

	@ViewInject(R.id.botton)
	private LocationSpinner mSpinner;

	@Override
	public int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.activity_main;
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		super.initView();	
		mSpinner.setListener(this);
	}

	@Override
	protected View getDropDownView() {
		// TODO Auto-generated method stub
		return mSpinner;
	}
		
	@Override
	public void onCheckChange(LocationBean bean) {
		// TODO Auto-generated method stub
		super.onCheckChange(bean);
		if(bean!=null){
			mSpinner.setText(bean.getName());
			toastShow(Name: +bean.getName()+ ID: +bean.getId());
		}
	}

}

 

備注:這個demo用了第三方框架Xutil的注解,還有自己整合的一個ListView適配器的ListAdapter+ViewHolder用法,若需要源碼聯系我即可。

 



 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved