NavigationDrawer+Fragment实现侧滑菜单效果
学习了NavigationDrawer 官方Support包中的SlidingMenu版本,练了下手.用到了ListView中item不同的布局
以后会升级加上ViewPager和GridView实现多页面图片切换
代码:
package com.light.study.android; import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView; import com.light.study.android.MyListAdapter.RowType; public class EventItem implements Item { private final String content; public EventItem(String content){
this.content = content;
} @Override
public int getViewType() {
return RowType.LIST_ITEM.ordinal();
} @Override
public View getView(LayoutInflater inflater, View convertView) {
if(convertView==null){
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.list_content);
tv.setText(content);
return convertView;
} @Override
public boolean isClickable() {
return true;
} }
package com.light.study.android; import com.light.study.android.MyListAdapter.RowType; import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView; public class HeaderItem implements Item {
private String name; public HeaderItem(String name){
this.name = name;
} @Override
public int getViewType() {
return RowType.HEAD_ITEM.ordinal();
} @Override
public View getView(LayoutInflater inflater, View convertView) {
if(convertView==null){
convertView = inflater.inflate(R.layout.head_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.headerText);
tv.setText(name);
return convertView; } @Override
public boolean isClickable() {
return false;
} }
package com.light.study.android; import android.view.LayoutInflater;
import android.view.View; public interface Item {
public int getViewType();
public boolean isClickable();
public View getView(LayoutInflater inflater, View convertView);
}
package com.light.study.android; import java.util.List; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter; public class MyListAdapter extends ArrayAdapter<Item> {
private Context mContext;
private LayoutInflater inflater;
private List<Item> list; public MyListAdapter(Context context, List<Item> list) {
super(context, 0, list);
this.mContext = context;
this.inflater = LayoutInflater.from(mContext);
this.list = list;
} public enum RowType{
HEAD_ITEM,LIST_ITEM
} @Override
public boolean isEnabled(int position) {
return this.list.get(position).isClickable();
} @Override
public int getViewTypeCount() {
return RowType.values().length;
} @Override
public int getItemViewType(int position) {
return list.get(position).getViewType();
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
return list.get(position).getView(inflater, convertView);
} }
package com.light.study.android; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class PageFragment extends Fragment {
public final static String ITEM_POSITION_NUMBER = "item_position_num";
public PageFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View convertView = inflater.inflate(R.layout.page_fragment_layout, null);
TextView tv = (TextView) convertView.findViewById(R.id.textView);
int num = getArguments().getInt(ITEM_POSITION_NUMBER);
//選擇對應的項目
String[] dynastyList = getResources().getStringArray(R.array.list_item);
tv.setText(dynastyList[num]);
return convertView;
}
}
package com.light.study.android; import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView; public class MainActivity extends Activity {
private DrawerLayout drawLayout;
private ListView menuList;
private ActionBarDrawerToggle toggle;
private CharSequence mDrawerTitle;
private String[] mMenuTitles;
private String[] historyTitles;
private String[] musicTitles;
private String[] movieTitles;
private String[] listTitles; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initListener();
if (savedInstanceState == null) {
selectItem(0);
}
} private void init() {
mDrawerTitle = getTitle();
//历史栏
historyTitles = getResources().getStringArray(R.array.history);
//音乐栏
musicTitles = getResources().getStringArray(R.array.music);
//电影栏
movieTitles = getResources().getStringArray(R.array.movie);
//标题数组
mMenuTitles = getResources().getStringArray(R.array.title);
//每一項的標題
listTitles = getResources().getStringArray(R.array.list_item); drawLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
menuList = (ListView) findViewById(R.id.left_menu); //设置菜单阴影效果
drawLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
List<Item> list = new ArrayList<Item>();
//菜单加入历史标题和历史项
HeaderItem historyHeader = new HeaderItem(mMenuTitles[0]);
list.add(historyHeader);
for(int i =0;i<historyTitles.length;i++){
EventItem historyitem = new EventItem(historyTitles[i]);
list.add(historyitem);
} //菜单加入音乐标题和音乐项
HeaderItem musicHeader = new HeaderItem(mMenuTitles[1]);
list.add(musicHeader);
for(int i =0;i<musicTitles.length;i++){
EventItem musicItem = new EventItem(musicTitles[i]);
list.add(musicItem);
} //菜单加入电影标题和电影项
HeaderItem movieHeader = new HeaderItem(mMenuTitles[2]);
list.add(movieHeader);
for(int i =0;i<movieTitles.length;i++){
EventItem movieItem = new EventItem(movieTitles[i]);
list.add(movieItem);
} MyListAdapter adapter = new MyListAdapter(this, list);
menuList.setAdapter(adapter); // enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true); toggle = new ActionBarDrawerToggle(this,
drawLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close){
public void onDrawerClosed(View view) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
} public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
}; drawLayout.setDrawerListener(toggle); } private void initListener() {
//菜单单击事件监听器
menuList.setOnItemClickListener(new DrawerItemClickListener()); } /* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("Light", "position:"+position);
selectItem(position);
}
} private void selectItem(int position) {
// update the main content by replacing fragments
PageFragment fragment = new PageFragment();
//将当前选择的项传递到Fragment
Bundle args = new Bundle();
args.putInt(PageFragment.ITEM_POSITION_NUMBER, position);
fragment.setArguments(args); FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); drawLayout.closeDrawer(menuList);
// update selected item and title, then close the drawer
menuList.setItemChecked(position, true);
//注意这里改变的是ActionBar的标题
getActionBar().setTitle(listTitles[position]);
} @Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
} @Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
toggle.onConfigurationChanged(newConfig);
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
} }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- The main content view-->
<FrameLayout android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <!-- The navigation drawer -->
<ListView android:id="@+id/left_menu"
android:layout_height="match_parent"
android:layout_width="240dp"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
head_item.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/headerText"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#757678"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:textColor="#f5c227"
android:clickable="false"
/> </LinearLayout>
list_item.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/list_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dip"
android:gravity="center"
android:linksClickable="false"
android:longClickable="false"
android:paddingBottom="1dip"
android:paddingTop="1dip"
android:textColor="#6d6d6d"
android:textSize="17sp" /> </LinearLayout>
page_fragment_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/> </FrameLayout>
arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="history">
<item >三国</item>
<item >楚汉</item>
<item >春秋</item>
<item >战国</item>
</string-array>
<string-array name="music">
<item >爵士</item>
<item >古典</item>
<item >现代</item>
<item >民谣</item>
</string-array>
<string-array name="movie">
<item >悬疑</item>
<item >爱情</item>
<item >历史</item>
<item >恐怖</item>
</string-array>
<string-array name="title">
<item >历史</item>
<item >音樂</item>
<item >电影</item>
</string-array>
<string-array name="list_item">
<item >歷史</item>
<item >三国</item>
<item >楚汉</item>
<item >春秋</item>
<item >战国</item>
<item >音樂</item>
<item >爵士</item>
<item >古典</item>
<item >现代</item>
<item >民谣</item>
<item >電影</item>
<item >悬疑</item>
<item >爱情</item>
<item >历史</item>
<item >恐怖</item>
</string-array>
</resources>
效果:
NavigationDrawer+Fragment实现侧滑菜单效果的更多相关文章
- Android 自定义View修炼-仿QQ5.0 的侧滑菜单效果的实现
有一段时间没有写博客了,最近比较忙,没什么时间写,刚好今天有点时间, 我就分享下,侧滑菜单的实现原理,一般android侧滑的实现原理和步骤如下:(源码下载在下面最后给出哈) 1.使用ViewGrou ...
- android组件之DrawerLayout(抽屉导航)-- 侧滑菜单效果
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/41696291 一. 介绍 导航抽屉显示在屏幕的最左侧,默认情况下是隐藏的,当用 ...
- SlidingMenu+ViewPager实现侧滑菜单效果
先简单介绍下SlidingMenu和ViewPager. ViewPager就是一个官方提供的多页面滑动组件,需要一个适配器来构建多个页面. 先来看看ViewPager对应的基本适配器PageAdap ...
- SlidingMenu 侧滑菜单的用法
很多APP都有侧滑菜单的功能,部分APP左右都是侧滑菜单~SlidingMenu 这个开源项目可以很好帮助我们实现侧滑功能,将侧滑菜单引入项目的方式很多中,先通过例子介绍各种引入方式,然后给大家展示个 ...
- Android DrawerLayout 高仿QQ5.2双向侧滑菜单
1.概述 之前写了一个Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭 ,恰逢QQ5.2又加了一个右侧菜单,刚好看了下DrawerLayout,一方面官方的东西,我都比较感兴趣:另一方面 ...
- android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)
ActionBar(V7)的添加非常简单,只需要在AndroidManifest.xml中指定Application或Activity的theme是Theme.Holo或其子类就可以了,在Androi ...
- android L 新控件侧滑菜单DrawerLayout 使用教程
介绍 drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产 ...
- 【转】android官方侧滑菜单DrawerLayout详解
原文网址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0925/1713.html drawerLayout是Support ...
- android官方侧滑菜单DrawerLayout详解
drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物.d ...
随机推荐
- magento 产品列表排序、分页功能
我们以 catalog_category_layered 控制器为例说明 在catalog.xml 找到catalog_category_layered配置段 <catalog_category ...
- windows2008 x86 安装 32位oracle
1.windows 2008 升级到sp2补丁 下载地址 : http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=15278 2. ...
- Java中ArrayList类详解
1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...
- 《ext江湖》第8章继承-代码片段
创建Animal对象 <html> <head> <title>11</title> <META HTTP-EQUIV="Content ...
- Codevs 3729 飞扬的小鸟
飞扬的小鸟 标签 动态规划 NOIp提高组 2014 难度 提高+/省选- 题目描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小 ...
- 九度OJ 1352 和为S的两个数字
题目地址:http://ac.jobdu.com/problem.php?pid=1352 题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和 ...
- 百度UEditor(富文本编辑器)的基础用法
百度的这个编辑器挺强大的,这里只是用他的文本功能,没有介绍上传图片视频的. 我用是的SSH来写的项目. 1. 把下载的UEditor(ueditor1_4_3_1-utf8-jsp)解压后全部复制到W ...
- 浅析JAVA设计模式(三)
4.接口隔离原则: ISP(Interface Segregation Principle) 客户端不应该依赖它不需要的接口,或者说类的依赖的关系应该建立在最小的接口上.举个例子,直接上代码: 1 ...
- 清理SQL多余登录信息
服务器列表.登陆帐户.密码等信息都记录在 %AppData%\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin (2008)%A ...
- phpcms V9 添加模块(转)
转自:http://www.cnblogs.com/Braveliu/p/5101345.html 为phpcms创建一个模块的开发流程 [1]创建模块目录 通过前面的学习,我们已经知道phpcms ...