您的位置:首页 > 其它

SlidingMenu侧拉菜单加强版

2016-01-18 20:19 218 查看

SlidingMenu<span style="font-family:宋体;">侧拉菜单加强版</span>

上次一次写了侧拉菜单,但是侧拉菜单里面却没写东西.这一次加上一些内容,

基于上次的博文来写的,所以先搞定上面那篇,才能看懂这个

这一次相比有哪些改变呢,点击左边的listview,右边的内容页内容也会改变,然后就是内容页的主题变了,内容页也采用帧布局,这里要说为什么要选帧布局,因为便于替换,



相比上次增加了一个替换帧布局内容页内容的函数,加载的加载的布局文件也变成的帧布局

MainActivity.java代码

package com.example.slidingmenu_gm;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity{
    private SlidingMenu slidingMenu;
    //继承SlidingFragmentActivity,下面的方法改为public
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.content);
		//设置关联的布局文件
		setBehindContentView(R.layout.menu_frame);
		//获取侧拉菜单
		slidingMenu =getSlidingMenu();
		//内容页的宽度大小值
		slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		//设置内容页和侧拉菜单的分割线,或者用图片
		//<shape>可以设置颜色渐变
		slidingMenu.setShadowDrawable(R.drawable.shadow);
		//设置渐变图片的宽度值
		slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
		//设置侧拉菜单的左边方向
slidingMenu.setMode(SlidingMenu.LEFT);
		//指明拖拽区域,1边缘拖拽,随意拖拽,不能拖拽
		//这里选择随意拖拽TOUCHMODE_FULLSCREEN
		//指向内容页的拖拽
		//这个是侧拉菜单区域,不要用这个,
		//不然里面的事件不能处理了slidingMenu.setTouchModeBehind(i);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
	    //左侧侧拉条目对应的fragment对象
		MenuFragment menuFragment =new MenuFragment();
	    getSupportFragmentManager()
	    .beginTransaction()
	    //需要去替换左侧侧拉帧布局的id2.fragment对象3,唯一标识
	    .replace(R.id.menu, menuFragment, "MENU")
	    .commit();
	}
	public void switchFragment(Fragment fragment){
		if(fragment!=null){
			//替换内容页对应帧布局内部内容操作
			    getSupportFragmentManager()
			    .beginTransaction()
			    //需要去替换左侧侧拉帧布局的id2.fragment对象3,唯一标识
			    .replace(R.id.content_frame, fragment, "MENU")
			    .commit();	
		}
	}
}
MenuFragment.java就是把onActivityCreated(Bundle savedInstanceState)
返回一个listView,然后给listview添加事件
package com.example.slidingmenu_gm;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MenuFragment extends Fragment {
	private View view;
	//初始化
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    }
    //xml--->view,就是Fragment要去展示的View.类似
    //activity中的setContent()
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view= View.inflate(getActivity(), R.layout.list_view, null);
		return view;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// 获取数据操作,数据填充Fragment中的onCreView返回的view
		ListView listView = (ListView) view.findViewById(R.id.list_view);
		//设置适配器
		listView.setAdapter(new ArrayAdapter<String>(
				getActivity(),
				android.R.layout.simple_list_item_1,
				android.R.id.text1,
				getData()));
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				BaseFragment fragment = null;
				switch (position) {
				case 0:fragment= new Fragment1();break;
				case 1:fragment= new Fragment2();break;
				case 2:fragment= new Fragment3();break;
				case 3:fragment= new Fragment4();break;
				case 4:fragment= new Fragment5();break;
				case 5:fragment= new Fragment6();break;
				}
//				//替换内容页对应帧布局内部内容操作
//				getActivity().getSupportFragmentManager()
//				.beginTransaction()
//				.replace(R.id.content_frame, fragment, "HOME")
//				.commit();
				switchFragment(fragment);
			}

			
		});
		super.onActivityCreated(savedInstanceState);
	}
	private List<String> getData() {
		List<String> arrayList =new ArrayList<String>();
		arrayList.add("Fragment1");
		arrayList.add("Fragment2");
		arrayList.add("Fragment3");
		arrayList.add("Fragment4");
		arrayList.add("Fragment5");
		arrayList.add("Fragment6");
		return arrayList;
	}
	private void switchFragment(BaseFragment fragment) {
    //获取MainActivity对象,才可以调用其上面的switchFragment
		if(getActivity() instanceof MainActivity){
			//getActivity()就是MainActivity或者是其子类
			((MainActivity)getActivity()).switchFragment(fragment);
		}
	}
}

Fragment1到6都是继承BaseFragment,BaseFragment又继承Fragment

看下BaseFragment的代码,没什么东西,就返回了相应类的类名

package com.example.slidingmenu_gm;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class BaseFragment extends Fragment {
  @Override
public void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
}
  @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		TextView textview =new TextView(getActivity());
		textview.setText(this.getClass().getSimpleName());
		return textview;
	}
  @Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}
  
}//总结一下写<span style="font-family:Calibri;">Fragment</span><span style="font-family:宋体;">其实就是看三个函数</span>

1. onCreate(Bundle savedInstanceState) 初始化

2. onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) Fragment要去展示的View.类似

3. onActivityCreated(Bundle savedInstanceState) 数据填充Fragment中的onCreView返回的view

运行效果截图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: