首页 > 代码库 > AndroidUI--SlidingMenu使用例子

AndroidUI--SlidingMenu使用例子

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