在我的導航抽屜中我遇到了一個問題,當它在第一個位置加載任何內容會導致應用程序崩潰,所以我的解決方案是將其設置爲更改爲「(appname)免費「或」(appname)高級版「,具體取決於是否購買了高級升級。在導航抽屜中禁用項目
我想這是不可點擊的,因爲它目前能夠點擊但沒有任何反應。理想情況下,這將是一個子菜單或標題,但我無法弄清楚如何實現它。這裏是我的代碼摘錄:
public class myClass extends SherlockActivity implements
OnItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
mTitle = mDrawerTitle = getTitle();
if (mIsPremium == true) {
mPlanetTitles = getResources().getStringArray(
R.array.planets_array_prem);
} else {
mPlanetTitles = getResources()
.getStringArray(R.array.planets_array);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:
/*
* Toast.makeText(getApplicationContext(),
* "case 0 - A Activity.", Toast.LENGTH_LONG).show(); Intent
* c0 = new Intent(getBaseContext(),
* activity.class); startActivity(c0);
*/break;
case 1:
Toast.makeText(getApplicationContext(),
"case 1 - B Activity", Toast.LENGTH_LONG).show();
Intent c1 = new Intent(getBaseContext(),
ActivityB.class);
startActivity(c1);
break;
default:
}
}
}
public void selectItem(int position) {
switch (position) {
case 0:
// setContentView(R.layout.main);
break;
case 1:
setContentView(R.layout.main);
break;
default:
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
我不使用的片段,我會做的點擊和移動到新的活動來實現導航的抽屜裏。我正在尋找解決我的主要問題,但會歡迎任何設計技巧。
謝謝
編輯
隨着CommonsWare的幫助下,我開發了這個:但沒有什麼是我的應用程序不同。我想第一次(情況0(位置0))被禁用。
public class MyArrayAdapter extends ArrayAdapter<String> {
public MyArrayAdapter(Context context, int position) {
super(context, 0);
}
public boolean areAllItemsEnabled() {
return false;
}
public boolean isEnabled(int position) {
if (position == 0) {
return false;
} else {
return true;
}
}
}
是的,我沒有看到這些問題的答案,但我很困惑,以我將如何做到這一點。我是新手,偶然發現一些事情,需要別人的幫助,這是其中之一。我感謝你的回答,並會嘗試,但如果你有一個非常棒的例子。我見過的每一個人總是發起一個新課,如果能夠的話,我想保留在當前課上。 – user1363871
@ user1363871:「我見過的每個人總是發起一個新班級,如果能夠的話,我想保留在當前班級中」 - 你無法做到。重寫方法的唯一方法是通過一個子類。我不清楚你需要什麼,而不是在http://stackoverflow.com/a/4636538/115145 – CommonsWare
中的第一個代碼片段沒有涉及的例子中,我創建了一個擴展String ArrayAdapter的ArrayAdapter。我把areAllItemsEnabled和isEnabled放在一起,最終的結果是沒有什麼不同。我將用新的ArrayAdapter編輯原始問題。謝謝你的幫助。 – user1363871