码迷,mamicode.com
首页 > 其他好文 > 详细

二级菜单

时间:2015-11-28 13:36:56      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

  1 /*
  2 *adapter
  3 */
  4 package com.bwie.adapter;
  5 
  6 import java.util.List;
  7 
  8 import android.content.Context;
  9 import android.content.Intent;
 10 import android.sax.StartElementListener;
 11 import android.view.LayoutInflater;
 12 import android.view.View;
 13 import android.view.ViewGroup;
 14 import android.widget.AdapterView;
 15 import android.widget.AdapterView.OnItemClickListener;
 16 import android.widget.BaseExpandableListAdapter;
 17 import android.widget.ImageView;
 18 import android.widget.ListView;
 19 import android.widget.TextView;
 20 
 21 import com.bwie.bean.Cook;
 22 import com.bwie.bean.Steps;
 23 import com.bwie.test.R;
 24 import com.bwie.test.StepsActivity;
 25 import com.nostra13.universalimageloader.core.DisplayImageOptions;
 26 import com.nostra13.universalimageloader.core.ImageLoader;
 27 
 28 //适配器适配数据
 29 public class MyExAdapter extends BaseExpandableListAdapter {
 30     private List<Cook> list;
 31 
 32     private Context context;
 33     
 34     private DisplayImageOptions options;
 35 
 36     public MyExAdapter(List<Cook> list, Context context) {
 37         super();
 38         this.list = list;
 39         this.context = context;
 40         // 默认显示机器人
 41         options = new DisplayImageOptions.Builder().cacheInMemory(true)
 42                 .showImageForEmptyUri(R.drawable.ic_launcher).cacheOnDisk(true)
 43                 .build();
 44     }
 45 
 46     @Override
 47     public Object getChild(int groupPosition, int childPosition) {
 48         // TODO Auto-generated method stub
 49         return list.get(groupPosition).getSteps().get(childPosition);
 50     }
 51 
 52     @Override
 53     public long getChildId(int groupPosition, int childPosition) {
 54         // TODO Auto-generated method stub
 55         return childPosition;
 56     }
 57 //获取二级列表视图
 1 /*
 2 *初始化并适配数据
 3 */
 4 package com.bwie.test;
 5 
 6 import com.bwie.bean.Steps;
 7 import com.nostra13.universalimageloader.core.DisplayImageOptions;
 8 import com.nostra13.universalimageloader.core.ImageLoader;
 9 
10 import android.os.Bundle;
11 import android.app.Activity;
12 import android.view.Menu;
13 import android.widget.ImageView;
14 import android.widget.TextView;
15 
16 public class StepsActivity extends Activity {
17 
18     private TextView tv;
19     private ImageView iv;
20     private DisplayImageOptions options;
21 
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_steps);
26         init();
27     }
28 //初始化并适配数据
29     private void init() {
30         // TODO Auto-generated method stub
31         options = new DisplayImageOptions.Builder().cacheInMemory(true)
32                 .showImageForEmptyUri(R.drawable.ic_launcher).cacheOnDisk(true)
33                 .build();
34         tv = (TextView) findViewById(R.id.steps_tv);
35         iv = (ImageView) findViewById(R.id.steps_iv);
36 
37         Steps steps = (Steps) getIntent().getSerializableExtra("step");
38 
39         tv.setText(steps.getStep());
40 
41         ImageLoader.getInstance().displayImage(steps.getImg(), iv, options);
42     }
43 
44 }

 

 58     @Override
 59     public View getChildView(int groupPosition, int childPosition,
 60             boolean isLastChild, View convertView, ViewGroup parent) {
 61 
 62         Steps steps = list.get(groupPosition).getSteps().get(childPosition);    
 63         convertView = LayoutInflater.from(context).inflate(R.layout.lv_childview,
 64                 null);
 65         
 66         TextView tv=(TextView)convertView.findViewById(R.id.elv_tv);
 67         ImageView iv=(ImageView)convertView.findViewById(R.id.elv_iv);
 68         
 69         tv.setText(steps.getStep());
 70         
 71         ImageLoader.getInstance().displayImage(steps.getImg(), iv, options);
 72         
 73         return convertView;
 74     }
 75 
 76     @Override
 77     public int getChildrenCount(int groupPosition) {
 78         // TODO Auto-generated method stub
 79         return list.get(groupPosition).getSteps().size();
 80     }
 81 
 82     @Override
 83     public Object getGroup(int groupPosition) {
 84         // TODO Auto-generated method stub
 85         return list.get(groupPosition);
 86     }
 87 
 88     @Override
 89     public int getGroupCount() {
 90         // TODO Auto-generated method stub
 91         return list.size();
 92     }
 93 
 94     @Override
 95     public long getGroupId(int groupPosition) {
 96         // TODO Auto-generated method stub
 97         return groupPosition;
 98     }
 99 //获取一级视图列表
100     @Override
101     public View getGroupView(int groupPosition, boolean isExpanded,
102             View convertView, ViewGroup parent) {
103         // TODO Auto-generated method stub
104         TextView tv = (TextView) LayoutInflater.from(context).inflate(
105                 R.layout.elv_group, null);
106         tv.setText(list.get(groupPosition).getTitle());
107         return tv;
108     }
109 
110     @Override
111     public boolean hasStableIds() {
112         // TODO Auto-generated method stub
113         return true;
114     }
115 
116     @Override
117     public boolean isChildSelectable(int groupPosition, int childPosition) {
118         // TODO Auto-generated method stub
119         return true;
120     }
121 
122 }
 1 /*
 2 *程序入口初始化
 3 */    
 4 
 5 package com.bwie.base;
 6 
 7 import com.nostra13.universalimageloader.core.ImageLoader;
 8 import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
 9 
10 import android.app.Application;
11 
12 public class BaseApplication extends Application {
13 //程序入口初始化imageloader
14     @Override
15     public void onCreate() {
16         // TODO Auto-generated method stub
17         super.onCreate();    
18         ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(this);
19         ImageLoader.getInstance().init(configuration);
20     }
21 }
 1 /*
 2 *MainActivity
 3 */
 4 
 5 package com.bwie.test;
 6 
 7 import android.app.Activity;
 8 import android.content.Intent;
 9 import android.os.Bundle;
10 import android.view.View;
11 import android.widget.ExpandableListView;
12 import android.widget.ExpandableListView.OnChildClickListener;
13 
14 import com.bwie.adapter.MyExAdapter;
15 import com.bwie.bean.CookMenu;
16 import com.google.gson.Gson;
17 import com.lidroid.xutils.HttpUtils;
18 import com.lidroid.xutils.exception.HttpException;
19 import com.lidroid.xutils.http.ResponseInfo;
20 import com.lidroid.xutils.http.callback.RequestCallBack;
21 import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
22 
23 public class MainActivity extends Activity {
24 
25     private final static String URL = "http://apis.juhe.cn/cook/query.php?menu=%E8%A5%BF%E5%85%B0%E8%8A%B1&key=b606cae1cfcc8461aa479821181b5a25";
26     private ExpandableListView elv;
27     private HttpUtils httpUtils;
28 
29     private CookMenu cookMenu;
30 
31     @Override
32     protected void onCreate(Bundle savedInstanceState) {
33         super.onCreate(savedInstanceState);
34         setContentView(R.layout.activity_main);
35         init();
36         httpRequest();
37         addListener();
38     }
39 //添加监听
40     private void addListener() {
41         // TODO Auto-generated method stub
42         elv.setOnChildClickListener(new OnChildClickListener() {
43 
44             @Override
45             public boolean onChildClick(ExpandableListView parent, View v,
46                     int groupPosition, int childPosition, long id) {
47                 Intent data = new Intent(MainActivity.this, StepsActivity.class);
48                 data.putExtra("step",
49                         cookMenu.getResult().getData().get(groupPosition)
50                                 .getSteps().get(childPosition));
51                 startActivity(data);
52                 return false;
53             }
54         });
55     }
56 //网络请求数据
57     private void httpRequest() {
58         // TODO Auto-generated method stub
59         httpUtils.send(HttpMethod.GET, URL, new RequestCallBack<String>() {
60 
61             @Override
62             public void onFailure(HttpException arg0, String arg1) {
63                 // TODO Auto-generated method stub
64 
65             }
66 
67             @Override
68             public void onSuccess(ResponseInfo<String> arg0) {
69                 // TODO Auto-generated method stub
70                 cookMenu = new Gson().fromJson(arg0.result, CookMenu.class);
71                 elv.setAdapter(new MyExAdapter(cookMenu.getResult().getData(),
72                         MainActivity.this));
73             }
74         });
75     }
76 //初始化数据
77     private void init() {
78         // TODO Auto-generated method stub
79         httpUtils = new HttpUtils();
80         elv = (ExpandableListView) findViewById(R.id.main_elv);
81 
82     }
83 }

 

 

二级菜单

标签:

原文地址:http://www.cnblogs.com/zxwisfdlkjglkf/p/5002544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!