我已經創建了一個具有導航抽屜的BaseActivity類,另一個類CheckList擴展了BaseActivity以具有導航抽屜。擴展導航抽屜不能更改活動。這是怎麼回事?
在BaseActivity中,我可以通過導航抽屜將活動更改爲CheckList。 但在CheckList。即使我可以看到抽屜,這對於改變課堂並不奏效。
BaseActivity:
public class BaseActivity extends AppCompatActivity{
protected DrawerLayout drawerLayout;
protected ListView drawerList;
protected ActionBarDrawerToggle drawerToggle;
protected CharSequence mDrawerTitle;
protected CharSequence mTitle;
protected String[] drawer_menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initActionBar();
initDrawer();
initDrawerList();
}
protected void initActionBar() {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
}
protected void initDrawer() {
setContentView(R.layout.drawer);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerList = (ListView)findViewById(R.id.left_drawer);
mTitle = mDrawerTitle = getTitle();
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle(mTitle);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(mDrawerTitle);
}
};
drawerToggle.syncState();
drawerLayout.setDrawerListener(drawerToggle);
}
protected void initDrawerList() {
drawer_menu = this.getResources().getStringArray(R.array.drawer_menu);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawer_menu);
drawerList.setAdapter(adapter);
drawerList.setOnItemClickListener(new DrawerItemClickListener());
}
//respond when click the ic_drawer
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
protected void selectItem(int position) {
//Fragment fragment = null;
switch (position) {
case 0:
break;
case 1:
Intent main = new Intent();
main.setClass(getApplicationContext(), MainActivity.class);
startActivity(main);
finish();
break;
case 2:
Intent checklist = new Intent();
checklist.setClass(getApplicationContext(), CheckList.class);
startActivity(checklist);
finish();
break;
default:
break;
}
/*FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();*/
drawerList.setItemChecked(position, true);
setTitle(drawer_menu[position]);
drawerLayout.closeDrawer(drawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
清單:
public class CheckList extends BaseActivity implements OnClickListener {
Button addBtn;
ListView listView;
ArrayList<String> checkListItem;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.checklist, drawerLayout);
addBtn = (Button)findViewById(R.id.addBtn);
listView = (ListView)findViewById(R.id.listItems);
addBtn.setOnClickListener(this);
checkListItem = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, checkListItem);
listView.setAdapter(adapter);
checkListItem.add(getString(R.string.cl_passport));
checkListItem.add(getString(R.string.cl_charger));
checkListItem.add(getString(R.string.cl_camera));
registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.checklist_contextmenu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case R.id.cl_contextmenu_delete:
checkListItem.remove(info.position);
adapter.notifyDataSetChanged();
return true;
case R.id.cl_contextmenu_edit:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(CheckList.this);
alertDialog.setTitle(getString(R.string.cl_editItem));
final EditText edit = new EditText(this);
edit.setText(checkListItem.get(info.position));
edit.setSelectAllOnFocus(true);
alertDialog.setView(edit);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkListItem.set(info.position, edit.getText().toString());
adapter.notifyDataSetChanged();
}
});
alertDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.create();
alertDialog.show();
return true;
default:
return super.onContextItemSelected(item);
}
}
@Override
public void onClick(View v) {
if(v==this.addBtn) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(CheckList.this);
alertDialog.setTitle(getString(R.string.cl_input));
final EditText checkListInput = new EditText(this);
alertDialog.setView(checkListInput);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkListItem.add(checkListInput.getText().toString());
}
});
alertDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.create();
alertDialog.show();
}
}
}
我不能簡單地類變更爲MainActivity的清單。但在BaseActivity中,我可以更改爲MainActivity和CheckList。
非常感謝!
更新 這是我drawer.xml文件
<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" >
<!-- Add content here -->
</FrameLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#e5e5e5"
android:dividerHeight="1dp"
android:background="#d6d6d6"/>
</android.support.v4.widget.DrawerLayout>
你充氣'R.layout.checklist'到'drawerLayout',這是在抽屜裏添加新的佈局,關於z順序。您需要將其膨脹到DrawerLayout中的內容ViewGroup中。 –
我該怎麼辦?我在編寫android時是新的....你能向我解釋更多嗎? – rascee
請編輯您的問題以添加您的'抽屜'佈局XML文件,我可以更具體。 –