Implementing Lateral Navigation 实现横向导航
2013-05-04 09:35
267 查看
Lateral navigation is navigation between sibling screens in the application's screen hierarchy (sometimes referred to as a screen map). The most prominent lateral navigation patterns are tabs and horizontal paging (also known as swipe views). This
pattern and others are described in Designing Effective Navigation. This lesson covers how to implement several of the primary lateral navigation patterns in Android. http://blog.csdn.net/sergeycao
is a dropdown list (sometimes implemented using a
In previous versions of Android, tabs could be implemented using a
As of Android 3.0, however, you should use either
For example, Android may automatically collapse tabs into a dropdown list if tabs don't fit horizontally in the action bar.
Android Support Package. For navigating between sibling screens representing a fixed number of sections, it's best to provide the
Below is an example of using a
You can also add indicators to your horizontal paging UI by adding a
allow swiping between them where appropriate. This behavior enables users to swipe horizontally across the selected tab's contents to navigate to adjacent tabs, without needed to directly interact with the tabs themselves. To implement this, you can use
a
Upon observing the current page changing, select the corresponding tab. You can set up this behavior using an
And upon selecting a tab, switch to the corresponding page in the
pattern and others are described in Designing Effective Navigation. This lesson covers how to implement several of the primary lateral navigation patterns in Android. http://blog.csdn.net/sergeycao
Implement Tabs
Tabs allow the user to navigate between sibling screens by selecting the appropriate tab indicator available at the top of the display. In Android 3.0 and later, tabs are implemented using theActionBarclass, and are generally set up in
Activity.onCreate(). In some cases, such as when horizontal space is limited and/or the number of tabs is large, an appropriate alternate presentation for tabs
is a dropdown list (sometimes implemented using a
Spinner).
In previous versions of Android, tabs could be implemented using a
TabWidgetand
TabHost. For details, see the Hello, Views tutorial.
As of Android 3.0, however, you should use either
N***IGATION_MODE_TABSor
N***IGATION_MODE_LISTalong with the
ActionBarclass.
Implement the Tabs Pattern with N***IGATION_MODE_TABS
To create tabs, you can use the following code in your activity'sonCreate()method. Note that the exact presentation of tabs may vary per device and by the current device configuration, to make best use of available screen space.
For example, Android may automatically collapse tabs into a dropdown list if tabs don't fit horizontally in the action bar.
@Override public void onCreate(Bundle savedInstanceState) { ... final ActionBar actionBar = getActionBar(); // Specify that tabs should be displayed in the action bar. actionBar.setNavigationMode(ActionBar.N***IGATION_MODE_TABS); // Create a tab listener that is called when the user changes tabs. ActionBar.TabListener tabListener = new ActionBar.TabListener() { public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { } public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { } public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { } }; // Add 3 tabs. for (int i = 0; i < 3; i++) { actionBar.addTab( actionBar.newTab() .setText("Tab " + (i + 1)) .setTabListener(tabListener)); } ... }
Implement the Tabs Pattern with N***IGATION_MODE_LIST
To use a dropdown list instead, use the following code in your activity'sonCreate()method. Dropdown lists are often preferable in cases where more information must be shown per navigation item, such as unread message counts, or where the number of available navigation items is large.
@Override public void onCreate(Bundle savedInstanceState) { ... final ActionBar actionBar = getActionBar(); // Specify that a dropdown list should be displayed in the action bar. actionBar.setNavigationMode(ActionBar.N***IGATION_MODE_LIST); actionBar.setListNavigationCallbacks( // Specify a SpinnerAdapter to populate the dropdown list. new ArrayAdapter ( actionBar.getThemedContext(), android.R.layout.simple_list_item_1, android.R.id.text1, new String[]{ "Tab 1", "Tab 2", "Tab 3" }), // Provide a listener to be called when an item is selected. new ActionBar.OnNavigationListener() { public boolean onNavigationItemSelected( int position, long id) { // Take action here, e.g. switching to the // corresponding fragment. return true; } }); ... }
Implement Horizontal Paging (Swipe Views)
Horizontal paging, or swipe views, allow users to swipe horizontally on the current screen to navigate to adjacent screens. This pattern can be implemented using theViewPagerwidget, currently available as part of the
Android Support Package. For navigating between sibling screens representing a fixed number of sections, it's best to provide the
ViewPagerwith a
FragmentPagerAdapter. For horizontal paging across collections of objects, it's best to use a
FragmentStatePagerAdapter, which destroys fragments as the user navigates to other pages, minimizing memory usage.
Below is an example of using a
ViewPagerto swipe across a collection of objects.
public class CollectionDemoActivity extends FragmentActivity { // When requested, this adapter returns a DemoObjectFragment, // representing an object in the collection. DemoCollectionPagerAdapter mDemoCollectionPagerAdapter; ViewPager mViewPager; public void onCreate(Bundle savedInstanceState) { // ViewPager and its adapters use support library // fragments, so use getSupportFragmentManager. mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter( getSupportFragmentManager()); mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mDemoCollectionPagerAdapter); } } // Since this is an object collection, use a FragmentStatePagerAdapter, // and NOT a FragmentPagerAdapter. public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter { public DemoCollectionPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { Fragment fragment = new DemoObjectFragment(); Bundle args = new Bundle(); // Our object is just an integer :-P args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); fragment.setArguments(args); return fragment; } @Override public int getCount() { return 100; } @Override public CharSequence getPageTitle(int position) { return "OBJECT " + (position + 1); } } // Instances of this class are fragments representing a single // object in our collection. public static class DemoObjectFragment extends Fragment { public static final String ARG_OBJECT = "object"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // The last two arguments ensure LayoutParams are inflated // properly. View rootView = inflater.inflate( R.layout.fragment_collection_object, container, false); Bundle args = getArguments(); ((TextView) rootView.findViewById(android.R.id.text1)).setText( Integer.toString(args.getInt(ARG_OBJECT))); return rootView; } }
You can also add indicators to your horizontal paging UI by adding a
PagerTitleStrip. Below is an example layout XML file for an activity whose entire contents are a
ViewPagerand a top-aligned
PagerTitleStripinside it. Individual pages (provided by the adapter) occupy the remaining space inside the
ViewPager.
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.PagerTitleStrip android:id="@+id/pager_title_strip" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="#33b5e5" android:textColor="#fff" android:paddingTop="4dp" android:paddingBottom="4dp" /> </android.support.v4.view.ViewPager>
Implement Swiping Between Tabs
One of the key design recommendations in Android 4.0 for tabs is toallow swiping between them where appropriate. This behavior enables users to swipe horizontally across the selected tab's contents to navigate to adjacent tabs, without needed to directly interact with the tabs themselves. To implement this, you can use
a
ViewPagerin conjunction with the
ActionBartabs API.
Upon observing the current page changing, select the corresponding tab. You can set up this behavior using an
ViewPager.OnPageChangeListenerin your activity's
onCreate()method:
@Override public void onCreate(Bundle savedInstanceState) { ... mViewPager.setOnPageChangeListener( new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { // When swiping between pages, select the // corresponding tab. getActionBar().setSelectedNavigationItem(position); } }); ... }
And upon selecting a tab, switch to the corresponding page in the
ViewPager. To do this, add an
ActionBar.TabListenerto your tab when creating it using the
newTab()method:
actionBar.newTab() ... .setTabListener(new ActionBar.TabListener() { public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { // When the tab is selected, switch to the // corresponding page in the ViewPager. mViewPager.setCurrentItem(tab.getPosition()); } ... }));
相关文章推荐
- Implementing Temporal Navigation 实现时间导航
- Implementing Descendant Navigation 实现下一代导航
- Providing Descendant and Lateral Navigation 提供下一代和横向导航
- Implementing Ancestral Navigation 实现原始导航
- CSS学习(二)ASP.NET实现带当前标识的横向导航
- jquery实现无限分级横向导航菜单的方法
- 实现Flex HotKey 键盘四个方向键(Navigation)导航功能
- Android导航菜单横向左右滑动并和上方的控件实现联动
- Windows Phone 系列- Simple MVVM Navigation 页面导航的实现
- Android导航菜单横向左右滑动并和下方的控件实现联动
- 纯css实现Magicline Navigation(下划线动画导航菜单)
- js实现横向伸展开的二级导航菜单代码
- jquery实现无限分级横向导航菜单的方法
- Android导航菜单横向左右滑动并和下方的控件实现联动
- 如何present出一个透明导航控制器NavigationController + UIVisualEffectView实现模糊效果、毛玻璃效果
- 使用Android support library25.0.0 提供的BottomNavigationView实现底部导航功能
- Android导航菜单横向左右滑动并和下方的控件实现联动
- #UWP#导航(navigation)的实现
- Android导航菜单横向左右滑动并和下方的控件实现联动
- [已完成,附上实现方式]DWZ横向导航实现动态左菜单树