在网上看了很多关于slidingmenu的例子,都不是很完全,自己写了一个,直接粘贴代码 就能运行,如果版本关系 还可以直接下载资源
BaseActivity设置slidingmenu的代码:
package com.activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import com.example.textslidingmenu.R;
import com.fragment.MenuFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
public class BaseActivity extends SlidingFragmentActivity {
private Fragment mContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置主界面视图
setContentView(R.layout.layout_main);
// 这是滑动菜单视图
setBehindContentView(R.layout.layout_left);
getSupportFragmentManager().beginTransaction()
.replace(R.id.left, new MenuFragment()).commit();
SlidingMenu sm = getSlidingMenu();
// 滑动菜单的方向
sm.setMode(SlidingMenu.LEFT);
// 阴影的宽度
sm.setShadowWidth(R.dimen.yinying);
// 背景
sm.setShadowDrawable(R.drawable.shadow_left);
// 菜单宽度
sm.setBehindOffsetRes(R.dimen.caidan);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
public void switchContent(Fragment fragment) {
mContent = fragment;
getSupportFragmentManager().beginTransaction()
.replace(R.id.content, fragment).commit();
getSlidingMenu().showContent();
}
}
MenuFragment菜单代码:
package com.fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.activity.BaseActivity;
import com.example.textslidingmenu.R;
public class MenuFragment extends ListFragment {
private boolean[] isFocused;
private int whichClick = -1;
private SampleAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_listview, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new SampleAdapter(getActivity());
adapter.add(new SampleItem(R.string.news, R.drawable.ic_menu_news,
R.string.news_en));
adapter.add(new SampleItem(R.string.news, R.drawable.ic_menu_news,
R.string.news_en));
adapter.add(new SampleItem(R.string.news, R.drawable.ic_menu_news,
R.string.news_en));
setListAdapter(adapter);
}
private class SampleItem {
public int tagRes;
public int iconRes;
public int tagResEn;
public SampleItem(int tagRes, int iconRes, int tagResEn) {
this.tagRes = tagRes;
this.iconRes = iconRes;
this.tagResEn = tagResEn;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
isFocused = new boolean[11];
for (int i = 0; i < 2; i++) {
isFocused[i] = false;
}
isFocused[0] = true;
}
public void changeListItemBg(int position) {
isFocused[whichClick == -1 ? 0 : whichClick] = false;
whichClick = position;
isFocused[position] = true;
notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.menu_listview_item, null);
}
ImageView icon = (ImageView) convertView
.findViewById(R.id.menu_listview_item_icon);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView
.findViewById(R.id.menu_listview_item_title);
title.setText(getItem(position).tagRes);
TextView titleEn = (TextView) convertView
.findViewById(R.id.menu_listview_item_title_en);
titleEn.setText(getItem(position).tagResEn);
convertView
.setBackgroundResource(isFocused[position] ? R.color.baise
: R.color.lvse);
return convertView;
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
adapter.changeListItemBg(position);
Fragment newContent = null;
Bundle bundle = new Bundle();
switch (position) {
case 0:
newContent = new InfoFragment();
break;
case 1:
newContent = new InfoFragment();
break;
case 2:
newContent = new InfoFragment();
break;
}
if (newContent != null)
switchFragment(newContent);
}
private void switchFragment(Fragment fragment) {
if (getActivity() == null)
return;
if (getActivity() instanceof BaseActivity) {
BaseActivity mActivity = (BaseActivity) getActivity();
mActivity.switchContent(fragment);
}
}
}
InfoFragment点击每一项对应的fragment代码:
package com.fragment;
import com.example.textslidingmenu.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class InfoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_main, null);
return view;
}
}
colors.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="baise">#ffffff</color>
<color name="lvse">#00ff20</color>
<color name="heise">#000000</color>
</resources>
dimens.xml代码:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="yinying">10dip</dimen>
<dimen name="caidan">180dip</dimen>
</resources>
strings.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TextSlidingMenu</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="news">新闻</string>
<string name="news_en">news</string>
</resources>
layout_left.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
layout_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
main_listview.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:listSelector="@android:color/transparent" />
menu_listview_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="wrap_content"
android:gravity="center_vertical"
android:background="@drawable/bg_menu_listview_item"
android:orientation="horizontal" >
<ImageView
android:id="@+id/menu_listview_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:padding="10dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical" >
<TextView
android:id="@+id/menu_listview_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/baise"
android:textAppearance="@android:style/TextAppearance.Medium" />
<TextView
android:id="@+id/menu_listview_item_title_en"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/baise"
android:textAppearance="@android:style/TextAppearance.Small" />
</LinearLayout>
</LinearLayout>
bg_menu_listview_item.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/lvse" android:state_pressed="false"/>
<item android:drawable="@color/heise" android:state_pressed="true"/>
</selector>
shadow_left.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:centerColor="#11000000"
android:endColor="#33000000"
android:startColor="#00000000" />
</shape>
以上就是全部代码,
当然 在一定要先导入slidingmenu的jar包,然后在把自己工程里的android-support-v4.jar包删掉,否则会报错,因为在slidingmenu里面已经有了一个,会起冲突。
源代码 地址:
http://download.csdn.net/detail/u011301387/7649789
BaseActivity设置slidingmenu的代码:
package com.activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import com.example.textslidingmenu.R;
import com.fragment.MenuFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
public class BaseActivity extends SlidingFragmentActivity {
private Fragment mContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置主界面视图
setContentView(R.layout.layout_main);
// 这是滑动菜单视图
setBehindContentView(R.layout.layout_left);
getSupportFragmentManager().beginTransaction()
.replace(R.id.left, new MenuFragment()).commit();
SlidingMenu sm = getSlidingMenu();
// 滑动菜单的方向
sm.setMode(SlidingMenu.LEFT);
// 阴影的宽度
sm.setShadowWidth(R.dimen.yinying);
// 背景
sm.setShadowDrawable(R.drawable.shadow_left);
// 菜单宽度
sm.setBehindOffsetRes(R.dimen.caidan);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
public void switchContent(Fragment fragment) {
mContent = fragment;
getSupportFragmentManager().beginTransaction()
.replace(R.id.content, fragment).commit();
getSlidingMenu().showContent();
}
}
MenuFragment菜单代码:
package com.fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.activity.BaseActivity;
import com.example.textslidingmenu.R;
public class MenuFragment extends ListFragment {
private boolean[] isFocused;
private int whichClick = -1;
private SampleAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_listview, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new SampleAdapter(getActivity());
adapter.add(new SampleItem(R.string.news, R.drawable.ic_menu_news,
R.string.news_en));
adapter.add(new SampleItem(R.string.news, R.drawable.ic_menu_news,
R.string.news_en));
adapter.add(new SampleItem(R.string.news, R.drawable.ic_menu_news,
R.string.news_en));
setListAdapter(adapter);
}
private class SampleItem {
public int tagRes;
public int iconRes;
public int tagResEn;
public SampleItem(int tagRes, int iconRes, int tagResEn) {
this.tagRes = tagRes;
this.iconRes = iconRes;
this.tagResEn = tagResEn;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
isFocused = new boolean[11];
for (int i = 0; i < 2; i++) {
isFocused[i] = false;
}
isFocused[0] = true;
}
public void changeListItemBg(int position) {
isFocused[whichClick == -1 ? 0 : whichClick] = false;
whichClick = position;
isFocused[position] = true;
notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.menu_listview_item, null);
}
ImageView icon = (ImageView) convertView
.findViewById(R.id.menu_listview_item_icon);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView
.findViewById(R.id.menu_listview_item_title);
title.setText(getItem(position).tagRes);
TextView titleEn = (TextView) convertView
.findViewById(R.id.menu_listview_item_title_en);
titleEn.setText(getItem(position).tagResEn);
convertView
.setBackgroundResource(isFocused[position] ? R.color.baise
: R.color.lvse);
return convertView;
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
adapter.changeListItemBg(position);
Fragment newContent = null;
Bundle bundle = new Bundle();
switch (position) {
case 0:
newContent = new InfoFragment();
break;
case 1:
newContent = new InfoFragment();
break;
case 2:
newContent = new InfoFragment();
break;
}
if (newContent != null)
switchFragment(newContent);
}
private void switchFragment(Fragment fragment) {
if (getActivity() == null)
return;
if (getActivity() instanceof BaseActivity) {
BaseActivity mActivity = (BaseActivity) getActivity();
mActivity.switchContent(fragment);
}
}
}
InfoFragment点击每一项对应的fragment代码:
package com.fragment;
import com.example.textslidingmenu.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class InfoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_main, null);
return view;
}
}
colors.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="baise">#ffffff</color>
<color name="lvse">#00ff20</color>
<color name="heise">#000000</color>
</resources>
dimens.xml代码:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="yinying">10dip</dimen>
<dimen name="caidan">180dip</dimen>
</resources>
strings.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TextSlidingMenu</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="news">新闻</string>
<string name="news_en">news</string>
</resources>
layout_left.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
layout_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
main_listview.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:listSelector="@android:color/transparent" />
menu_listview_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="wrap_content"
android:gravity="center_vertical"
android:background="@drawable/bg_menu_listview_item"
android:orientation="horizontal" >
<ImageView
android:id="@+id/menu_listview_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:padding="10dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical" >
<TextView
android:id="@+id/menu_listview_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/baise"
android:textAppearance="@android:style/TextAppearance.Medium" />
<TextView
android:id="@+id/menu_listview_item_title_en"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/baise"
android:textAppearance="@android:style/TextAppearance.Small" />
</LinearLayout>
</LinearLayout>
bg_menu_listview_item.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/lvse" android:state_pressed="false"/>
<item android:drawable="@color/heise" android:state_pressed="true"/>
</selector>
shadow_left.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:centerColor="#11000000"
android:endColor="#33000000"
android:startColor="#00000000" />
</shape>
以上就是全部代码,
当然 在一定要先导入slidingmenu的jar包,然后在把自己工程里的android-support-v4.jar包删掉,否则会报错,因为在slidingmenu里面已经有了一个,会起冲突。
源代码 地址:
http://download.csdn.net/detail/u011301387/7649789