码迷,mamicode.com
首页 > 其他好文 > 详细

SlidingMenu的使用2

时间:2015-04-08 19:46:28      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

 1 public class MainActivity extends FragmentActivity implements OnItemClickListener {
 2 
 3     private SlidingMenu mSlidingMenu;
 4     private Fragment mCurrentFragment;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         // requestWindowFeature(Window.FEATURE_NO_TITLE);
10         setContentView(R.layout.activity_main);
11 
12         mSlidingMenu = new SlidingMenu(this);
13         mSlidingMenu.setMenu(R.layout.empty_left_menu);
14         mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
15         mSlidingMenu.setBehindWidth((int) (getResources().getDisplayMetrics().widthPixels * 0.75f));
16         mSlidingMenu.setMode(SlidingMenu.LEFT);
17         mSlidingMenu.attachToActivity(this, 0);
18 
19         LeftFragment leftFragment = new LeftFragment();
20         leftFragment.setOnItemClickListener(this);
21 
22         ContentFragment contentFragment = new ContentFragment();
23 
24         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
25         ft.replace(R.id.left_empty_layout, leftFragment);
26         ft.replace(R.id.content_empty_layout, contentFragment);
27         ft.commit();
28 
29         mCurrentFragment = contentFragment;
30     }
31 
32     @Override
33     public void onItemClick(String str) {
34         mSlidingMenu.showContent();
35 
36         if ("btn1".equals(str)) {
37             FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
38             mCurrentFragment = new ContentFragment();
39             ft.replace(R.id.content_empty_layout, mCurrentFragment);
40             ft.commit();
41         } else if ("btn2".equals(str)) {
42             FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
43             mCurrentFragment = new SettingFragment();
44             ft.replace(R.id.content_empty_layout, new SettingFragment());
45             ft.commit();
46         } else if ("btn3".equals(str)) {
47             FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
48             mCurrentFragment = new BuyFragment();
49             ft.replace(R.id.content_empty_layout, new BuyFragment());
50             ft.commit();
51         } else {
52             if (mCurrentFragment instanceof ContentFragment) {
53                 ContentFragment content = (ContentFragment) mCurrentFragment;
54                 content.changeContent(str);
55             }
56         }
57     }
58 }
 1 public class LeftFragment extends Fragment implements OnClickListener {
 2 
 3     public static interface OnItemClickListener {
 4         public void onItemClick(String str);
 5     }
 6 
 7     private OnItemClickListener mClickListener;
 8 
 9     @Override
10     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
11         return inflater.inflate(R.layout.fgm_left, container, false);
12     }
13 
14     public void setOnItemClickListener(OnItemClickListener l) {
15         mClickListener = l;
16     }
17 
18     @Override
19     public void onActivityCreated(Bundle savedInstanceState) {
20         super.onActivityCreated(savedInstanceState);
21 
22         View view = getView();
23 
24         LinearLayout parent = (LinearLayout) view.findViewById(R.id.parent);
25 
26         int childCount = parent.getChildCount();
27 
28         for (int i = 0; i < childCount; i++) {
29             View child = parent.getChildAt(i);
30             child.setOnClickListener(this);
31         }
32     }
33 
34     @Override
35     public void onClick(View v) {
36         Button btn = (Button) v;
37         String str = (String) btn.getText();
38 
39         if (mClickListener != null) {
40             mClickListener.onItemClick(str);
41         }
42     }
43 }
 1 public class ContentFragment extends Fragment {
 2 
 3     @Override
 4     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 5         return inflater.inflate(R.layout.fgm_content, container, false);
 6     }
 7 
 8     public void changeContent(String txt) {
 9         TextView tv = (TextView) getView().findViewById(R.id.content_tv);
10         tv.setText(txt);
11     }
12 }
1 public class BuyFragment extends Fragment {
2 
3     @Override
4     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
5         return inflater.inflate(R.layout.fgm_buy, container, false);
6     }
7 }
1 public class SettingFragment extends Fragment {
2 
3     @Override
4     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
5         return inflater.inflate(R.layout.fgm_setting, container, false);
6     }
7 }

 

SlidingMenu的使用2

标签:

原文地址:http://www.cnblogs.com/CodeUtils/p/4403118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!