扩展导航抽屉无法更改活动。发生了什么事?

时间:2015-12-04 07:01:00

标签: android android-intent android-activity navigation-drawer

我创建了一个带导航抽屉的BaseActivity类,另一个类CheckList将BaseActivity扩展为导航抽屉。

在BaseActivity中,我可以通过导航抽屉将活动更改为CheckList。 但是在CheckList中。即使我可以看到抽屉,它也不适合改变课程。

BaseActivity:

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();
    }
}
}

清单:

<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中将类更改为MainActivity。但是在BaseActivity中,我可以更改为MainActivity和CheckList。

非常感谢!!

更新 这是我的drawer.xml文件

Node

1 个答案:

答案 0 :(得分:2)

您无法点击抽屉的列表项,因为您将CheckList活动的布局直接膨胀到DrawerLayout的{ {1}}。这导致它在#34;顶部&#34;在所有BaseActivity的孩子DrawerLayout中,您的点击次数不会传播到下方的抽屉View。相反,您希望将ListView的布局扩展为CheckList中的content_frame FrameLayout

DrawerLayout课程中为BaseActivity添加protected字段,并像其他FrameLayout一样对其进行初始化。

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); ... } } 中的inflate()来电,如下所示:

CheckList