The example of this article for everyone to share the Android imitation American Group Drop-down menu Implementation Code, classification to choose, for your reference, the specific contents are as follows
Effect chart
Operating Platform
AS2.0
Third-party framework: Butterknife
Build.gradle
dependencies {
Compile filetree (dir: ' Libs ', include: [' *.jar '])
testcompile ' junit:junit:4.12 ' compile '
com.android.support:appcompat-v7:23.4.0 '
compile ' com.jakewharton:butterknife:7.0.1 '
}
Implementation code
1, the implementation of logic: the main use of spinner and Baseadapter to implement menu Drop-down. The specific aesthetic effect depends on the adjustment of each control, as well as the necessary split line added.
2. Core code
Item code for adapter: Item_main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/" Apk/res/android "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
Android : orientation= "vertical"
>
<textview
android:id= "@+id/item_main_tv"
android:layout_width= "Match_parent"
android:layout_height= "40DP"
android:layout_marginleft= "16DP"
android:background = "#fff"
android:gravity= "center_vertical"
android:singleline= "true"
android:text= "Test text"
Android:textsize= "15DP"
/>
<!--split line-->
<textview
android:id= "@+id/main_tv_line"
android:layout_width= "match_parent"
android:layout_height= "1DP"
android:layout_marginleft= " 16DP "
android:layout_marginright=" 16DP "
android:background=" #edecec "/>
</LinearLayout>
Main Page layout file Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" "Vertical" &
Gt
<linearlayout android:layout_width= "match_parent" android:layout_height= "40DP" > <spinner
Android:id= "@+id/main_sp1" style= "@style/style_spinner" > </Spinner> <!--This is the dividing line between spinner <imageview style= "@style/style_mian_verline"/> <spinner android:id= "@+id/main_sp2" sty
le= "@style/style_spinner" ></Spinner> <imageview style= "@style/style_mian_verline"/> <spinner android:id= "@+id/main_sp3" style= "@style/style_spinner" ></Spinner> </linear
Layout> <!--set a split line--> <textview android:layout_width= "match_parent" android:layout_height= "1DP" Android:background="#dcdcdc"/> <linearlayout android:layout_width= "match_parent" android:layout_height= "Match_parent" android:background= "#ffffff" > <textview android:layout_width= "match_parent" Android:layout_heigh t= "match_parent" android:gravity= "center" android:text= "I am categorized below the body content"/> </LinearLayout> </linearl
Ayout>
Style file
<!--spinner style-->
<style name= "Style_spinner" >
<item name= "Android:layout_width" >0DP </item>
<item name= "android:layout_height" >40dp</item>
<item name= "Android:layout_" Weight ">1</item>
<item name=" Android:dropdownverticaloffset ">40dp</item>
<item Name= "Android:dropdownwidth" >1000dp</item>
</style>
<!--define the line style between spinner-->
<style name= "Style_mian_verline" >
<item name= "Android:layout_width" >1dp</item>
< Item Name= "Android:layout_height" >25dp</item>
<item name= "Android:background" > #dcdcdc </ item>
<item name= "android:layout_gravity" >center_vertical</item>
</style>
Mainactivity and adapters are written together for easy operation
public class Mainactivity extends Appcompatactivity {@Bind (R.ID.MAIN_SP1) Spinner mMainSp1;
@Bind (R.ID.MAIN_SP2) Spinner mMainSp2;
@Bind (R.ID.MAIN_SP3) Spinner mMainSp3;
Private list<string> Data_list1;
Private list<string> Data_list2;
Private list<string> Data_list3; Private Baseadapter arr_adapter;//Adapter @Override protected void OnCreate (Bundle savedinstancestate) {Super.oncrea
Te (savedinstancestate);
Setcontentview (R.layout.activity_main);
Butterknife.bind (this);
InitData ();
Initadapter ();
Setlistener (); //Set up the Listener event, in which the order of the future merchant list is handled by the private void Setlistener () {Mmainsp1.setonitemselectedlistener (new Adapterview.onite Mselectedlistener () {@Override public void onitemselected (adapterview<?> parent, view view, int positio
n, long id) {Toast.maketext (Mainactivity.this, "clicked" +data_list1.get (position), Toast.length_short). Show (); } @Override public void Onnothingselected (adapterview<?> parent) {}}); Mmainsp2.setonitemselectedlistener (New Adapterview.onitemselectedlistener () {@Override public void Onitemsele CTED (adapterview<?> Parent, view view, int position, long id) {Toast.maketext (Mainactivity.this, "clicked" +data
_list2.get (position), Toast.length_short). Show ();
@Override public void onnothingselected (adapterview<?> parent) {}}); Mmainsp3.setonitemselectedlistener (New Adapterview.onitemselectedlistener () {@Override public void Onitemsele CTED (adapterview<?> Parent, view view, int position, long id) {Toast.maketext (Mainactivity.this, "clicked" +data
_list3.get (position), Toast.length_short). Show ();
@Override public void onnothingselected (adapterview<?> parent) {}});
private void Initadapter () {//Adapter Arr_adapter = new Myadapter (DATA_LIST1);
Mmainsp1.setadapter (Arr_adapter); Adapter Arr_adapter = new Myadapter (DATA_LIST2);
Mmainsp2.setadapter (Arr_adapter);
Adapter Arr_adapter = new Myadapter (DATA_LIST3);
Mmainsp3.setadapter (Arr_adapter); //Set up the data source, which is to be obtained on the network.
Because the data is not too much, the performance optimization problem is no longer considered private void InitData () {//Data Data_list1 = new arraylist<> ();
Data_list1.add ("All categories");
Data_list1.add ("Snack");
Data_list1.add ("fruit");
Data_list1.add ("laundry");
Data_list1.add ("Car Wash");
Data_list1.add ("Tea drink");
Data Data_list2 = new arraylist<> ();
Data_list2.add ("Business Circle");
Data_list2.add ("Zhengzhou Aviation Institute");
Data_list2.add ("North China Water Conservancy and Hydropower University");
Data_list2.add ("Henan University of Traditional Chinese Medicine");
Data_list2.add ("Henan University of Finance and Economics");
Data Data_list3 = new arraylist<> ();
Data_list3.add ("intelligent sort");
Data_list3.add ("Praise first");
Data_list3.add ("Closest to Me");
Data_list3.add ("Lowest price");
}//Adapter class Myadapter extends Baseadapter {private Layoutinflater mlayoutinflater;
Private list<string> mdatalist; Myadapter (List<strinG> dataList) {this.mdatalist = dataList;
Mlayoutinflater = Layoutinflater.from (mainactivity.this);
@Override public int GetCount () {return mdatalist.size ();
@Override public Object getitem (int position) {return mdatalist.get (position);
@Override public long getitemid (int position) {return position;
//data is not too much, not using viewholder for processing. @Override public View getview (int position, View Convertview, ViewGroup parent) {Convertview = View.inflate (Mai
Nactivity.this, r.layout.item_main, NULL);
TextView TextView = (TextView) Convertview.findviewbyid (R.ID.ITEM_MAIN_TV);
Textview.settext (Mdatalist.get (position));
return convertview;
}
}
}
The above is the entire contents of this article, I hope to learn more about Android software programming help.