[英]Extended navigation drawer can't change activity. What's going on?
我创建了带有导航抽屉的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();
}
}
}
我不能简单地在CheckList中将类更改为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>
你不能因为你的充气抽屉上的列表项单击CheckList
直接活动的布局到DrawerLayout
中的BaseActivity
。 这导致它在所有DrawerLayout
的子View
的“顶部”,并且您的点击不会传播到下面的抽屉ListView
。 相反,你要夸大CheckList
的布局到content_frame
FrameLayout
的内DrawerLayout
。
在BaseActivity
类中,为FrameLayout
添加一个protected
字段,并像其他View
一样对其进行初始化。
public class BaseActivity extends AppCompatActivity {
protected DrawerLayout drawerLayout;
protected ListView drawerList;
protected FrameLayout contentFrame;
...
protected void initDrawer() {
setContentView(R.layout.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);
contentFrame = (FrameLayout) findViewById(R.id.content_frame);
...
}
}
然后按如下所示更改CheckList
中的CheckList
inflate()
调用:
getLayoutInflater().inflate(R.layout.checklist, contentFrame);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.