安卓导航抽屉(呼叫活动)与AbstractMainActivity [英] Android Navigation Drawer (calling activities) with AbstractMainActivity

查看:196
本文介绍了安卓导航抽屉(呼叫活动)与AbstractMainActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有它创建的导航抽屉AbstractMainActivity。在那里,我也应该处理的菜单项的点击,然后调用新的活动。在这些活动中,我想再次使用相同的导航抽屉。

I want to have a AbstractMainActivity which creates the Navigation Drawer. In there I should also handle the clicks on the menu items and then call new activities. In those activities, I want to again use the same Navigation Drawer.

我将扩大在子类中的AbstractMainActivity并从每个子类中调用getLayoutResourceID不同(如下建议:<一href="http://stackoverflow.com/questions/8821240/android-how-to-create-my-own-activity-and-extend-it">android如何创建自己的活动,并延长它?)。

I would extend in the Subclasses with the AbstractMainActivity and call the getLayoutResourceID differently from each subclass (as suggested here: android how to create my own Activity and extend it ?).

现在的问题是,现在在我想建立导航抽屉的AbstractMainActivity,我没有任何访问导航抽屉布局(XML)元素,我当然希望有针对不同的基地布局子类。

The problem is, that now in the AbstractMainActivity where I want to build the Navigation Drawer, I do not have any access to the navigation drawer layout (xml) element, as I of course want to have a different base layout for the subclasses.

,我需要在所有的子类布局文件,以包括布局?但是,这并不正常工作,我该怎么办错了,如果我想用碎片的活动,而不是使用导航抽屉?

Would I need to "include layout" in all the subclasses layout files? But this does not work, what do I do wrong if I want to use Activities instead of Fragments with the Navigation Drawer?

public abstract class MainActivity extends Activity {

private String[] menuItems;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    setContentView(getLayoutResourceId());

    menuItems = getResources().getStringArray(R.array.menu_items);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItems));
    // Set the list's click listener
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

}


protected abstract int getLayoutResourceId();

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        selectItem(position);
    }

    /** Swaps fragments in the main content view */
    private void selectItem(int position) {
        //Fragment fragment = new PlanetFragment();
        Bundle args = new Bundle();
       // args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);

        Intent intent = new Intent(MainActivity.this, ProductListActivity.class);
        startActivity(intent);

    }
}




public class ProductListActivity extends MainActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.options_menu, menu);
    return true;
}

@Override
protected int getLayoutResourceId() {
    // TODO Auto-generated method stub
    return R.layout.activity_product_list;
}

这是产品列表子类的布局(activity_product_list.xml):

This is the layout of the product list sub-class (activity_product_list.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ProductList" >

<include layout="@layout/activity_main"/>

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
</ListView>

这是抽屉式导航栏的布局(activity_main.xml):

This is the layout of the navigation drawer (activity_main.xml):

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="300dp"
android:layout_height="500dp" >

<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#c3c3c3"/>

但不能正常工作,但如果我没有它,我得到空指针异常时,我的子类叫抽象类,在这里我想建立导航抽屉的OnCreate,它没有找到的布局 - 元件设置列表和布局(R.id.left_drawer或R.id.drawer_layout)!

But the does not work, but if I do not have it, I get null-pointer exceptions when my subclass calls the onCreate of the abstract class, where I want to build the Navigation Drawer, it does not find the layout-elements to set the lists and layout (R.id.left_drawer or R.id.drawer_layout)!

推荐答案

我也试图找出如何做到这一点。

I'm also trying to figure out how to do this.

我看到一个非常好的教程,不正是你想要 这里 。 我们的想法是创建一个抽象类的活动 AbstractNavDrawerActivity 的带抽屉的所有活动将继承。此类使用 NavDrawerActivityConfiguration bean类保存所有有关的资产净值抽屉包括需要被充气的布局信息

I've seen a very good tutorial that does exactly what you want here. The idea is to create an abstract activity class AbstractNavDrawerActivity that all activities with drawers will inherit from. This class uses a NavDrawerActivityConfiguration bean class that holds all the information about the nav drawer including the layout that needs to be inflated

另一种方法是创建一个 NavDrawerUtil 类,你会在这里把静态方法,与NAV抽屉互动。这样,你会因为你需要调用每个活动中的这些方法。

Another approach would be creating a NavDrawerUtil class where you would put static methods that interact with the nav drawer. You would then call these methods from each activity as you need.

第二种方法为您提供了更多的灵活性,你不担心为了布局通货膨胀和这样的,但我认为这是一个不太干净的解决方案比第一个与 AbstractNavDrawerActivity 与导航抽屉里的所有活动继承了像你这样的建议。

The second approach gives you more flexibility and you don't have to worry about order of layout inflations and such but I think it's a less clean solution than the first one with the AbstractNavDrawerActivity that all activities with nav drawer inherit from like you suggested.

这篇关于安卓导航抽屉(呼叫活动)与AbstractMainActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆