首页 > 代码库 > Eoe客户端源码分析---SlidingMenu的使用
Eoe客户端源码分析---SlidingMenu的使用
Eoe客户端源码分析及代码注释
使用滑动菜单SlidingMenu,单击滑动菜单的不同选项,可以通过ViewPager和PagerIndicator显示对应的数据内容。
0 BaseSlidingFragmentActivity.java
主要函数:
(1)showMenu()
/** * Opens the menu and shows the menu view.*/
public void showMenu() {
showMenu(true);
}
(2)showContent()
/**Closes the menu and shows the above view. */
public void showContent() {
showContent(true);
}
(3)toggle()
/**Toggle the SlidingMenu. If it is open, it will be closed, and viceversa.*/
public void toggle() {
toggle(true);
}
/**
* Toggle the SlidingMenu. If it is open, itwill be closed, and vice versa.
*
* @param animate true to animate thetransition, false to ignore animation
*/
public void toggle(booleananimate) {
if (isMenuShowing()) {
showContent(animate);
} else {
showMenu(animate);
}
}
(4)
//设置SlidingMenu使用的布局
publicvoid setBehindContentView(int id) {
setBehindContentView(getLayoutInflater().inflate(id,null));
}
public void setBehindContentView(View v) {
setBehindContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
}
public void setBehindContentView(View v, LayoutParams params) {
mHelper.setBehindContentView(v, params);
}
//获取与该Acitivity相关的SlidingMenu对象
public SlidingMenu getSlidingMenu() {
returnmHelper.getSlidingMenu();
}
1. behind_sldingmenu.xml 滑动菜单的部分页面布局文件
滑动菜单主要由一个标题布局(@layout/behinf_title)、菜单选项列表布局和两个自定义图形按钮等控件元素组成
<FrameLayoutxmlns: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="#dadada"
android:orientation="vertical">
<!--sliding menu layout -->
<includelayout="@layout/behind_title"/>
<ListView
android:id="@+id/behind_list_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_marginBottom="@dimen/list_margin_height"
android:layout_marginTop="@dimen/title_height"
android:divider="@drawable/dis_behind_side"
android:listSelector="#0fff"
android:cacheColorHint="#0000">
</ListView>
</FrameLayout>
2. 初始化滑动菜单
private SlidingMenu sm;
// [start]初始化函数
private void initSlidingMenu() {
//设置滑动菜单的布局文件
setBehindContentView(R.layout.behind_slidingmenu);
// 获取滑动菜单对象并设置外观属性customize the SlidingMenu
sm = getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
sm.setShadowDrawable(R.drawable.slidingmenu_shadow);
//sm.setShadowWidth(20);
sm.setBehindScrollScale(0);
}
3. 显示菜单:
(1)单击主界面左上角的LinearLayout控件
// fromabove_title.xml -- > pop up slidingmenu (showMenu() )
llGoHome = (LinearLayout) findViewById(R.id.Linear_above_toHome);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.Linear_above_toHome:
showMenu();
break;
}
}
(2)单击手机的菜单选项
@Override
public boolean onKeyDown(int keyCode, KeyEventevent) {
else if (keyCode == KeyEvent.KEYCODE_MENU) {
if (sm.isMenuShowing()) {
toggle();
} else {
showMenu();
}
}
}
4. 设置填充滑动菜单中列表项数据的适配器
4.1 behind_list_show.xml (每一个列表项的布局)
每一个列表项(子菜单选项)由一个ImageView(子菜单图标)和TextView(子菜单名称)组成
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/behind_list_height"
android:orientation="horizontal"
android:background="@drawable/back_behind_listitem_style">
<ImageView
android:id="@+id/imageview_behind_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/dis_menu_blog"
android:scaleType="fitCenter"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/textview_behind_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#666"
android:textSize="@dimen/behind_list_text_size"/>
</LinearLayout>
4.2定义getData函数获取填充列表项的数据List
private List<Map<String, Object>> getData() {
List<Map<String, Object>>list = new ArrayList<Map<String, Object>>();
//社区精选
Map<String, Object> map = new HashMap<String,Object>();
map.put(LIST_TEXT, getResources().getString(R.string.menuGood));
map.put(LIST_IMAGEVIEW, R.drawable.dis_menu_handpick);
list.add(map);
//新闻资讯
map = new HashMap<String, Object>();
map.put(LIST_TEXT, getResources().getString(R.string.menuNews));
map.put(LIST_IMAGEVIEW, R.drawable.dis_menu_news);
list.add(map);
//学习教程
map = new HashMap<String, Object>();
map.put(LIST_TEXT, getResources().getString(R.string.menuStudio));
map.put(LIST_IMAGEVIEW, R.drawable.dis_menu_studio);
list.add(map);
//社区博客
map = new HashMap<String, Object>();
map.put(LIST_TEXT, getResources().getString(R.string.menuBlog));
map.put(LIST_IMAGEVIEW, R.drawable.dis_menu_blog);
list.add(map);
return list;
}
4.3定义列表项数据填充适配器
SimpleAdapter lvAdapter = new SimpleAdapter(this, getData(),
R.layout.behind_list_show,new String[]{LIST_TEXT,
LIST_IMAGEVIEW},
new int[]{R.id.textview_behind_title,
R.id.imageview_behind_icon}) {
@Override
publicView getView(int position, View convertView, ViewGroupparent) {
View view = super.getView(position,convertView, parent);
//如果是当前选中的子菜单项
if (position ==mTag) {
//设置新的背景图片 标识该菜单选项被选中
view.setBackgroundResource(R.drawable.back_behind_list);
lvTitle.setTag(view);//绑定当前选中的子菜单选项到lvTitle上
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
return view;
}
};
5. 单击滑动菜单中的列表项 启动不同的ViewPager
(1)NavigationModel 类的定义
package cn.eoe.app.entity;
public class NavigationModel {
private Stringname;
//作为唯一标识符 newsblog wiki 方便于每个页面请求相对应的地址
private Stringtags;
public NavigationModel(String name1,String tags1){
this.name = name1;
this.tags = tags1;
}
}
(2)创建NavigationModel 对象,并添加四个子菜单对应的数据(name,tag)
private List<NavigationModel> navs;
private void initNav() {
navs = new ArrayList<NavigationModel>();
NavigationModel nav1 =new NavigationModel(getResources().getString(
R.string.menuGood),"");
NavigationModel nav2 =new NavigationModel(getResources().getString(
R.string.menuNews), Constants.TAGS.NEWS_TAG);
NavigationModel nav3 =new NavigationModel(getResources().getString(
R.string.menuStudio),Constants.TAGS.WIKI_TAG);
NavigationModel nav4 =new NavigationModel(getResources().getString(
R.string.menuBlog),Constants.TAGS.BLOG_TAG);
Collections.addAll(navs, nav1, nav2, nav3, nav4);
}
(3)初始化列表项数据
MainActivity.java
initialListView(){}
lvAdapter = new SimpleAdapter(this, getData(),
R.layout.behind_list_show,new String[]{LIST_TEXT,
LIST_IMAGEVIEW},
new int[]{R.id.textview_behind_title,
R.id.imageview_behind_icon});
lvTitle.setAdapter(lvAdapter);
(4)列表项单击事件监听函数
lvTitle.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view,
int position,long id) {
NavigationModel navModel = navs.get(position);
mAboveTitle.setText(navModel.getName());
current_page = navModel.getTags();
if (lvTitle.getTag() !=null) {
if (lvTitle.getTag() == view) {
//如果本次单击的子菜单选项(view)和上一次选择的子菜单选项(lvTitle.getTag())相同,则直接显示当前子菜单项对应的内容
MainActivity.this.showContent();
return;
}
//若果不相同,重新将原来菜单选项的背景色改为透明
((View) lvTitle.getTag())
.setBackgroundColor(Color.TRANSPARENT);
}
lvTitle.setTag(view);//重新绑定新的子菜单选项到lvTitle上
//设置新的背景图片 标识该菜单选项被选中
view.setBackgroundResource(R.drawable.back_behind_list);
imgQuery.setVisibility(View.VISIBLE);
//根据选择的不同子菜单选项执行不同的异步任务,显示对应的数据内容
switch (position) {
case 0:
imgQuery.setVisibility(View.GONE);
newMyTask().execute(topDao);
break;
case 1:
newMyTask().execute(newsDao);
break;
case 2:
newMyTask().execute(wikiDao);
break;
case 3:
new MyTask().execute(blogsDao);
break;
}
}
});