Android UI开发——SlidingPaneLayout

文章来自:http://blog.csdn.net/xyz_lmn/article/details/12618149


SlidingPaneLayout也是系统支持的高级控件,是Android团对在2013 google IO大会期间更新的Support库(Version 13)中新加入的重要的功能。它支持左右滑动菜单,和SlidingMenu相似。这篇文章简单的介绍SlidingPaneLayout怎么使用,SlidingPaneLayout的使用和前面介绍的Navigation Drawer的使用无二异。

    

    SlidingPaneLayout是一个水平的多层的布局控件,左侧或第一个视图是导航层,其他的为内容视图。

SlidingPaneLayout一般支持左侧滑出,没有右侧导航。


SlidingPaneLayout属于布局类:

[html]  view plain copy print ?
  1. <android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/sliding_layout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.   
  7.     <ListView  
  8.         android:id="@+id/sliding_list"  
  9.         android:layout_width="150dp"  
  10.         android:layout_height="match_parent"  
  11.         android:background="#111"  
  12.         android:choiceMode="singleChoice"  
  13.         android:divider="@android:color/transparent"  
  14.         android:dividerHeight="0dp" />  
  15.   
  16.     <FrameLayout  
  17.         android:id="@+id/content_frame"  
  18.         android:layout_width="300dp"  
  19.         android:layout_height="match_parent"  
  20.         android:layout_weight="1" />  
  21.   
  22. </android.support.v4.widget.SlidingPaneLayout>  


使用 SlidingPaneLayout:

[java]  view plain copy print ?
  1. public class SlidingPaneLayoutActivity extends SherlockFragmentActivity {  
  2.     private SlidingPaneLayout mSlidingPaneLayout;  
  3.     private ListView mSliderList;  
  4.     private ActionBarToggle mActionBarToggle;  
  5.   
  6.     private CharSequence mDrawerTitle;  
  7.     private CharSequence mTitle;  
  8.     private String[] mPlanetTitles;  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_slider);  
  14.   
  15.         mTitle = mDrawerTitle = getTitle();  
  16.         mPlanetTitles = getResources().getStringArray(R.array.planets_array);  
  17.         mSlidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.sliding_layout);  
  18.         mSliderList = (ListView) findViewById(R.id.sliding_list);  
  19.   
  20.         // set a custom shadow that overlays the main content when the slider opens  
  21.         mSlidingPaneLayout.setShadowResource(R.drawable.drawer_shadow);  
  22.         // set up the slider's list view with items and click listener  
  23.         mSliderList.setAdapter(new ArrayAdapter<String>(this,  
  24.                 R.layout.list_item, mPlanetTitles));  
  25.         mSliderList.setOnItemClickListener(new DrawerItemClickListener());  
  26.   
  27.         // enable ActionBar app icon to behave as action to toggle sliding pane  
  28.         getSupportActionBar().setHomeButtonEnabled(true);  
  29.   
  30.         // ActionBarDrawerToggle ties together the the proper interactions  
  31.         // between the sliding pane and the action bar app icon  
  32.         mActionBarToggle = new ActionBarToggle(  
  33.                 this,                  /* host Activity */  
  34.                 mSlidingPaneLayout,         /* SlidingPaneLayout object */  
  35.                 R.drawable.ic_drawer_light,  /* sliding pane  image to replace 'Up' caret */  
  36.                 R.string.slider_open,  /* "open drawer" description for accessibility */  
  37.                 R.string.slider_close  /* "close drawer" description for accessibility */  
  38.                 ) {  
  39.             @Override  
  40.             public void closeView() {  
  41.                 getSupportActionBar().setTitle(mTitle);  
  42.                 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()  
  43.                 super.closeView();  
  44.             }  
  45.               
  46.             @Override  
  47.             public void openView() {  
  48.                 getSupportActionBar().setTitle(mDrawerTitle);  
  49.                 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()  
  50.                 super.openView();  
  51.             }  
  52.         };  
  53.         mSlidingPaneLayout.setPanelSlideListener(mActionBarToggle);  
  54.   
  55.         if (savedInstanceState == null) {  
  56.             selectItem(0);  
  57.         }  
  58.     }  
  59.   
  60.     @Override  
  61.     public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {  
  62.         getSupportMenuInflater().inflate(R.menu.main, menu);  
  63.         return super.onCreateOptionsMenu(menu);  
  64.     }  
  65.   
  66.     @Override  
  67.     public boolean onPrepareOptionsMenu(com.actionbarsherlock.view.Menu menu) {  
  68.         // If the sliding pane is open, hide action items related to the content view  
  69.         boolean drawerOpen = mSlidingPaneLayout.isOpen();  
  70.         menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);  
  71.         return super.onPrepareOptionsMenu(menu);  
  72.     }  
  73.       
  74.     @Override  
  75.     public boolean onOptionsItemSelected(  
  76.             com.actionbarsherlock.view.MenuItem item) {  
  77.         // The action bar home/up action should open or close the drawer.  
  78.         // ActionBarDrawerToggle will take care of this.  
  79.        if (mActionBarToggle.onOptionsItemSelected(item)) {  
  80.            return true;  
  81.        }  
  82.        // Handle action buttons  
  83.        switch(item.getItemId()) {  
  84.        case R.id.action_websearch:  
  85.            // create intent to perform web search for this planet  
  86.            Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);  
  87.            intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());  
  88.            // catch event that there's no activity to handle intent  
  89.            if (intent.resolveActivity(getPackageManager()) != null) {  
  90.                startActivity(intent);  
  91.            } else {  
  92.                Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();  
  93.            }  
  94.            return true;  
  95.        default:  
  96.            return super.onOptionsItemSelected(item);  
  97.        }  
  98.     }  
  99.   
  100.     /* The click listner for ListView in the navigation drawer */  
  101.     private class DrawerItemClickListener implements ListView.OnItemClickListener {  
  102.         @Override  
  103.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  104.             selectItem(position);  
  105.         }  
  106.     }  
  107.   
  108.     private void selectItem(int position) {  
  109.         // update the main content by replacing fragments  
  110.         Fragment fragment = new PlanetFragment();  
  111.         Bundle args = new Bundle();  
  112.         args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);  
  113.         fragment.setArguments(args);  
  114.   
  115.           
  116.         getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();  
  117.   
  118.         // update selected item and title, then close the slider  
  119.         mSliderList.setItemChecked(position, true);  
  120.         setTitle(mPlanetTitles[position]);  
  121.         mSlidingPaneLayout.closePane();  
  122.     }  
  123.   
  124.     @Override  
  125.     public void setTitle(CharSequence title) {  
  126.         mTitle = title;  
  127.         getSupportActionBar().setTitle(mTitle);  
  128.     }  
  129.   
  130.     /** 
  131.      * When using the ActionBarDrawerToggle, you must call it during 
  132.      * onPostCreate() and onConfigurationChanged()... 
  133.      */  
  134.   
  135.     @Override  
  136.     protected void onPostCreate(Bundle savedInstanceState) {  
  137.         super.onPostCreate(savedInstanceState);  
  138.         // Sync the toggle state after onRestoreInstanceState has occurred.  
  139.         mActionBarToggle.syncState();  
  140.         getSupportActionBar().setDisplayHomeAsUpEnabled(mActionBarToggle.isDrawerIndicatorEnabled());  
  141.     }  
  142.   
  143.     @Override  
  144.     public void onConfigurationChanged(Configuration newConfig) {  
  145.         super.onConfigurationChanged(newConfig);  
  146.         // Pass any configuration change to the slider toggls  
  147.         mActionBarToggle.onConfigurationChanged(newConfig);  
  148.         getSupportActionBar().setDisplayHomeAsUpEnabled(mActionBarToggle.isDrawerIndicatorEnabled());  
  149.     }  
  150.       
  151.     @Override  
  152.     public void onBackPressed() {  
  153.         if(mSlidingPaneLayout.isSlideable() && mSlidingPaneLayout.isOpen()){  
  154.             mSlidingPaneLayout.closePane();  
  155.         }  
  156.         else{  
  157.             super.onBackPressed();  
  158.         }  
  159.     }  
  160.   
  161.     /** 
  162.      * Fragment that appears in the "content_frame", shows a planet 
  163.      */  
  164.     public static class PlanetFragment extends Fragment {  
  165.         public static final String ARG_PLANET_NUMBER = "planet_number";  
  166.   
  167.         public PlanetFragment() {  
  168.             // Empty constructor required for fragment subclasses  
  169.         }  
  170.   
  171.         @Override  
  172.         public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  173.                 Bundle savedInstanceState) {  
  174.             View rootView = inflater.inflate(R.layout.fragment_planet, container, false);  
  175.             int i = getArguments().getInt(ARG_PLANET_NUMBER);  
  176.             String planet = getResources().getStringArray(R.array.planets_array)[i];  
  177.   
  178.             int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),  
  179.                             "drawable", getActivity().getPackageName());  
  180.             ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);  
  181.             getActivity().setTitle(planet);  
  182.             return rootView;  
  183.         }  
  184.     }  
  185. }  



SlidingPaneLayout的使用和Navigation Drawer相同,有疑问的地方可以参考demo。







参考:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值