【问题标题】:Android Navigation Drawer (calling activities) with AbstractMainActivity带有 AbstractMainActivity 的 Android Navigation Drawer(调用活动)
【发布时间】:2013-09-09 20:48:11
【问题描述】:

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

我将使用 AbstractMainActivity 扩展子类,并以与每个子类不同的方式调用 getLayoutResourceID(如此处建议:android how to create my own Activity and extend it?)。

问题是,现在在我想要构建导航抽屉的 AbstractMainActivity 中,我无法访问导航抽屉布局 (xml) 元素,因为我当然希望为子类。

我需要在所有子类布局文件中“包含布局”吗?但这不起作用,如果我想在 Navigation Drawer 中使用活动而不是 Fragments,我做错了什么?

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)的布局:

<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):

<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)!

【问题讨论】:

  • 你最后做了什么?我也有同样的问题。
  • @mattblang 关注this 链接。

标签: android navigation-drawer


【解决方案1】:

我也在想办法做到这一点。

我看过一个非常好的教程,它完全符合您的要求here。 这个想法是创建一个抽象活动类AbstractNavDrawerActivity,所有带有抽屉的活动都将继承自该类。这个类使用一个NavDrawerActivityConfiguration bean 类,它包含有关导航抽屉的所有信息,包括需要膨胀的布局

另一种方法是创建一个NavDrawerUtil 类,您可以在其中放置与导航抽屉交互的静态方法。然后,您可以根据需要从每个活动中调用这些方法。

第二种方法为您提供了更大的灵活性,您不必担心布局膨胀的顺序等问题,但我认为它比第一种方法不太干净,所有带有导航抽屉的活动都继承自 AbstractNavDrawerActivity就像你建议的那样。

【讨论】:

  • stackoverflow.com/a/4922740/3060087 是使用相同方法的更简单的解决方案。这个例子是为了在每个 Activity 中实现相同的滑动抽屉,但是可以通过简单地将 DrawerActivity 中的 LinearLayout 替换为 DrawerLayout 来调整导航抽屉。
【解决方案2】:

我在以前的应用程序中所做的是将滑动菜单构建为抽象活动中的函数调用。当您设置一个扩展抽象活动的新活动时,您在 onCreate() 中执行函数调用。我目前正在使用 Navigation Drawer 进行类似的实现,所以我不太确定它是否有效,但它可能是您开始的好地方。您所有将调用 Navigation Drawer 的活动都需要将 DrawerLayout 作为顶级布局元素。

【讨论】:

  • 谢谢,所以我的方法是朝着正确的方向前进,在子活动布局中包含抽屉布局?然后我可以在抽象类中设置导航抽屉菜单,但不知何故“包含”还不起作用。最坏的情况是我在布局中有代码重复,但至少在活动代码中没有。
  • 对。我实际上刚刚实现了这个并且它有效。每个新的活动布局都需要具有 DrawerLayout 顶层和包含的抽屉元素。您应该为抽屉元素提供与抽象布局中相同的 id,以便您可以重用从抽屉的抽象活动继承的函数调用。不幸的是,由于 Android 的设计方案似乎正在从多个 Activity 转变为一个具有多个片段的 Activity,因此我认为他们不会很快添加“包含”功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多