IT技术互动交流平台

AndroidSpinner用PopupWindow实现

来源:IT165收集  发布日期:2015-05-22 20:26:34

先看效果图:

控件类:

 

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<LocationBean>{

	public LocationSpinnerAdapter(Context context, List<LocationBean> 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<LocationBean> 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<LocationBean> 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链接:http://download.csdn.net/download/anddroid_lanyan/8727625

 

备注:这个demo用了第三方框架Xutil的注解,还有自己整合的一个ListView适配器的ListAdapter+ViewHolder用法,若需要源码联系我即可。

延伸阅读:

  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规