編輯:關於 Android 程式設計
2013 年前後,YouTube、Facebook、Path 等應用紛紛採用 Navigation Drawer 這種導航形式;Google 也為了解決 Android 錢版面越來越混亂的問題,把它納入官方設計規範。雖然後來 Google+ 改用了別的方案,引發過一輪關於抽屜導航利弊的討論,但對開發者而言,把功能做出來才是重點。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.naviganationdrawer.MainActivity" >
<android.support.v4.widget.DrawerLayout
android:id="@+id/mDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 主內容必須是第一個子 View -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- 導航抽屜 -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/holo_red_light"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
DrawerLayout 作為版面的根控件。其中第一個子 View 是當前界面的主要內容,第二與第三個子 View 則是抽屜選單內容。若只需要一個抽屜,第三個 View 可以省略。
顯示主要內容的 View 一定要放在第一位。原因在於 XML 版面中的 View 順序對應 Android 系統的 z-ordering,而抽屜必須疊在內容之上,順序錯了它就會被蓋住。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/content_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</FrameLayout>
mPlanetTitles = new String[]{"財經", "科技"};
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mPlanetTitles));
mDrawerLayout = (DrawerLayout) findViewById(R.id.mDrawerLayout);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
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 FragmentContent(mImageList[position]);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
這裡做了四件事:替換主內容區的 Fragment、更新列表的勾選狀態、設定標題、關閉抽屜。最後那句 closeDrawer() 別忘了——使用者選完項目後抽屜應該自動收起,這是預期行為。
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
public class FragmentContent extends Fragment {
private int resourceId;
private View view;
private ImageView mImageView;
public FragmentContent(int i) {
resourceId = i;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragmentcontent, null);
mImageView = (ImageView) view.findViewById(R.id.content_image);
mImageView.setImageResource(resourceId);
return view;
}
}
實作到這裡,抽屜已經能用了,但還缺一個直覺的開關入口。官方規範建議在 ActionBar 左側放一個漢堡選單圖示,並在抽屜展開時把它變換成返回箭頭,同時讓 ActionBar 的標題改成目前所選頁面的名稱。這部分可以透過 ActionBarDrawerToggle 來處理,你只要把 DrawerLayout 與 ActionBar 傳進建構子,並在 Activity 的 onPostCreate 中呼叫 syncState() 即可。
此外別忘了處理 onOptionsItemSelected,讓使用者點擊 ActionBar 左側圖示時也能開合抽屜。少了這一步,抽屜就只能靠滑動開啟,桌上型模擬器或平板上的操作體驗會打折扣。
RecyclerView 進階:ItemTouchHelper 實作拖曳排序與側滑刪除
前言 RecyclerView 的應用越來越廣,不同場景往往需要不同的互動。比如很多論壇首頁的側滑刪除,或是長按某個項目後拖曳與其他項目交換位置。RecyclerView
Android 循環滾動廣告條:封裝簡便、平滑過渡、可載入網路圖片並點擊跳轉
今天來點乾貨——一個橫向循環滾動的廣告條。 它有點類似淘寶的 banner 廣告位:既能手勢滑動,也會依固定時間間隔自動滾動,滑到盡頭時還能無縫循環,過渡非常平滑。圖片從
Android 監聽 ScrollView 的滑動事件
開發時常需要監聽 ScrollView 的滑動情況,例如滑動了多少距離、是否滑到頂部或底部。可惜 SDK 並未直接提供對外的方法,雖然 ScrollView 內部有一個
Android 螢幕適配完全整理:從 px、dpi 到資源目錄的取捨
一、幾個必須先搞清楚的基礎量 真實長度:英寸(inch)。 密度 dpi:每英寸的像素數,也常被叫作 density。 像素 px:圖片與螢幕的物理點。 dip(dp