Custom pull-down style Spinner and androidspinner in Android

Source: Internet
Author: User

Custom pull-down style Spinner and androidspinner in Android

Custom pull-down style Spinner in Android


This article continues to introduce the android custom control series and the use of custom Spinner controls.


Implementation

1. Define the drop-down control layout (ListView and child control layout)

2. Custom SpinerPopWindow class

3. Define the Adapter for data Filling






1. Define the control layout

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "# f2f2f2" android: orientation = "vertical" android: padding = "5dp"> <LinearLayout android: id = "@ + id/layout" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_margin = "5dp" android: background = "# cbcbcb" android: orientation = "vertical" android: padding = "0.2px"> <RelativeLayout android: layout_width = "wrap_content" android: layout_height = "42dp"> <TextView android: id = "@ + id/TV _value" android: layout_width = "match_parent" android: layout_height = "match_parent" android: layout_centerVertical = "true" android: layout_marginRight = "50dp" android: background = "# fff" android: ellipsize = "end" android: gravity = "left | center" android: hint = "Please select" android: paddingLeft = "10dp" android: singleLine = "true" android: textColor = "# ff000000" android: textSize = "18dp"> </TextView> <LinearLayout android: id = "@ + id/bt_dropdown" android: layout_width = "50dp" android: layout_height = "match_parent" android: layout_alignParentRight = "true" android: background = "# fff" android: gravity = "center" android: onClick = "onClick"> <ImageView android: layout_width = "20dp" android: layout_height = "15dp" android: background = "@ drawable/down_arrow"/> </LinearLayout> </RelativeLayout>

Ii. Define the SpinerPopWindow class

Package org. gaochun. widget; import java. util. list; import org. gaochun. r; import org. gaochun. adapter. spinerAdapter; import org. gaochun. adapter. spinerAdapter. IOnItemSelectListener; import android. content. context; import android. graphics. drawable. colorDrawable; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup. layoutParams; import android. widget. adapterView; import andro Id. widget. adapterView. onItemClickListener; import android. widget. listView; import android. widget. popupWindow;/*** custom SpinerPopWindow class * @ author gao_chun **/public class SpinerPopWindow extends PopupWindow implements OnItemClickListener {private Context mContext; private ListView mListView; private SpinerAdapter mAdapter; private IOnItemSelectListener mItemSelectListener; public SpinerPopWindow (Con Text context) {super (context); mContext = context; init ();} public void setItemListener (IOnItemSelectListener listener) {mItemSelectListener = listener;} public void setAdatper (SpinerAdapter adapter) {mAdapter = adapter; mListView. setAdapter (mAdapter);} private void init () {View = LayoutInflater. from (mContext ). inflate (R. layout. spiner_window_layout, null); setContentView (view); setWidth (L AyoutParams. WRAP_CONTENT); setHeight (LayoutParams. WRAP_CONTENT); setFocusable (true); ColorDrawable dw = new ColorDrawable (0x00); setBackgroundDrawable (dw); mListView = (ListView) view. findViewById (R. id. listview); mListView. setOnItemClickListener (this);} public void refreshData (List <String> list, int selIndex) {if (list! = Null & selIndex! =-1) {if (mAdapter! = Null) {mAdapter. refreshData (list, selIndex) ;}}@ Override public void onItemClick (AdapterView <?> Arg0, View view, int pos, long arg3) {dismiss (); if (mItemSelectListener! = Null) {mItemSelectListener. onItemClick (pos );}}}

3. Define the Adapter

package org.gaochun.adapter;import java.util.List;import org.gaochun.R;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class SpinerAdapter extends BaseAdapter {    public static interface IOnItemSelectListener{        public void onItemClick(int pos);    };    private List<String> mObjects;    private LayoutInflater mInflater;    public SpinerAdapter(Context context,List<String> mObjects){        this.mObjects = mObjects;        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    }    public void refreshData(List<String> objects, int selIndex){        mObjects = objects;        if (selIndex < 0){            selIndex = 0;        }        if (selIndex >= mObjects.size()){            selIndex = mObjects.size() - 1;        }    }    @Override    public int getCount() {        return mObjects.size();    }    @Override    public Object getItem(int pos) {        return mObjects.get(pos).toString();    }    @Override    public long getItemId(int pos) {        return pos;    }    @Override    public View getView(int pos, View convertView, ViewGroup arg2) {        ViewHolder viewHolder;        if (convertView == null) {            convertView = mInflater.inflate(R.layout.spiner_item_layout, null);            viewHolder = new ViewHolder();            viewHolder.mTextView = (TextView) convertView.findViewById(R.id.textView);            convertView.setTag(viewHolder);        } else {            viewHolder = (ViewHolder) convertView.getTag();        }        //Object item =  getItem(pos);        viewHolder.mTextView.setText(mObjects.get(pos));        return convertView;    }    public static class ViewHolder    {        public TextView mTextView;    }}


Iv. Call example

Public class MainActivity extends Activity implements OnClickListener, SpinerAdapter. IOnItemSelectListener {private List <String> mListType = new ArrayList <String> (); // type List private TextView mTView; private SpinerAdapter mAdapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_search); mTView = (TextView) findViewById (R. id. TV _value); // initialize the data String [] names = getResources (). getStringArray (R. array. array_name); for (int I = 0; I <names. length; I ++) {mListType. add (names [I]);} mAdapter = new SpinerAdapter (this, mListType); mAdapter. refreshData (mListType, 0); // display the first data // mTView. setText (names [0]); // initialize PopWindow mSpinerPopWindow = new SpinerPopWindow (this); mSpinerPopWindow. setAdatper (mAdapter); mSpinerPopWindow. setItemListener (this);} // set PopWindow private SpinerPopWindow mSpinerPopWindow; private void showSpinWindow () {Log. e ("", "showSpinWindow"); mSpinerPopWindow. setWidth (mTView. getWidth (); mSpinerPopWindow. showAsDropDown (mTView) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. bt_dropdown: showSpinWindow (); break;}/* (non-Javadoc) * @ see org. gaochun. adapter. spinerAdapter. IOnItemSelectListener # onItemClick (int) */@ Override public void onItemClick (int pos) {// TODO Auto-generated method stub if (pos> = 0 & amp; pos <= mListType. size () {String value = mListType. get (pos); mTView. setText (value. toString ());}}}


Download source code:Http://download.csdn.net/download/gao_chun/8653069




V. Custom Control Series

Loading Dialog: http://blog.csdn.net/gao_chun/article/details/45270031 with custom progress in Android Projects

Button listener settings and Optimization in ListView: http://blog.csdn.net/gao_chun/article/details/41249131

Android project custom top title bar: http://blog.csdn.net/gao_chun/article/details/45255929

Android custom set circular image controls: http://blog.csdn.net/gao_chun/article/details/39207557

Android custom bottom Tab, project overall interface framework: http://blog.csdn.net/gao_chun/article/details/37903673

Custom AlertDialog prompt box: http://blog.csdn.net/gao_chun/article/details/37757571


Reprinted to indicate the source.

Related Article

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.