android slidingmenu滑動菜單(完全版)


在網上看了很多關於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

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com