有关侧滑菜单的使用,GitHub上的源码就已经很好,今天参考着例子自己写了一个Demo:
首先我们要明白这样的侧滑菜单的实现的过程,首先必须有一个Activity来承载所要转换的Fragment
,而Fragment需要我们首页,也就是HomeFragment,然后就是左右的Fragment:
对于XML:Activity
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EEB422"
android:gravity="left"
android:orientation="vertical" >
<ImageView
android:id="@+id/im_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:src="@drawable/homeleft" />
</LinearLayout>
<FrameLayout
android:id="@+id/homefragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
</FrameLayout>
</LinearLayout>
这里面需要使用Framelayout的布局去实现。
左右的Fragment 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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
可以写做一样的布局,Demo嘛有效果就好了。
对于HomeFragment的布局,这个自己定义吧,都一样的。
然后我们看Activity:
public class MainActivity extends SlidingFragmentActivity implements
OnClickListener {
private SlidingMenu menu;
private FragmentManager fragmentManager;
private ImageView left_im;// 左侧按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();// 实例化
// SetMenu();// 设置slidingmenu属性
setLeft_right();
}
需要我们把Activity去继承SlidingFragment,然后在oncreate中去实例化一些控件和设置Slidingmenu的属性:
public void init() {
left_im = (ImageView) findViewById(R.id.im_left);
left_im.setOnClickListener(this);
fragmentManager = getSupportFragmentManager();
}
private void setLeft_right() {
// 设置滑动菜单的属性值
menu = getSlidingMenu();// 初始化
// 如果只显示左侧菜单就是用LEFT,右侧就RIGHT,左右都支持就LEFT_RIGHT
menu.setShadowDrawable(R.drawable.shadow);// 设置阴影的图片资源
menu.setShadowWidthRes(R.dimen.slidingmenu_offset);// 设置阴影图片的宽度
// sm.setBehindWidth(200);//设置菜单的宽
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);// SlidingMenu划出时主页面显示的剩余宽度
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);// 设置滑动的区域
setBehindContentView(R.layout.fragment_left);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu, new LeftFragment()).commit();
menu.setMode(SlidingMenu.LEFT_RIGHT);// 设置菜单滑动模式,菜单是出现在左侧还是右侧,还是左右两侧都有
// SlidingMenu可以同时支持划出左右两侧的菜单,互不冲突,而且动画优美,体验良好。
// 设置左右侧都有
// 此时要再次添加布局菜单,上一个为左侧,这个为右侧
menu.setSecondaryMenu(R.layout.right_item);
getSupportFragmentManager().beginTransaction()
.replace(R.id.right, new RightFragment()).commit();
menu.setSecondaryShadowDrawable(R.drawable.shadowright);
// 设置滑动菜单的视图界面
menu.setMenu(R.layout.menu);
fragmentManager.beginTransaction()
.replace(R.id.homefragment, new HomeFragment()).commit();
}
对于Slidingmenu的属性网上很多的介绍,根据项目需求去设置,但是这里说一下做的时候遇到的一个问题,我们先去设置左侧的Fragment,
setBehindContentView
用这个方法,这里需要写一个menu的布局来(FrameLayout),而不是直接使用的左侧的布局的Id,如果使用的话,就是出现左侧的布局加载和右侧的一样的结果,而<pre name="code" class="java">setSecondaryMenu右侧的布局使用这个方法来加载。这样基本的效果已经出来了,但是我们还需要去实现左右侧按钮点击的效果:
有这样的一个方法:
<pre name="code" class="html">/**
* 设置点击事件
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.home_im1:
menu.showMenu();
break;
case R.id.home_im2:
menu.showSecondaryMenu(false);//设置右边点击事件
break;
}
}
这样可以实现左右侧滑而不互不影响,如果只是左侧的话,只需要setMode的方法设置左侧侧滑就OK了,而属性的设置的时候就不需要去设置右侧的布局了。
当然菜单也只是作为一个导航使用,点击导航中的数据还需我们去返回Activity中:
左侧:
<pre name="code" class="java">import com.example.slidingmenu729.MainActivity;
import com.example.slidingmenu729.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class LeftFragment extends Fragment {
private String[] arry = { "中南海", "故宫", "长城", "后海", "颐和园", "天安门", "清凉谷" ,"111111","111111","111111","111111","111111","111111","111111","111111","111111","111111","111111"};
private ListView listView;
private Button btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left, container, false);
listView = (ListView) view.findViewById(R.id.listView1);
btn = (Button) view.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (getActivity() instanceof MainActivity) {
MainActivity activity = (MainActivity) getActivity();
activity.menuItemClicked(0000);
}
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arry);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (getActivity() instanceof MainActivity) {
MainActivity activity = (MainActivity) getActivity();
activity.menuItemClicked(position);
}
}
});
return view;
}
}
Activity中:
<pre name="code" class="java">/**
* 点击返回键关闭菜单
*/
@Override
public void onBackPressed() {
// 点击返回键关闭滑动菜单
if (menu.isMenuShowing()) {
menu.showContent();
} else {
super.onBackPressed();
}
}
public void menuItemClicked(int position) {
menu.showContent();// 点击关闭
if (position == 0000) {
Toast.makeText(getApplicationContext(), "再次回到首页~~~~~", 0).show();
fragmentManager.beginTransaction()
.replace(R.id.homefragment, new HomeFragment()).commit();
} else {
Toast.makeText(getApplicationContext(), "ListVew数据的Fragment切换", 0)
.show();
fragmentManager.beginTransaction()
.replace(R.id.homefragment, new OtherFragment()).commit();
}
}
这样的就是实现了左右侧滑的效果了,如何里面有些资源,可以去GItHub下载就好,只是自己做的时候用的一些东西,会出现处理不好的地方。希望对自己对别人有所帮助。