0

我已經創建了一個具有導航抽屜的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> 
+0

你充氣'R.layout.checklist'到'drawerLayout',這是在抽屜裏添加新的佈局,關於z順序。您需要將其膨脹到DrawerLayout中的內容ViewGroup中。 –

+0

我該怎麼辦?我在編寫android時是新的....你能向我解釋更多嗎? – rascee

+0

請編輯您的問題以添加您的'抽屜'佈局XML文件,我可以更具體。 –

回答

1

你不能因爲你直接充脹CheckList活動的佈局到BaseActivityDrawerLayout抽屜的列表項點擊。這會導致它在所有DrawerLayout的小孩View s的「頂部」,並且您的點擊不會傳播到底部的抽屜ListView。相反,您要將CheckList的佈局擴展到DrawerLayout內的content_frameFrameLayout

BaseActivity類中爲FrameLayout添加protected字段,並將其初始化爲與其他View s類似。

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如下:

getLayoutInflater().inflate(R.layout.checklist, contentFrame); 
+0

非常感謝!它解決了我的問題!我想更多地瞭解這一點。這裏的FrameLayout是什麼意思?這是否意味着我有3層。最低的是帶有FrameLayout的抽屜。中間一個是checklist.xml,我把它放在drawer.xml的頂部。頂層是抽獎項目?所以我可以直接傳播給該項目?對不起,問這個愚蠢的問題......;( – rascee

+0

我可能不會很好地解釋這一點,但:DrawerLayout是您的Activity的根視圖;主父View,它包含FrameLayout和它的內容抽屜,在這種情況下是ListView,按照順序將視圖添加到它們的父級,因此ListView是最後一個,位於FrameLayout的頂部,您可以點擊它。當您將CheckList的佈局充填到FrameLayout中時,其視圖現在是FrameLayout的子項,並位於抽屜下方。你是在根視圖和抽屜之間插入CheckList的視圖。 –