从活动类打开Android导航抽屉

时间:2013-07-22 04:20:57

标签: java android-layout android-intent android-listview

我正在研究android Navigation Drawer并且通过他们的文档看起来,抽屉只能扩展Fragment Activity,以便从我的所有活动中打开抽屉,我需要将我的所有活动都变成一个片段,不是一个可行的解决方案。

有没有办法可以打开从活动中扩展FragmentActivity的抽屉?

当我尝试从Activity类扩展抽屉活动时,以及另一个将打开抽屉扩展draweractivity类(此处为SlideMenuActivity)的活动时,应用程序崩溃并发出NullPointerException。

下面是打开抽屉布局的代码,但是第一个活动启动后,我无法访问抽屉。

应用程序在onPostCreate方法

上的syncState点崩溃
@Override
protected void onPostCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onPostCreate(savedInstanceState);
    getActionDrawerToggle().syncState();
}

public class SlideMenuActivity extends FragmentActivity implements OnItemClickListener


{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setDrawerLayout();
        setDrawerList();
        if (savedInstanceState == null) 
        {
            getDrawerListView().setSelectionAfterHeaderView();
            getDrawerListView().setSelection(1);
            selectItem(1);
        }
    }
//  
//  @Override
//  public void setContentView(int layoutResID) {
//      // TODO Auto-generated method stub
//      super.setContentView(layoutResID);
//  }

    private DrawerLayout getDrawerView()
    {
        return (DrawerLayout)findViewById(R.id.drawer_layout);
    }

    private ListView getDrawerListView()
    {
        return (ListView) findViewById(R.id.left_drawer);
    }

    private ActionBarDrawerToggle getActionDrawerToggle()
    {
        ActionBarDrawerToggle drawerToggle=new ActionBarDrawerToggle(
                this,                  /* host Activity */
                getDrawerView(),         /* 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 */
                ) {
            @Override
            public void onDrawerClosed(View view) {
               super.onDrawerClosed(view);
            }

            @Override
            public void onDrawerOpened(View view) {
                super.onDrawerOpened(view);
            }
        };
        return drawerToggle;
    }

    private void setDrawerLayout(){
        // set a custom shadow that overlays the main content when the drawer opens
        getDrawerView().setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK);
        // enable ActionBar app icon to behave as action to toggle navigation drawer
        getDrawerView().setDrawerListener(getActionDrawerToggle());

    }

    /**
     *  Set up the drawer's list view with items and click listener
     */
    private void setDrawerList()
    {
        ImageView imageView=new ImageView(this);
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.precision_logo));
        CustomBaseAdapter adapter=new CustomBaseAdapter();
        adapter.list=getListViewData();
        adapter.context=this;
        ListView drawerList=getDrawerListView();
        drawerList.setHeaderDividersEnabled(true);
        drawerList.addHeaderView(imageView, null, false);
        drawerList.setScrollingCacheEnabled(false);
        drawerList.setAdapter(adapter);
        drawerList.setOnItemClickListener(this);

    }

    private void selectItem(int position){

         // update the main content by replacing fragments

//        Fragment fragment=null;
//      FragmentManager manager=getSupportFragmentManager();
        switch (position) {
        case 1:
            this.startActivity(new Intent(this,SavedTankListActivity.class));
//          fragment = new SavedMixesFragment();
//          fragment = new SavedTankListActivity();
//          manager.beginTransaction().replace(R.id.content_frame, fragment).commit();
            break;

        case 2:
//          fragment=new MixGuideFragment();
//          manager.beginTransaction().replace(R.id.content_frame, fragment).commit();
            break;

        default:
//          fragment = new SavedMixesFragment();
//          manager.beginTransaction().replace(R.id.content_frame, fragment).commit();

            break;
        }

        getDrawerView().closeDrawer(getDrawerListView());

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onPostCreate(savedInstanceState);
        getActionDrawerToggle().syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        getActionDrawerToggle().onConfigurationChanged(newConfig);
    }

    private ArrayList<DrawerListModel> getListViewData()
    {
        ArrayList<DrawerListModel> listViewData=new ArrayList<DrawerListModel>();
        String[] listItemArray=getResources().getStringArray(R.array.slide_bar_list_item_array);
        for(int index=0;index<listItemArray.length;index++)
        {
            DrawerListModel model=new DrawerListModel();
            model.listItem=listItemArray[index];
            listViewData.add(model);
        }
        return listViewData;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id)
    {
        view.setSelected(true);
        selectItem(position); 
    }

    public void openDrawerList(View view){
        getDrawerView().openDrawer(getDrawerListView());
    }
}

3 个答案:

答案 0 :(得分:1)

我也在查看文档,并认为我必须将所有活动切换为片段。只是为了澄清它,事实并非如此。您可以在线性布局下或根据需要使用任意数量的元素。

例如,如果没有appdrawer的基本活动是:

<RelativeLayout>
   <TextView>
   </TextView>
   <Button>
   <Button>
</RelativeLayout>

简单地做

<android.support.v4.widget.DrawerLayout>

    <RelativeLayout>
       <TextView>
       </TextView>
       <Button>
       <Button>
    </RelativeLayout>

    <ListView>
    </ListView>
</android.support.v4.widget.DrawerLayout>

答案 1 :(得分:0)

据说你可以有一个主要的活动,你把导航抽屉和所有子活动类都用来使用抽屉,但我不明白的是你如何避免重复包含所有子活动的抽屉的布局。 / p>

答案 2 :(得分:0)

导航抽屉不需要在片段中。您可以创建一个类,然后从您的所有活动中扩展(正如您尝试的那样)。

public class SlideMenuActivity extends Activity{..}

您的主要活动看起来像

public class MainActivity extends SlideMenuActivity{..}

SlideMenuActivity的实现方式与creating a navigation activity中描述的相同。

您的所有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"
android:background="#FFFFFF" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="#CCCCCC"
    android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

如果没有看到您的logcat输出,我不确定您收到错误的原因,但希望这可以帮助您了解更多导航抽屉代码。