NavigationDrawer+Fragment实现侧滑菜单效果

时间:2022-11-28 01:26:22

学习了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实现侧滑菜单效果