罪过啊,罪过,很久都没更新博客了,今天刚好有时间,然后现在时下流行很多应用都有侧滑功能,不用说,这就是今天的主题,fragment这也是本人第一次使用这个东西,原来一直在2.3的环境下开发,表示相当压抑,一直没时间研究新特征,表示很无奈,不过以后会有新的东西奉献给大家,在大家继续之前,如果有不会使用的fragment的童鞋,先建议看下面一篇文章,里面讲的很详细,所以我这里就不罗嗦了,哈哈
http://blog.csdn.net/aomandeshangxiao/article/details/7671533
下面就开始正餐了,在这以前也参考一些大牛的神笔,比如eoe超版Kris大神,这里也不废话了,先看效果图:
下面就详细分析整个实现过程:
1、 主界面由两个fragment构成:Content+Menu,我们看看Content的代码:
01 | public final String tag = this .getClass().getName(); |
02 |
public String text = null ; |
04 |
public ContentFragment(String text) { |
10 |
public void onCreate(Bundle savedInstanceState) { |
11 |
super .onCreate(savedInstanceState); |
12 |
setRetainInstance( true ); |
13 |
Log.e(tag, "onCreate:" + text); |
17 |
public View onCreateView(LayoutInflater inflater, ViewGroup container, |
18 |
Bundle savedInstanceState) { |
19 |
Log.e(tag, "onCreateView:" + text); |
21 |
View view = inflater.inflate(R.layout.content_layout, null ); |
22 |
TextView textView = (TextView) view.findViewById(R.id.content_tv); |
23 |
if (!TextUtils.isEmpty(text)) { |
24 |
textView.setText(text); |
Content 里面只有一个Textview用来显示内容,我需要在Activity里面告诉它需要显示神马。
我们再来看看Menu里面的代码:
01 | public final String tag = this .getClass().getName(); |
02 |
public ListView menu_lv = null ; |
03 |
public MenuAdapter menuAdapter = null ; |
04 |
public Context mContext = null ; |
06 |
public MenuFragment(Context mContext) { |
07 |
this .mContext = mContext; |
11 |
public void onCreate(Bundle savedInstanceState) { |
13 |
super .onCreate(savedInstanceState); |
17 |
public View onCreateView(LayoutInflater inflater, ViewGroup container, |
18 |
Bundle savedInstanceState) { |
20 |
View menuView = inflater.inflate(R.layout.menu_layout, null ); |
21 |
menu_lv = (ListView) menuView.findViewById(R.id.menu_lv); |
22 |
menuAdapter = new MenuAdapter(mContext); |
23 |
menu_lv.setAdapter(menuAdapter); |
24 |
menu_lv.setOnItemClickListener(itemListener); |
28 |
public OnItemClickListener itemListener = new OnItemClickListener() { |
31 |
public void onItemClick(AdapterView<?> arg0, View arg1, int position, |
34 |
((MainActivity)mContext).getSlidingMenu().toggle(); |
35 |
Toast.makeText(mContext, menuAdapter.menuStr[position], Toast.LENGTH_SHORT).show(); |
36 |
FragmentTransaction ft = ((MainActivity)mContext).getFragmentManager().beginTransaction(); |
37 |
ContentFragment cf = new ContentFragment(menuAdapter.menuStr[position]); |
38 |
ft.replace(R.id.main_rl, cf); |
这里我有个listview,在listview监听里面我拿到的Main的fragment事物管理,然后处理Content进行显示内容的变更。
下面看看listview 配置的 Adapter代码:
01 | public String[] menuStr = { "好友推荐" , "关于我" , "意见反馈" , "版本更新" }; |
02 |
public int [] viewId = { R.drawable.recommendfriend, R.drawable.about, |
03 |
R.drawable.suggestion, R.drawable.refurbish }; |
04 |
public LayoutInflater inflater = null ; |
06 |
public MenuAdapter(Context context) { |
07 |
inflater = LayoutInflater.from(context); |
11 |
public int getCount() { |
13 |
return menuStr.length; |
17 |
public Object getItem( int position) { |
19 |
return menuStr[position]; |
23 |
public long getItemId( int position) { |
34 |
public View getView( int position, View convertView, ViewGroup parent) { |
37 |
if (convertView == null ) { |
38 |
convertView = inflater.inflate(R.layout.menu_items, null ); |
39 |
holder = new Holder(); |
40 |
holder.items_iv = (ImageView) convertView |
41 |
.findViewById(R.id.items_iv); |
42 |
holder.items_tv = (TextView) convertView |
43 |
.findViewById(R.id.items_tv); |
44 |
convertView.setTag(holder); |
46 |
holder = (Holder) convertView.getTag(); |
48 |
holder.items_iv.setBackgroundResource(viewId[position]); |
49 |
holder.items_tv.setText(menuStr[position]); |
这里就是Menu显示的内容。
下面来看看Activity里面代码:
01 | public FragmentTransaction fraTra = null ; |
02 |
public MenuFragment menuFra = null ; |
03 |
public ContentFragment contentFra = null ; |
06 |
public void onCreate(Bundle savedInstanceState) { |
07 |
super .onCreate(savedInstanceState); |
08 |
setContentView(R.layout.activity_main); |
09 |
setBehindContentView(R.layout.menu_layout); |
10 |
fraTra = this .getFragmentManager().beginTransaction(); |
11 |
menuFra = new MenuFragment( this ); |
12 |
contentFra = new ContentFragment( "I'm John" ); |
13 |
fraTra.replace(R.id.menu_fl, menuFra); |
14 |
fraTra.replace(R.id.main_rl, contentFra); |
17 |
SlidingMenu sm = getSlidingMenu(); |
18 |
sm.setShadowWidth( 15 ); |
19 |
sm.setBehindOffset( 300 ); |
20 |
sm.setShadowDrawable(R.drawable.shadow); |
21 |
sm.setFadeDegree( 0 .35f); |
22 |
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN); |
24 |
getActionBar().setDisplayHomeAsUpEnabled( true ); |
28 |
public boolean onCreateOptionsMenu(Menu menu) { |
30 |
getMenuInflater().inflate(R.menu.activity_main, menu); |
35 |
public boolean onOptionsItemSelected(MenuItem item) { |
36 |
switch (item.getItemId()) { |
37 |
case android.R.id.home: |
42 |
return super .onOptionsItemSelected(item); |
这里主要是Activity对Menu和Content进行了集中处理、设置侧滑菜单的相关属性。
相信大家看到这里都明白了吧。很简单的一个小例子,但是很实用,时间不早了今天就说到这里,有啥疑问,给我留言,谢谢。
差点忘了,附上源码:
http://www.eoeandroid.com/thread-288729-1-1.html
由于gitHub经常访问不了,先暂时放到eoe上面。