android 二级菜单、双ListView 仿美团、购物App二级菜单
来源:互联网 发布:软件开发团队管理制度 编辑:程序博客网 时间:2024/05/01 14:06
android 二级菜单、双ListView 仿美团、购物二级菜单
下载Demo
效果如下图,下载后直接粘贴就可以用
Bean.java
package com.mengle.app;import java.util.List;public class Bean {/** * result : Y address : * [{"name":"北京","custId":"010","area":["东城区","西城区","崇文区" * ,"宣武区","延庆县"]},{"name" * :"上海","custId":"021","area":["黄浦区","卢湾区","徐汇区","长宁区","静安区","其他"]}] */private String result;/** * name : 北京 custId : 010 area : ["东城区","西城区","崇文区","宣武区","延庆县"] */private List<AddressEntity> address;public void setResult(String result) {this.result = result;}public void setAddress(List<AddressEntity> address) {this.address = address;}public String getResult() {return result;}public List<AddressEntity> getAddress() {return address;}public static class AddressEntity {private String name;private String custId;private List<String> area;public void setName(String name) {this.name = name;}public void setCustId(String custId) {this.custId = custId;}public void setArea(List<String> area) {this.area = area;}public String getName() {return name;}public String getCustId() {return custId;}public List<String> getArea() {return area;}}}MainActivity.java
package com.mengle.app;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.view.Window;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.PopupWindow;import android.widget.TextView;import android.widget.Toast;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.mengle.app.Bean.AddressEntity;public class MainActivity extends Activity { private DisplayMetrics dm; private PopupWindow popWindow; private MoreAdapter moreAdapter; private ListView morelist; private TextView tv; private List<AddressEntity> mainList; private ListView mainlist; private MainAdapter mainAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); dm = new DisplayMetrics(); // 取得窗口属性 getWindowManager().getDefaultDisplay().getMetrics(dm); initModle();// 添加数据 tv = (TextView) findViewById(R.id.tv); tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { downPopwindow(); } }); } private void downPopwindow() { // showAsDropDown(View anchor);相对某个控件的位置(正左下方),无偏移 // showAsDropDown(View anchor, int x, int // y);相对某个控件的位置,有偏移;x表示相对x轴的偏移,正表示向左,负表示向右;y表示相对y轴的偏移,正是向下,负是向上; View contentView = LayoutInflater.from(this).inflate(R.layout.pop_down, null); int screenHeight = dm.heightPixels * 2 / 3; // 这里就给具体大小的数字,要不然位置不好计算 popWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, screenHeight); // popWindow.setAnimationStyle(R.style.anim);// 淡入淡出动画 // popWindow.setTouchable(false);// 是否响应touch事件 popWindow.setFocusable(true);// 是否具有获取焦点的能力 // 点击PopupWindow以外的区域,PopupWindow是否会消失。 popWindow.setBackgroundDrawable(new BitmapDrawable()); popWindow.setOutsideTouchable(true); mainlist = (ListView) contentView.findViewById(R.id.classify_mainlist); morelist = (ListView) contentView.findViewById(R.id.classify_morelist); mainAdapter = new MainAdapter(this, mainList); mainAdapter.setSelectItem(0); mainlist.setAdapter(mainAdapter); mainlist.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { List<String> lists = mainList.get(position).getArea(); initAdapter(lists); mainAdapter.setSelectItem(position); mainAdapter.notifyDataSetChanged(); } }); mainlist.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 一定要设置这个属性,否则ListView不会刷新 initAdapter(mainList.get(0).getArea()); morelist.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String s = (String) moreAdapter.getItem(position); Toast.makeText(getApplicationContext(), s, 1).show(); moreAdapter.setSelectItem(position); moreAdapter.notifyDataSetChanged(); } }); popWindow.showAsDropDown(tv); } private void initAdapter(List<String> lists) { moreAdapter = new MoreAdapter(this, lists); morelist.setAdapter(moreAdapter); moreAdapter.notifyDataSetChanged(); } private void initModle() { mainList = new ArrayList<AddressEntity>(); Gson gson = new Gson(); String json = "{\"result\":\"Y\", \"address\":[{\"name\":\"北京\",\"custId\":\"" + R.drawable.ic_category_1 + "\", \"area\":[\"东城区\",\"西城区\",\"崇文区\",\"宣武区\",\"延庆县\"]},{\"name\":\"上海\", \"custId\":\"" + R.drawable.ic_category_2 + "\",\"area\":[\"黄浦区\",\"卢湾区\",\"徐汇区\",\"长宁区\",\"静安区\",\"其他\"]}]}"; java.lang.reflect.Type type = new TypeToken<Bean>() { }.getType(); Bean b = gson.fromJson(json, type); mainList.addAll(b.getAddress()); }}activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#2A3C50" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="60dp" android:background="#FAFA00" android:drawableRight="@drawable/holo_light" android:gravity="center" android:text="二级列表" android:textSize="21sp" /></LinearLayout>pop_down.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="horizontal" > <ListView android:id="@+id/classify_mainlist" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:background="#fff" android:cacheColorHint="#00000000" android:divider="#ffebebeb" android:dividerHeight="1px" android:fastScrollEnabled="true" android:listSelector="#00000000" android:scrollbars="none" /> <ListView android:id="@+id/classify_morelist" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:background="#fff" android:cacheColorHint="#00000000" android:divider="#ffebebeb" android:dividerHeight="1px" android:fastScrollEnabled="true" android:listSelector="#00000000" android:scrollbars="none" /></LinearLayout>MainAdapter.java
package com.mengle.app;import java.util.List;import com.mengle.app.Bean.AddressEntity;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class MainAdapter extends BaseAdapter {private Context context;private List<AddressEntity> list;private int position = 0;private Holder hold;public MainAdapter(Context context, List<AddressEntity> list) {this.context = context;this.list = list;}public int getCount() {return list.size();}public Object getItem(int position) {return list.get(position);}public long getItemId(int position) {return position;}public View getView(int arg0, View view, ViewGroup viewGroup) {if (view == null) {view = View.inflate(context, R.layout.item_mainlist, null);hold = new Holder(view);view.setTag(hold);} else {hold = (Holder) view.getTag();}hold.img.setImageResource(Integer.parseInt(list.get(arg0).getCustId()));hold.txt.setText(list.get(arg0).getName());hold.layout.setBackgroundColor(0xFFEBEBEB);if (arg0 == position) {hold.layout.setBackgroundColor(0xFFFFFFFF);}return view;}public void setSelectItem(int position) {this.position = position;}public int getSelectItem() {return position;}private static class Holder {LinearLayout layout;ImageView img;TextView txt;public Holder(View view) {txt = (TextView) view.findViewById(R.id.mainitem_txt);img = (ImageView) view.findViewById(R.id.mainitem_img);layout = (LinearLayout) view.findViewById(R.id.mainitem_layout);}}}
item_mainlist.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="55dp" android:orientation="horizontal" > <LinearLayout android:id="@+id/mainitem_layout" android:layout_width="match_parent" android:layout_height="55dp" android:gravity="center_vertical" android:orientation="horizontal" > <ImageView android:id="@+id/mainitem_img" android:layout_width="32dp" android:layout_height="32dp" android:paddingLeft="10dp" android:src="@drawable/ic_category_2" /> <TextView android:id="@+id/mainitem_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:text="热门分类" android:textColor="#000000" android:textSize="18sp" /> </LinearLayout></LinearLayout>
MoreAdapter.java
package com.mengle.app;import java.util.List;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class MoreAdapter extends BaseAdapter {private Context context;private int position = 0;Holder hold;private List<String> lists;public MoreAdapter(Context context, List<String> lists) {this.context = context;this.lists = lists;}public int getCount() {return lists.size();}public Object getItem(int position) {return lists.get(position);}public long getItemId(int position) {return position;}public View getView(int arg0, View view, ViewGroup viewGroup) {if (view == null) {view = View.inflate(context, R.layout.item_morelist, null);hold = new Holder(view);view.setTag(hold);} else {hold = (Holder) view.getTag();}hold.txt.setText(lists.get(arg0).toString());hold.txt.setTextColor(0xFF666666);if (arg0 == position) {hold.txt.setTextColor(0xFFFF8C00);}return view;}public void setSelectItem(int position) {this.position = position;}private static class Holder {TextView txt;public Holder(View view) {txt = (TextView) view.findViewById(R.id.moreitem_txt);}}}item_morelist.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="55dp" android:background="#ffffffff" android:orientation="vertical" android:paddingLeft="10dp" > <TextView android:id="@+id/moreitem_txt" android:layout_width="match_parent" android:layout_height="55dp" android:gravity="center_vertical" android:text="全部分类" android:textColor="#666666" android:textSize="17sp" /></LinearLayout>
0 0
- android 二级菜单、双ListView 仿美团、购物App二级菜单
- ListView实现二级菜单
- Android 二级菜单
- Android 二级菜单最终版
- android 二级菜单实现
- 二级菜单
- 二级菜单
- 二级菜单
- 二级菜单
- 二级菜单
- 二级菜单
- 二级菜单
- 二级菜单
- 二级菜单
- android 二级listview菜单。类似QQ的好友显示
- Android中ListView实现分类二级下拉菜单的效果
- Android 二级动画弹出菜单
- Android中实现二级菜单
- Cpp--拷贝构造函数
- VC++之Office自动化开发
- Oracle解析json字符串 获取指定值自定义函数代码
- mybatis中运算符转义
- centos7防火墙
- android 二级菜单、双ListView 仿美团、购物App二级菜单
- C++中,内存5个区
- 中国剩余定理算法详解(余数互质和不互质)
- android的view的生命周期
- UIMenuController
- 正则表达式,数字(含小数)
- 表示不同文件类型的魔术数字
- Spark的standalone环境搭建(1.6.0)
- github【如何删除一个repository(仓库)】