android-Creating a Navigation Drawer
The navigation drawer is a panel that displays the app’s main navigation options on the left edge of the screen. It is hidden most of the time, but is revealed when the user swipes
a finger from the left edge of the screen or, while at the top level of the app, the user touches the app icon in the action bar.
>the following layout uses a DrawerLayout
with two child views: a FrameLayout
to
contain the main content (populated by a Fragment
at runtime), and a ListView
for
the navigation drawer.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- The navigation drawer --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="#111"/> </android.support.v4.widget.DrawerLayout>How you do so depends on the content of your app, but a navigation drawer often consists of a
ListView
,
so the list should be populated by an Adapter
(such
as ArrayAdapter
or SimpleCursorAdapter
).
public class MainActivity extends Activity { private String[] mPlanetTitles; private DrawerLayout mDrawerLayout; private ListView mDrawerList; ... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPlanetTitles = getResources().getStringArray(R.array.planets_array); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); // Set the adapter for the list view mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles)); // Set the list's click listener mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); ... } }
private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { selectItem(position); } } /** Swaps fragments in the main content view */ private void selectItem(int position) { // Create a new fragment and specify the planet to show based on position Fragment fragment = new PlanetFragment(); Bundle args = new Bundle(); args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); fragment.setArguments(args); // Insert the fragment by replacing any existing fragment FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.content_frame, fragment) .commit(); // Highlight the selected item, update the title, and close the drawer mDrawerList.setItemChecked(position, true); setTitle(mPlanetTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } @Override public void setTitle(CharSequence title) { mTitle = title; getActionBar().setTitle(mTitle); }> To listen for drawer open and close events, call
setDrawerListener()
on
your DrawerLayout
and
pass it an implementation of DrawerLayout.DrawerListener
.
This interface provides callbacks for drawer events such asonDrawerOpened()
and onDrawerClosed()
.
However, rather than implementing the DrawerLayout.DrawerListener
,
if your activity includes the action bar, you can instead extend the ActionBarDrawerToggle
class.
The ActionBarDrawerToggle
implementsDrawerLayout.DrawerListener
so
you can still override those callbacks, but it also facilitates the proper interaction behavior between the action bar icon and the navigation drawer (discussed further in the next section).
相關文章
- 安卓導航抽屜 Navigation Drawer 實現沉浸通知欄安卓Navigation
- Flutter drawer側邊欄Flutter
- flutter - 自定義 Drawer 元件(不依賴 Scaffold)Flutter元件
- ABAP webdynpro的view navigation和WebUI的view navigationWebViewNavigationUI
- Jetpack 之 Navigation 初探JetpackNavigation
- android-Providing Up Navigation,Providing Proper Back NavigationAndroidNavigation
- HarmonyOS:Navigation元件的使用Navigation元件
- Flutter自定義View——仿高德三級聯動DrawerFlutterView
- Flutter 系列文章:Flutter Drawer 控制元件介紹Flutter控制元件
- Flutter之drawer詳細分析(你要的操作都有)Flutter
- Flutter - Drawer 抽屜檢視與自定義headerFlutterHeader
- 『React Navigation 3x系列教程』之React Navigation 3x開發指南ReactNavigation
- react-navigation 使用錦囊ReactNavigation
- react-navigation圖文攻略ReactNavigation
- Jetpack Navigation----原始碼解析JetpackNavigation原始碼
- Android Jetpack Navigation基本使用AndroidJetpackNavigation
- BZOJ1481 : Navigation GameNavigationGAM
- 不到30行, 用any-touch實現一個drawer
- Flutter學習篇(二)—— Drawer和水紋按壓效果Flutter
- [譯] 5 分鐘讓 Drawer 在狀態列下可見
- Flutter 基礎(六)Material 元件之 BottomNavigationBar、TabBar、DrawerFlutter元件NavigationtabBar
- Flutter基礎(六)Material元件之BottomNavigationBar、TabBar、DrawerFlutter元件NavigationtabBar
- React Navigation 的個人分析與融合ReactNavigation
- Android 官方元件 Navigation 初使用Android元件Navigation
- react navigation實現透明彈窗ReactNavigation
- react-navigation的超級大坑ReactNavigation
- Web Navigation(stack棧的運用)WebNavigation
- B. Navigation System【CF 1320】Navigation
- Avoided redundant navigation to current location: "/users"IDENavigation
- 自定義react-navigation的TabBarReactNavigationtabBar
- react-navigation路由篇之StackRouterReactNavigation路由
- ReactNative填坑之旅–Navigation篇ReactNavigation
- Basic Knowledge and System Navigation Question ----RohanNavigation
- 鴻蒙HarmonyOS實戰-ArkUI元件(Navigation)鴻蒙UI元件Navigation
- android-Creating Multiple APKs for Different Screen Sizes,Creating Multiple APKs for Different GL TeAndroidAPK
- 谷歌官方元件Navigation你會用了嗎?谷歌元件Navigation
- 谷歌官方元件Navigation你瞭解了嗎?谷歌元件Navigation
- 最全面的Navigation的使用指南Navigation