AndroidUI--SlidingMenu使用例子

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

               

AndroidUI--SlidingMenu使用例子


2013年12月3日 开源项目SlidingMenu的使用学习


最近想学习有趣的UI,也想把这些UI整合到自己的项目当中,之前就已经了解到SlidingMenu这个开源控件,用过印象笔记和唱吧的童鞋可能就会看到过这种滑动菜单的效果。以下我也是通过学习其他大牛,然后自己稍微做了下Demo让自己熟悉SlidingMenu这个开源控件的使用,发现并太复杂,当然直接拿来用是没有太多问题,但要弄懂这个控件的效果实现还是不太容易,毕竟是大牛做出来的东西嘛。

我在网上找了很久,发现下面这个家伙写得不错,在这里也推荐一下

http://blog.csdn.net/yangyu20121224/article/details/9255829



他讲的很详细,让我写也不一定能写得像他那样,所以我就不讲了,我这里就是借助他提供的项目,稍微做了个Demo.

关键操作

把slidingmenu_library这个项目import到工作区当中去,然后将作为library导入到新建的项目当中去


项目结构效果图如下:





项目运行效果图:






源码:

/2013.12.2_SlidingMenu_for_weibo/src/com/wwj/slidingmenu/demo/MainActivity.java

package com.wwj.slidingmenu.demo;import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends SlidingFragmentActivity @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  // 设置标题栏的标题  setTitle("新浪微博客户端");  // 设置是否能够使用ActionBar来滑动  setSlidingActionBarEnabled(true);  // 设置是否显示Home图标按钮  getActionBar().setDisplayHomeAsUpEnabled(true);  getActionBar().setIcon(R.drawable.weibo_logo);  // 初始化滑动视图  initSlidingMenu(savedInstanceState); } private void initSlidingMenu(Bundle savedInstanceState) {  // 如果保存的状态不为空则得到之前保存的Fragment,否则实例化MyFragment  if (savedInstanceState != null) {   mContent = getSupportFragmentManager().getFragment(     savedInstanceState, "mContent");  }  if (mContent == null) {   mContent = new MyFragment();  }  // 设置主界面视图  setContentView(R.layout.content_frame);  getSupportFragmentManager().beginTransaction()    .replace(R.id.content_frame, new MyFragment()).commit();  // 设置滑动菜单的视图  setBehindContentView(R.layout.menu_frame);  getSupportFragmentManager().beginTransaction()    .replace(R.id.menu_frame, new SampleListFragment()).commit();  // 实例话滑动菜单对象  SlidingMenu sm = getSlidingMenu();  // 设置滑动阴影的宽度  sm.setShadowWidthRes(R.dimen.shadow_width);  // 设置滑动阴影的图像资源  sm.setShadowDrawable(R.drawable.shadow);  // 设置滑动菜单视图的宽度  sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);  // 设置渐入渐出效果的值  sm.setFadeDegree(0.35f);  // 设置触摸屏幕的模式  sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); } /**  * 保存Fragment的状态  */ @Override protected void onSaveInstanceState(Bundle outState) {  super.onSaveInstanceState(outState);  getSupportFragmentManager().putFragment(outState, "mContent", mContent); } /**  * 菜单按钮点击事件,通过点击ActionBar的Home图标按钮来打开滑动菜单  */ @Override public boolean onOptionsItemSelected(MenuItem item) {  switch (item.getItemId()) {  case android.R.id.home:   toggle();   return true;  }  return super.onOptionsItemSelected(item); } @Override public boolean onCreateOptionsMenu(Menu menu) {  // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.main, menu);  return true; } private Fragment mContent; /**  * 切换视图  * @param fragment  */ public void switchContent(Fragment fragment) {  mContent = fragment;  getSupportFragmentManager().beginTransaction()    .replace(R.id.content_frame, fragment).commit();  getSlidingMenu().showContent(); }}


/2013.12.2_SlidingMenu_for_weibo/src/com/wwj/slidingmenu/demo/SampleListFragment.java

package com.wwj.slidingmenu.demo;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;/** *  * @author wwj 功能描述:列表Fragment,用来显示滑动菜单打开后的内容 */public class SampleListFragment extends ListFragment @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {  return inflater.inflate(R.layout.list, null); } @Override public void onActivityCreated(Bundle savedInstanceState) {  super.onActivityCreated(savedInstanceState);  SampleAdapter adapter = new SampleAdapter(getActivity());  adapter.add(new SampleItem("登录", R.drawable.login_logo));  adapter.add(new SampleItem("疑问", R.drawable.d_yiwen));  adapter.add(new SampleItem("右哼哼", R.drawable.d_youhengheng));  adapter.add(new SampleItem("晕", R.drawable.d_yun));  adapter.add(new SampleItem("抓狂", R.drawable.d_zhuakuang));  adapter.add(new SampleItem("猪头", R.drawable.d_zhutou));  adapter.add(new SampleItem("做鬼脸", R.drawable.d_zuoguilian));  adapter.add(new SampleItem("左哼哼", R.drawable.d_zuohengheng));  setListAdapter(adapter); } public class SampleAdapter extends ArrayAdapter<SampleItem> {  public SampleAdapter(Context context) {   super(context, 0);  }  @Override  public View getView(int position, View convertView, ViewGroup parent) {   if (convertView == null) {    convertView = LayoutInflater.from(getContext()).inflate(      R.layout.row, null);   }   ImageView icon = (ImageView) convertView     .findViewById(R.id.row_icon);   icon.setImageResource(getItem(position).iconRes);   TextView title = (TextView) convertView     .findViewById(R.id.row_title);   title.setText(getItem(position).tag);   return convertView;  } } @Override public void onListItemClick(ListView l, View v, int position, long id) {  Fragment newContent = null;  switch (position) {  case 0:   newContent = new MyFragment();   break;  case 1:   newContent = new MyFragment1();   break;  case 2:   newContent = new MyFragment2();   break;  case 3:   newContent = new MyFragment3();   break;  case 4:   newContent = new MyFragment4();   break;  case 5:   newContent = new MyFragment5();   break;  case 6:   newContent = new MyFragment6();  case 7:   newContent = new MyFragment7();   break;  }  if (newContent != null) {   switchFragment(newContent);  }  super.onListItemClick(l, v, position, id); } private void switchFragment(Fragment fragment) {  if (getActivity() == null) {   return;  }  if (getActivity() instanceof MainActivity) {   MainActivity fca = (MainActivity) getActivity();   fca.switchContent(fragment);  } } private class SampleItem {  public String tag;  public int iconRes;  public SampleItem(String tag, int iconRes) {   this.tag = tag;   this.iconRes = iconRes;  } }}

/2013.12.2_SlidingMenu_for_weibo/src/com/wwj/slidingmenu/demo/MyFragment.java

package com.wwj.slidingmenu.demo;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/*** *  * @author wwj *  */public class MyFragment extends Fragment @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {  View view = inflater.inflate(R.layout.login, null);  return view; } @Override public void onActivityCreated(Bundle savedInstanceState) {  super.onActivityCreated(savedInstanceState); } @Override public void onPause() {  super.onPause(); }}

/2013.12.2_SlidingMenu_for_weibo/src/com/wwj/slidingmenu/demo/MyFragment1.java

package com.wwj.slidingmenu.demo;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.ImageView;import android.widget.TextView;public class MyFragment1 extends Fragment @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {  View view = inflater.inflate(R.layout.row, null);  TextView title = (TextView) view.findViewById(R.id.row_title);  title.setText("疑问");  ImageView icon = (ImageView) view.findViewById(R.id.row_icon);  icon.setImageResource(R.drawable.d_yiwen);  return view; } @Override public void onActivityCreated(Bundle savedInstanceState) {  super.onActivityCreated(savedInstanceState); } @Override public void onDestroyView() {  super.onDestroyView(); }}

其他Fragment类似,就不贴了。








           

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值