There are a lot of applications on the market, with a drop-down list of features, all the options are placed in the Drop-down list, when the user clicks on the selection, pop-up all the options, the user selects an item, the Drop-down list automatically hidden, many Drop-down lists are used ListView + Popupwindow to achieve, As Google has introduced a replacement for ListView's Recyclerview, this is a simple implementation:
Mainactivity.java
Package Com.jackie.countdowntimer;
Import android.graphics.drawable.BitmapDrawable;
Import Android.os.Bundle;
Import Android.os.CountDownTimer;
Import Android.support.design.widget.FloatingActionButton;
Import Android.support.design.widget.Snackbar;
Import android.support.v7.app.AppCompatActivity;
Import Android.support.v7.widget.LinearLayoutManager;
Import Android.support.v7.widget.RecyclerView;
Import Android.support.v7.widget.Toolbar;
Import Android.view.View;
Import Android.widget.EditText;
Import Android.widget.ImageButton;
Import Android.widget.PopupWindow;
Import java.util.ArrayList;
Import java.util.List;
public class Mainactivity extends Appcompatactivity implements View.onclicklistener {private EditText mnumberedittext;
Private ImageButton Mselectimagebutton;
Private Popupwindow Mpopupwindow;
Private Recyclerview Mrecyclerview;
Private Recyclerviewadapter Mrecyclerviewadapter;
Private list<string> mnumberlist;
@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar);
Setsupportactionbar (toolbar);
Floatingactionbutton fab = (Floatingactionbutton) Findviewbyid (R.id.fab); Fab.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {Snac
Kbar.make (View, "Replace with your own action", Snackbar.length_long). Setaction ("action", null). Show ();
}
});
Initialize Data Mnumberlist = new arraylist<> ();
for (int i = 0; i < i++) {Mnumberlist.add ("1000000" + i);
} Mnumberedittext = (edittext) Findviewbyid (R.id.number);
Mselectimagebutton = (ImageButton) Findviewbyid (R.id.select_number);
Mselectimagebutton.setonclicklistener (this); @Override public void OnClick (View v) {switch (V.getid ()) {case R.id.select_number:showselectnumberpopupwindow ();
Break
}/** * Pop-up select Number dialog box * * private void Showselectnumberpopupwindow () {Initrecyclerview ();
Mpopupwindow = new Popupwindow (Mrecyclerview, Mnumberedittext.getwidth ()-4, 200); Mpopupwindow.setoutsidetouchable (TRUE);
Set external can be clicked Mpopupwindow.setbackgrounddrawable (New Bitmapdrawable ()); Mpopupwindow.setfocusable (TRUE);
Enable Popupwindow to get focus//display in the lower left corner of the input box Mpopupwindow.showasdropdown (Mnumberedittext, 2,-5); /** * Initialize Recyclerview, imitate ListView drop-down List effect * * private void Initrecyclerview () {Mrecyclerview = new Rec
Yclerview (this);
Set the layout manager mrecyclerview.setlayoutmanager (new Linearlayoutmanager (this));
Mrecyclerview.setbackgroundresource (R.drawable.background);
Set adapter Mrecyclerviewadapter = new Recyclerviewadapter (this, mnumberlist);
Mrecyclerview.setadapter (Mrecyclerviewadapter); Set Click events
Mrecyclerviewadapter.setonitemclicklistener (New Recyclerviewadapter.onitemclicklistener () {@Override
public void Onitemclick (view view, int position) {Mnumberedittext.settext (Mnumberlist.get (position));
Mnumberedittext.setselection (Mnumberedittext.gettext (). toString (). Length ());
Mpopupwindow.dismiss ();
}
});
Add a split line mrecyclerview.additemdecoration (this, divideritemdecoration.vertical_list) to the new Divideritemdecoration; }
}
recyclerviewadapter.java
Package Com.jackie.countdowntimer;
Import Android.content.Context;
Import Android.support.v7.widget.RecyclerView;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ImageButton;
Import Android.widget.TextView;
Import java.util.List;
/** * Created by Jackie on 2015/12/18. * Recyclerview Adapter * * * public class Recyclerviewadapter extends Recyclerview.adapter<recyclerviewadapter.recycle
rviewholder> {private Onitemclicklistener monitemclicklistener;
Private context Mcontext;
Private list<string> mnumberlist;
Public Recyclerviewadapter (context context, list<string> numberlist) {this.mcontext = context;
This.mnumberlist = numberlist;
public interface Onitemclicklistener {void Onitemclick (view view, int position); } public void Setonitemclicklistener (Onitemclicklistener onitemclicklistener) {This.monitemclicklistener = OnIt EmclicklisTener;
@Override public Recyclerviewadapter.recyclerviewholder Oncreateviewholder (viewgroup parent, int viewtype) { Recyclerviewholder holder = new Recyclerviewholder (Layoutinflater.from (Mcontext). Inflate (R.layout.number_item,
Parent, false));
return holder; @Override public void Onbindviewholder (final recyclerviewadapter.recyclerviewholder holder, final int position)
{Holder.numberTextView.setText (Mnumberlist.get (position)); If a callback is set, set the Click event if (Monitemclicklistener!= null) {Holder.numberTextView.setOnClickListener (new view.oncli Cklistener () {@Override public void OnClick (View v) {Monitemclicklistener.onitemclick (Holde
R.itemview, Holder.getlayoutposition ());
}
}); Holder.deleteImageButton.setOnClickListener (New View.onclicklistener () {@Override public void Oncl
Ick (View v) {mnumberlist.remove (position); Notification Refresh Notifyitemremoved (position);
}
});
@Override public int GetItemCount () {return mnumberlist.size ();
The public class Recyclerviewholder extends Recyclerview.viewholder {TextView numbertextview;
ImageButton Deleteimagebutton;
Public Recyclerviewholder (View Itemview) {super (Itemview);
Numbertextview = (TextView) Itemview.findviewbyid (R.id.number_item);
Deleteimagebutton = (ImageButton) Itemview.findviewbyid (r.id.number_item_delete); }
}
}
divideritemdecoration.java
Package Com.jackie.countdowntimer; * * Copyright (C) 2014 the Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "Lic
Ense ");
* Limitations under the License.
* * Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Canvas;
Import Android.graphics.Rect;
Import android.graphics.drawable.Drawable;
Import Android.support.v7.widget.LinearLayoutManager;
Import Android.support.v7.widget.RecyclerView;
Import Android.view.View; /** * This class are from the V7 samples of the Android SDK.
It ' s not by me!
* <p/> * License above for details. * * public class Divideritemdecoration extends Recyclerview.itemdecoration {private static final int[] Attrs = new I nt[]{Android.
R.attr.listdivider};
public static final int horizontal_list = linearlayoutmanager.horizontal;
public static final int vertical_list = linearlayoutmanager.vertical; Private DrawabLe Mdivider;
private int morientation; Public Divideritemdecoration (context context, int orientation) {Final TypedArray a = Context.obtainstyledattributes (
ATTRS);
Mdivider = a.getdrawable (0);
A.recycle ();
SetOrientation (orientation); public void setorientation (int orientation) {If orientation!= horizontal_list && Orientation!= VER
Tical_list) {throw new IllegalArgumentException ("Invalid orientation");
} morientation = orientation; @Override public void OnDraw (Canvas C, recyclerview parent) {if (morientation = = vertical_list) {D
Rawvertical (c, parent);
else {Drawhorizontal (c, parent);
} public void Drawvertical (Canvas C, Recyclerview parent) {Final int left = Parent.getpaddingleft ();
Final int right = Parent.getwidth ()-parent.getpaddingright ();
Final int childcount = Parent.getchildcount ();
for (int i = 0; i < ChildCount; i++) { Final View child = Parent.getchildat (i);
Android.support.v7.widget.RecyclerView v = new Android.support.v7.widget.RecyclerView (Parent.getcontext ());
Final Recyclerview.layoutparams params = (recyclerview.layoutparams) child.getlayoutparams ();
Final int top = Child.getbottom () + Params.bottommargin;
final int bottom = top + mdivider.getintrinsicheight ();
Mdivider.setbounds (left, top, right, bottom);
Mdivider.draw (c);
} public void Drawhorizontal (Canvas C, Recyclerview parent) {Final int top = Parent.getpaddingtop ();
final int bottom = Parent.getheight ()-Parent.getpaddingbottom ();
Final int childcount = Parent.getchildcount ();
for (int i = 0; i < ChildCount. i++) {final View child = Parent.getchildat (i);
Final Recyclerview.layoutparams params = (recyclerview.layoutparams) child.getlayoutparams ();
Final int left = Child.getright () + Params.rightmargin; Final int right =Left + mdivider.getintrinsicheight ();
Mdivider.setbounds (left, top, right, bottom);
Mdivider.draw (c); @Override public void Getitemoffsets (Rect outrect, int itemposition, Recyclerview parent) {if (Morien
tation = = vertical_list) {outrect.set (0, 0, 0, mdivider.getintrinsicheight ());
else {outrect.set (0, 0, mdivider.getintrinsicwidth (), 0);
}
}
}
The effect chart is as follows:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.