效果
使用最新的Android-support-v4.jar
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2013年資料/6月/4日/Android 抽屜導航
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<
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 -->
...
<!-- The navigation drawer -->
<
ListView
android:id
=
"@+id/left_drawer"
android:layout_width
=
"120dp"
android:layout_height
=
"match_parent"
android:layout_gravity
=
"start"
android:choiceMode
=
"singleChoice"
android:divider
=
"@android:color/transparent"
android:dividerHeight
=
"0dp"
android:background
=
"#EEA9B8"
/>
</
android.support.v4.widget.DrawerLayout
>
/** 抽屜導航變量 */
private
DrawerLayout mDrawerLayout;
private
ListView mDrawerList;
private
ActionBarDrawerToggle mDrawerToggle;
...
/** 抽屜導航相關 */
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.draw, GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK);
mDrawerList.setAdapter(
new
SimpleAdapter(
this
, getDrawerItems(
false
),
R.layout.drawer_item,
new
String[] {
"drawer_img"
,
"drawer_title"
},
new
int
[] { R.id.drawer_img,
R.id.drawer_title }));
mDrawerList.setOnItemClickListener(
new
DrawerItemClickListener());
mDrawerToggle =
new
ActionBarDrawerToggle(
this
,
/** host Activity */
mDrawerLayout,
/** DrawerLayout object */
R.drawable.draw,
/** nav drawer image to replace 'Up' caret */
R.string.search_label,
/** "open drawer" description for accessibility */
R.string.defalut_text
/** "close drawer" description for accessibility */
) {
public
void
onDrawerClosed(View view) {
getActionBar().setTitle(
"onDrawerClosed"
);
invalidateOptionsMenu();
// creates call to onPrepareOptionsMenu()
}
public
void
onDrawerOpened(View drawerView) {
getActionBar().setTitle(
"onDrawerOpened"
);
invalidateOptionsMenu();
// creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
...
/**
* 監聽抽屜導航item點擊事件
*/
private
class
DrawerItemClickListener
implements
ListView.OnItemClickListener {
@Override
public
void
onItemClick(AdapterView<?> parent, View view,
int
position,
long
id) {
selectItem(view, position);
}
}
private
boolean
flag =
false
;
private
void
selectItem(View view,
int
position) {
if
(position ==
0
) {
if
(!flag) {
mDrawerList.setAdapter(
new
SimpleAdapter(
this
, getDrawerItems(
true
),
R.layout.drawer_item,
new
String[] {
"drawer_img"
,
"drawer_title"
},
new
int
[] { R.id.drawer_img,
R.id.drawer_title }));
flag =
true
;
}
else
{
mDrawerList.setAdapter(
new
SimpleAdapter(
this
, getDrawerItems(
false
),
R.layout.drawer_item,
new
String[] {
"drawer_img"
,
"drawer_title"
},
new
int
[] { R.id.drawer_img,
R.id.drawer_title }));
flag =
false
;
}
}
else
{
TextView title = (TextView) view.findViewById(R.id.drawer_title);
String cityName = title.getText().toString();
this
.cityInput.setText(cityName);
}
}
詳見我翻譯的 http://www.linuxidc.com/Linux/2013-06/85406.htm
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11