Android实现美团下拉功能
程序员文章站
2022-06-03 16:31:33
本文实例为大家分享了android实现美团下拉功能的具体代码,供大家参考,具体内容如下
效果图
实现
布局文件:
本文实例为大家分享了android实现美团下拉功能的具体代码,供大家参考,具体内容如下
效果图
实现
布局文件:
<?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:background="#f0f0f0" android:orientation="vertical" > <framelayout android:id="@+id/titlebar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#5555" > <relativelayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#ffffff" android:gravity="center_vertical" android:orientation="horizontal" > <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_centervertical="true" android:layout_marginleft="5dp" android:gravity="center_vertical" > <imageview android:layout_width="25dp" android:layout_height="25dp" android:onclick="back" android:src="@mipmap/icon_arrow_green" /> <textview android:id="@+id/supplier_list_title_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:text="全部" android:textcolor="#39ac69" android:textsize="18dp" /> </linearlayout> <imageview android:id="@+id/supplier_list_cart_iv" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="15dp" android:src="@mipmap/icon_car" /> </relativelayout> </framelayout> <view android:layout_width="match_parent" android:layout_height="1dp" android:background="#e2e2e2" /> <linearlayout android:layout_width="match_parent" android:layout_height="40dp" android:background="#ffffff" android:orientation="horizontal" > <linearlayout android:id="@+id/supplier_list_product" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="horizontal" > <textview android:id="@+id/supplier_list_product_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="全部" android:textsize="14dp" /> <imageview android:layout_width="25dp" android:layout_height="25dp" android:src="@mipmap/icon_arrow_down" /> </linearlayout> <linearlayout android:id="@+id/supplier_list_sort" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="horizontal" > <textview android:id="@+id/supplier_list_sort_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="综合排序" android:textsize="14dp" /> <imageview android:layout_width="25dp" android:layout_height="25dp" android:src="@mipmap/icon_arrow_down" /> </linearlayout> <linearlayout android:id="@+id/supplier_list_activity" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="horizontal" > <textview android:id="@+id/supplier_list_activity_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="优惠活动" android:textsize="14dp" /> <imageview android:layout_width="25dp" android:layout_height="25dp" android:src="@mipmap/icon_arrow_down" /> </linearlayout> </linearlayout> <view android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#e2e2e2" /> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" > <listview android:id="@+id/supplier_list_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margintop="10dp" android:cachecolorhint="@android:color/transparent" android:divider="#f0f0f0" android:dividerheight="10dp" android:fadingedge="none" android:listselector="#00000000" android:scrollbarstyle="outsideoverlay" android:scrollingcache="false" /> <progressbar android:id="@+id/progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:indeterminatedrawable="@drawable/shape_progress" android:indeterminateduration="1000" /> </relativelayout> </linearlayout>
<?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="wrap_content" android:background="#ffffff" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <textview android:id="@+id/listview_popwind_tv" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginleft="15dp" android:layout_marginright="15dp" android:gravity="center_vertical" android:text="地点" android:textcolor="#5a5959" android:textsize="18dp" /> </linearlayout> <view android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginleft="15dp" android:layout_marginright="15dp" android:background="#e2e2e2" /> </linearlayout>
<?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:background="#5000" android:orientation="vertical" > <listview android:id="@+id/popwin_supplier_list_lv" android:layout_width="match_parent" android:layout_height="wrap_content" android:cachecolorhint="@android:color/transparent" android:divider="#0000" android:dividerheight="0dp" android:fadingedge="none" android:listselector="#00000000" android:scrollbarstyle="outsideoverlay" android:scrollbars="none" android:scrollingcache="false" /> <linearlayout android:id="@+id/popwin_supplier_list_bottom" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical" /> </linearlayout>
activity代码:
package com.example.hfs.popwindowmeituan; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import android.app.activity; import android.content.intent; import android.graphics.color; import android.graphics.drawable.bitmapdrawable; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.imageview; import android.widget.linearlayout; import android.widget.listview; import android.widget.popupwindow; import android.widget.progressbar; import android.widget.simpleadapter; import android.widget.textview; import android.widget.toast; import android.widget.adapterview.onitemclicklistener; import android.widget.popupwindow.ondismisslistener; public class mainactivity extends activity implements onclicklistener { private listview listview, poplistview; private progressbar progressbar; private list<map<string, string>> menudata1, menudata2, menudata3; private popupwindow popmenu; private simpleadapter menuadapter1, menuadapter2, menuadapter3; private linearlayout product, sort, activity; private imageview cartiv; private textview producttv, sorttv, activitytv, titletv; private int green, grey; private string currentproduct, currentsort, currentactivity; private int menuindex = 0; private intent intent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_supplier_list); findview(); initmenudata(); initpopmenu(); } private void initmenudata() { menudata1 = new arraylist<map<string, string>>(); string[] menustr1 = new string[] { "全部", "粮油", "衣服", "图书", "电子产品", "酒水饮料", "水果" }; map<string, string> map1; for (int i = 0, len = menustr1.length; i < len; ++i) { map1 = new hashmap<string, string>(); map1.put("name", menustr1[i]); menudata1.add(map1); } menudata2 = new arraylist<map<string, string>>(); string[] menustr2 = new string[] { "综合排序", "配送费最低" }; map<string, string> map2; for (int i = 0, len = menustr2.length; i < len; ++i) { map2 = new hashmap<string, string>(); map2.put("name", menustr2[i]); menudata2.add(map2); } menudata3 = new arraylist<map<string, string>>(); string[] menustr3 = new string[] { "优惠活动", "特价活动", "免配送费", "可在线支付" }; map<string, string> map3; for (int i = 0, len = menustr3.length; i < len; ++i) { map3 = new hashmap<string, string>(); map3.put("name", menustr3[i]); menudata3.add(map3); } } @override public void onclick(view v) { // todo auto-generated method stub switch (v.getid()) { case r.id.supplier_list_product: producttv.settextcolor(color.parsecolor("#39ac69")); poplistview.setadapter(menuadapter1); popmenu.showasdropdown(product, 0, 2); menuindex = 0; break; case r.id.supplier_list_sort: sorttv.settextcolor(color.parsecolor("#39ac69")); poplistview.setadapter(menuadapter2); popmenu.showasdropdown(product, 0, 2); menuindex = 1; break; case r.id.supplier_list_activity: activitytv.settextcolor(color.parsecolor("#39ac69")); poplistview.setadapter(menuadapter3); popmenu.showasdropdown(product, 0, 2); menuindex = 2; break; } } protected void findview() { listview = (listview) findviewbyid(r.id.supplier_list_lv); product = (linearlayout) findviewbyid(r.id.supplier_list_product); sort = (linearlayout) findviewbyid(r.id.supplier_list_sort); activity = (linearlayout) findviewbyid(r.id.supplier_list_activity); producttv = (textview) findviewbyid(r.id.supplier_list_product_tv); sorttv = (textview) findviewbyid(r.id.supplier_list_sort_tv); activitytv = (textview) findviewbyid(r.id.supplier_list_activity_tv); titletv = (textview) findviewbyid(r.id.supplier_list_title_tv); cartiv = (imageview) findviewbyid(r.id.supplier_list_cart_iv); progressbar = (progressbar) findviewbyid(r.id.progress); product.setonclicklistener(this); sort.setonclicklistener(this); activity.setonclicklistener(this); cartiv.setonclicklistener(this); } private void initpopmenu() { initmenudata(); view contentview = view.inflate(this, r.layout.popwin_supplier_list, null); popmenu = new popupwindow(contentview, linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent); popmenu.setoutsidetouchable(true); popmenu.setbackgrounddrawable(new bitmapdrawable()); popmenu.setfocusable(true); popmenu.setanimationstyle(r.style.popwin_anim_style); popmenu.setondismisslistener(new ondismisslistener() { public void ondismiss() { producttv.settextcolor(color.parsecolor("#5a5959")); sorttv.settextcolor(color.parsecolor("#5a5959")); activitytv.settextcolor(color.parsecolor("#5a5959")); } }); poplistview = (listview) contentview .findviewbyid(r.id.popwin_supplier_list_lv); contentview.findviewbyid(r.id.popwin_supplier_list_bottom) .setonclicklistener(new onclicklistener() { public void onclick(view arg0) { popmenu.dismiss(); } }); menuadapter1 = new simpleadapter(this, menudata1, r.layout.item_listview_popwin, new string[] { "name" }, new int[] { r.id.listview_popwind_tv }); menuadapter2 = new simpleadapter(this, menudata2, r.layout.item_listview_popwin, new string[] { "name" }, new int[] { r.id.listview_popwind_tv }); menuadapter3 = new simpleadapter(this, menudata3, r.layout.item_listview_popwin, new string[] { "name" }, new int[] { r.id.listview_popwind_tv }); poplistview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> arg0, view arg1, int pos, long arg3) { popmenu.dismiss(); if (menuindex == 0) { currentproduct = menudata1.get(pos).get("name"); titletv.settext(currentproduct); producttv.settext(currentproduct); toast.maketext(mainactivity.this, currentproduct, toast.length_short).show(); } else if (menuindex == 1) { currentsort = menudata2.get(pos).get("name"); titletv.settext(currentsort); sorttv.settext(currentsort); toast.maketext(mainactivity.this, currentsort, toast.length_short).show(); } else { currentactivity = menudata3.get(pos).get("name"); titletv.settext(currentactivity); activitytv.settext(currentactivity); toast.maketext(mainactivity.this, currentactivity, toast.length_short).show(); } } }); } }
其实和我博客里面有一篇自定义spinner很像
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: PHP中的闭包(匿名函数)浅析