博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SlidingPaneLayout+ActionBarDrawerToggle组合开发
阅读量:6587 次
发布时间:2019-06-24

本文共 8256 字,大约阅读时间需要 27 分钟。

hot3.png

类库appcompat-support-v7库项目  (注意:请使用Android 19以下版本最佳,因为设计概念的变化,Android L版本支持不佳)

android-support-v4.jar

效果预览

132407_pKHr_2256215.gif

主布局文件slinding_layout.xml

    
    

Activity逻辑文件

public class SlidingPaneLayoutActivity extends ActionBarActivity {    private SlidingPaneLayout mSlidingPaneLayout;    private ListView mSliderList;    private ActionBarDrawerToggle mActionBarToggle;    private CharSequence mDrawerTitle;    private CharSequence mTitle;    private String[] mPlanetTitles;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.slinding_layout);        //获取标题        mTitle = mDrawerTitle = getTitle();        //获取菜单列表        mPlanetTitles = getResources().getStringArray(R.array.planets_array);        //获取SlidingPaneLayout        mSlidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.sliding_layout);        mSliderList = (ListView) findViewById(R.id.sliding_list);        //设置背景        mSlidingPaneLayout.setShadowDrawable(new ColorDrawable(0x99000000));        mSliderList.setAdapter(new ArrayAdapter
(this,                android.R.layout.simple_list_item_1, mPlanetTitles));        mSliderList.setOnItemClickListener(new DrawerItemClickListener());        getSupportActionBar().setHomeButtonEnabled(true);               mActionBarToggle = new ActionBarDrawerToggle(                this,                  /* host Activity */                new DrawerLayout(this),         /* 注意,这里必须有个 DrawerLayout*/                R.drawable.ic_toggler_home,                R.string.slider_open,  /* "open drawer" description for accessibility */                R.string.slider_close  /* "close drawer" description for accessibility */                ) {                   @Override         public void onDrawerClosed(View drawerView) {         super.onDrawerClosed(drawerView);          getSupportActionBar().setTitle(mTitle);          postInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()         }                  @Override         public boolean onOptionsItemSelected(MenuItem item) {         //---------------这里需要特别注意---不能再这里调用super.onOptionsItemSelected(item);         if(item.getItemId()==android.R.id.home)         {         if(!mSlidingPaneLayout.isOpen())         {         mSlidingPaneLayout.openPane();         }else{                  mSlidingPaneLayout.closePane();         }         }         return true;         }                  @Override         public void onDrawerOpened(View drawerView) {         super.onDrawerOpened(drawerView);         getSupportActionBar().setTitle(mDrawerTitle);         postInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()         }                         };        mSlidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.SimplePanelSlideListener(){         @Override         public void onPanelClosed(View panel) {         super.onPanelClosed(panel);         //模拟调用         mActionBarToggle.onDrawerClosed(panel);         }                  @Override         public void onPanelOpened(View panel) {         // TODO Auto-generated method stub         super.onPanelOpened(panel);         //模拟调用         mActionBarToggle.onDrawerOpened(panel);         }                  @Override         public void onPanelSlide(View panel, float slideOffset) {         super.onPanelSlide(panel, slideOffset);                  mActionBarToggle.onDrawerSlide(panel, slideOffset);         }        });        if (savedInstanceState == null) {            selectItem(0);        }    }       @Override    public boolean onCreateOptionsMenu(Menu menu) {     getMenuInflater().inflate(R.menu.main, menu);     return super.onCreateOptionsMenu(menu);    }    @Override    public boolean onPrepareOptionsMenu(Menu menu) {            boolean drawerOpen = mSlidingPaneLayout.isOpen();          menu.findItem(R.id.action_search).setVisible(!drawerOpen);       return super.onPrepareOptionsMenu(menu);    }        @Override    public boolean onOptionsItemSelected(MenuItem item) {           if (mActionBarToggle.onOptionsItemSelected(item))      {             return true;         }       switch(item.getItemId()) {          case R.id.action_search:              Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);              intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());              if (intent.resolveActivity(getPackageManager()) != null) {                  startActivity(intent);              } else {                  Toast.makeText(this, R.string.app_name, Toast.LENGTH_LONG).show();              }              return true;          default:              return super.onOptionsItemSelected(item);          }           }    private class DrawerItemClickListener implements ListView.OnItemClickListener {        @Override        public void onItemClick(AdapterView
 parent, View view, int position, long id) {            selectItem(position);        }    }    private void selectItem(int position) {        Fragment fragment = new PlanetFragment();        Bundle args = new Bundle();        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);        fragment.setArguments(args);                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();        mSliderList.setItemChecked(position, true);        setTitle(mPlanetTitles[position]);        mSlidingPaneLayout.closePane();    }    @Override    public void setTitle(CharSequence title) {        mTitle = title;        getSupportActionBar().setTitle(mTitle);    }    @Override    protected void onPostCreate(Bundle savedInstanceState) {        super.onPostCreate(savedInstanceState);        mActionBarToggle.syncState();        getSupportActionBar().setDisplayHomeAsUpEnabled(mActionBarToggle.isDrawerIndicatorEnabled());    }    @Override    public void onConfigurationChanged(Configuration newConfig) {        super.onConfigurationChanged(newConfig);        mActionBarToggle.onConfigurationChanged(newConfig);        getSupportActionBar().setDisplayHomeAsUpEnabled(mActionBarToggle.isDrawerIndicatorEnabled());    }        @Override    public void onBackPressed() {     if(mSlidingPaneLayout.isSlideable() && mSlidingPaneLayout.isOpen()){     mSlidingPaneLayout.closePane();     }     else{     super.onBackPressed();     }    }        private void postInvalidateOptionsMenu() { try { Method method = this.getClass().getMethod("invalidateOptionsMenu", new Class
[]{}); if(method!=null) {             if (Modifier.isPublic(method.getModifiers()) &&                     Modifier.isPublic(method.getDeclaringClass().getModifiers())) {              method.invoke(this, new Object[]{});             }             else if(!method.isAccessible())             {             method.setAccessible(true);             method.invoke(this, new Object[]{});             } } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }     /**     * Fragment that appears in the "content_frame", shows a planet     */    public static class PlanetFragment extends Fragment {        public static final String ARG_PLANET_NUMBER = "planet_number";        public PlanetFragment() {        }        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container,                Bundle savedInstanceState) {            View rootView = inflater.inflate(R.layout.fragment_main, container, false);            int i = getArguments().getInt(ARG_PLANET_NUMBER);            String planet = getResources().getStringArray(R.array.planets_array)[i];            int imageId = getResources().getIdentifier("audio_identify_start_default",                            "drawable", getActivity().getPackageName());            ((ImageView) rootView.findViewById(R.id.section_image)).setImageResource(imageId);            getActivity().setTitle(planet);            return rootView;        }    }

fragment布局文件fragment_main.xml

    
        

转载于:https://my.oschina.net/ososchina/blog/408190

你可能感兴趣的文章
怎么给电脑设置IP地址和DNS地址,各系统设置IP/DNS几种方法
查看>>
Shader 学习笔记 ---Depth of Field 介绍
查看>>
星级 评分
查看>>
通信协议之广播---recvfrom 放回客户端的ip地址第一次全为0.0.0.0
查看>>
php 常用函数
查看>>
oracle-3-子查询和常用函数
查看>>
item2
查看>>
云计算面临安全挑战
查看>>
C# 线程手册 第三章 使用线程 Monitor.TryEnter()
查看>>
分享11个超棒的移动应用(mobile apps)开发解决方案
查看>>
C/C++获取文件大小
查看>>
深入理解Java内存模型(五)——锁
查看>>
Chalubo僵尸网络来袭 IOT设备或将受到DDoS攻击
查看>>
如何实现百万TPS?详解JMQ4的存储设计
查看>>
这么说吧,NIO很简单,其实就是个牛逼IO
查看>>
七、【应用的主要框架】
查看>>
使用Python快速获取公众号文章定制电子书(二)
查看>>
iOS下JS与OC互相调用(七)--Cordova 基础
查看>>
Three.js 关于立方体贴图产生边缘锯齿问题
查看>>
Nacos v0.7.0:对接CMDB,实现基于标签的服务发现能力
查看>>