其他分享
首页 > 其他分享> > AndroidUI--SlidingMenu使用例子,android面试试题

AndroidUI--SlidingMenu使用例子,android面试试题

作者:互联网

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);

}

/**

*/

@Override

protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

getSupportFragmentManager().putFragment(outState, “mContent”, mContent);

}

/**

*/

@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;

/**

*/

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;

/**

*/

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 {

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;

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

/***

*/

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);

}

标签:SlidingMenu,AndroidUI,--,savedInstanceState,import,Override,new,android,public
来源: https://blog.csdn.net/m0_64604042/article/details/121884651