Android仿美团分类下拉菜单实例代码
程序员文章站
2024-03-01 09:32:52
本文实例为大家分享了android仿美团下拉菜单的实现代码,分类进行选择,供大家参考,具体内容如下
效果图
操作平台
as2.0
第三方框架:butterkni...
本文实例为大家分享了android仿美团下拉菜单的实现代码,分类进行选择,供大家参考,具体内容如下
效果图
操作平台
as2.0
第三方框架: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' }
实现代码
1、实现逻辑:主要利用spinner与baseadapter来实现菜单下拉。具体美观效果要靠各个控件的调整,以及必要的分割线添加。
2、核心代码
适配器的item代码: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="测试文字" android:textsize="15dp" /> <!--分割线--> <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>
主页面布局文件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" > <linearlayout android:layout_width="match_parent" android:layout_height="40dp" > <spinner android:id="@+id/main_sp1" style="@style/style_spinner"> </spinner> <!--这个是spinner之间的分割线--> <imageview style="@style/style_mian_verline"/> <spinner android:id="@+id/main_sp2" style="@style/style_spinner" ></spinner> <imageview style="@style/style_mian_verline"/> <spinner android:id="@+id/main_sp3" style="@style/style_spinner" ></spinner> </linearlayout> <!--设置一条分割线--> <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_height="match_parent" android:gravity="center" android:text="我是分类下面的正文内容"/> </linearlayout> </linearlayout>
style文件
<!--spinner的样式--> <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> <!--定义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与适配器写在了一起,便于操作
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;//适配器 @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); butterknife.bind(this); initdata(); initadapter(); setlistener(); } //设置监听事件,将来商家列表的排序都在这里面处理 private void setlistener() { mmainsp1.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { toast.maketext(mainactivity.this, "点击了"+data_list1.get(position), toast.length_short).show(); } @override public void onnothingselected(adapterview<?> parent) { } }); mmainsp2.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { toast.maketext(mainactivity.this, "点击了"+data_list2.get(position), toast.length_short).show(); } @override public void onnothingselected(adapterview<?> parent) { } }); mmainsp3.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { toast.maketext(mainactivity.this, "点击了"+data_list3.get(position), toast.length_short).show(); } @override public void onnothingselected(adapterview<?> parent) { } }); } private void initadapter() { //适配器 arr_adapter = new myadapter(data_list1); mmainsp1.setadapter(arr_adapter); //适配器 arr_adapter = new myadapter(data_list2); mmainsp2.setadapter(arr_adapter); //适配器 arr_adapter = new myadapter(data_list3); mmainsp3.setadapter(arr_adapter); } //设置数据来源,这个要在网络获取。由于数据不会太多,不再考虑性能优化问题 private void initdata() { //数据 data_list1 = new arraylist<>(); data_list1.add("全部分类"); data_list1.add("零食"); data_list1.add("水果"); data_list1.add("洗衣"); data_list1.add("洗车"); data_list1.add("茶饮"); //数据 data_list2 = new arraylist<>(); data_list2.add("商圈"); data_list2.add("郑州航院"); data_list2.add("华北水利水电大学"); data_list2.add("河南中医大学"); data_list2.add("河南财经大学"); //数据 data_list3 = new arraylist<>(); data_list3.add("智能排序"); data_list3.add("好评优先"); data_list3.add("离我最近"); data_list3.add("价格最低"); } //适配器 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; } //数据不太多,没有使用viewholder进行处理。 @override public view getview(int position, view convertview, viewgroup parent) { convertview = view.inflate(mainactivity.this, r.layout.item_main, null); textview textview = (textview) convertview.findviewbyid(r.id.item_main_tv); textview.settext(mdatalist.get(position)); return convertview; } } }
以上就是本文的全部内容,希望对大家学习android软件编程有所帮助。