Android Spinner implemented with PopupWindow

Source: Internet
Author: User

Android Spinner implemented with PopupWindow

First look:

 

Controls:

 

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 stubsetOnClickListener(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}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(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);}}

Adapter:

 

 

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}@Overridepublic int setContentView() {// TODO Auto-generated method stubreturn R.layout.item;}@Overridepublic void initial(View view, LocationBean arg1,OnListViewListener
   
     arg2) {// TODO Auto-generated method stubViewHolder.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;}}
The width and height of PopupWindow can be defined according to your own needs. You can manually modify the ousideTouch event, preferably the custom style pouwindow public method. The following is a preferred method for users who pass in a 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() {@Overridepublic void onItemClick(AdapterView
   parent, View view,int position, long id) {// TODO Auto-generated method stubif(mPosition!=position){mPosition=position;mListener.onCheckChange(mAdapter.getItem(position));}else{mListener.onCheckChange(null);}}});return mView;}}
 

The following is a test class (BaseActivity is mandatory for individuals with obsessive-compulsive disorder): 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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(getContentView());ViewUtils.inject(this);initView();}public abstract int getContentView();protected void initView() {// TODO Auto-generated method stub}@Overridepublic void onShow() {// TODO Auto-generated method stubif(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 stubreturn null;}protected View getPopupWindowView() {// TODO Auto-generated method stubreturn SpinnerPopupView.getInstance().getCurrentView(this, LocationBean.getData(), this);}@Overridepublic void onCheckChange(LocationBean bean) {// TODO Auto-generated method stubif(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;@Overridepublic int getContentView() {// TODO Auto-generated method stubreturn R.layout.activity_main;}@Overrideprotected void initView() {// TODO Auto-generated method stubsuper.initView();mSpinner.setListener(this);}@Overrideprotected View getDropDownView() {// TODO Auto-generated method stubreturn mSpinner;}@Overridepublic void onCheckChange(LocationBean bean) {// TODO Auto-generated method stubsuper.onCheckChange(bean);if(bean!=null){mSpinner.setText(bean.getName());toastShow(Name: +bean.getName()+ ID: +bean.getId());}}}

 

Note: This demo uses the third-party framework Xutil annotation, as well as the ListAdapter + ViewHolder usage of a self-integrated ListView adapter. If you need the source code, contact me.

 



 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.