标签:
效果图:
一般新闻客户端都用到了SlidingMenu+Fragment+ViewPage这样一个综合的应用使得界面效果比较美观大方
好的我们一步步来实现上面的效果,首先是滑动菜单效果。在这里我们引入一个框架库,专门用于实现滑动菜单的开发库,通过这个库我们能很方便的制作各种滑动效果。
1、新建一个工程,然后将SlidingMenu-master库引入到我们的工程项目中,引入方法如下图所示。
注意:引入这个库后要把原工程中的android-support-v4.jar 架包删掉否则编译会出错,原因是引入的库中已经有了该架包,如果不删除原工程的这会产生冲突。
2、在MainActivity中继承SlidingFragmentActivity
<span style="font-size:18px;">public class MainActivity extends SlidingFragmentActivity { private SlidingMenu sm; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setBehindContentView(R.layout.menu); setContentView(R.layout.content); // Fragment1 f = new Fragment1(); // // getSupportFragmentManager().beginTransaction().replace(R.id.content, f) // .commit(); sm = getSlidingMenu(); // 2 设置滑动菜单是在左边出来还是右边出来 // 参数可以设置左边LEFT,也可以设置右边RIGHT ,还能设置左右LEFT_RIGHT sm.setMode(SlidingMenu.LEFT); // 3 设置滑动菜单出来之后,内容页,显示的剩余宽度 sm.setBehindOffsetRes(R.dimen.slidingmenu_offset); // 4 设置滑动菜单的阴影 设置阴影,阴影需要在开始的时候,特别暗,慢慢的变淡 sm.setShadowDrawable(R.drawable.shadow); // 5 设置阴影的宽度 sm.setShadowWidth(R.dimen.shadow_width); // 6 设置滑动菜单的范围 // 第一个参数 SlidingMenu.TOUCHMODE_FULLSCREEN 可以全屏滑动 // 第二个参数 SlidingMenu.TOUCHMODE_MARGIN 只能在边沿滑动 // 第三个参数 SlidingMenu.TOUCHMODE_NONE 不能滑动 sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); // 创建fragment MenuFragment menuFragment = new MenuFragment(); // 获取fragment的管理者 getSupportFragmentManager() // 开启事物 .beginTransaction() // 替换 .replace(R.id.menu_layout, menuFragment, "Menu") // 提交 .commit(); // 向右滑动 // sm.setSecondaryMenu(R.layout.right_view); // sm.setSecondaryShadowDrawable(R.drawable.shadowright); // RightMenuFragment rightMenuFragment = new RightMenuFragment(); // getSupportFragmentManager().beginTransaction().replace(R.id.right_view, // rightMenuFragment).commit(); HomeFragment homeFragment = new HomeFragment(); getSupportFragmentManager().beginTransaction() .replace(R.id.content, homeFragment, "home").commit(); } // 方法回调 public void switchFragment(Fragment f) { getSupportFragmentManager().beginTransaction().replace(R.id.content, f) .commit(); // 自动切换 sm.toggle(); } }</span>
MenuFragment.java,,通过它来切换界面
<span style="font-size:18px;">@SuppressLint("NewApi") public class MenuFragment extends Fragment implements OnItemClickListener { private View view; private static final String tag = "MenuFragment"; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Log.i(tag, "oncreate"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(tag, "onCreateView"); view = inflater.inflate(R.layout.list_view, null); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); Log.i(tag, "onActivityCreated"); ListView list = (ListView) view.findViewById(R.id.list_view); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, initdata()); list.setAdapter(adapter); list.setOnItemClickListener(this); } public List<String> initdata() { List<String> listdata = new ArrayList<String>(); listdata.add("Fragment1"); listdata.add("fragment2"); listdata.add("Fragment3"); return listdata; } @Override public void onItemClick(AdapterView<?> parent, View view, int postion, long id) { Fragment f = null; switch (postion) { case 0: f = new Fragment1(); break; case 1: f = new Fragment2(); break; case 2: f = new Fragment3(); break; default: break; } switchFragment(f); } public void switchFragment(Fragment f) { if (f != null) { if (getActivity() instanceof MainActivity) { ((MainActivity) getActivity()).switchFragment(f); } } } }</span>
<span style="font-size:18px;">public class Fragment1 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) { // TODO Auto-generated method stub TextView tv = new TextView(getActivity()); tv.setText(Fragment1.class.getSimpleName()); return tv; } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); } }</span>
界面 menu
<span style="font-size:18px;"><?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:id="@+id/menu_layout"> </FrameLayout></span>
<span style="font-size:18px;"><?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:id="@+id/content" android:orientation="vertical" > </FrameLayout></span>
标签:
原文地址:http://blog.csdn.net/u010529455/article/details/42264499