【问题标题】:Android: Navigation drawer menu on any activitiesAndroid:任何活动上的导航抽屉菜单
【发布时间】:2015-04-16 07:57:53
【问题描述】:

我使用ExpandableListView 制作了一个左侧菜单,这个菜单在我的MainActivity 上效果很好。现在我想在我的其他活动中使用它,我该如何简单地做到这一点?

我应该只为菜单创建一个类还是可以重复使用我的MainActivity

这是我MainActivity的代码:

public class MainActivity extends BaseMenuActivity {

public static final String SAVE_VALUES = "SaveFile";
static String saveDirectory = String.valueOf(Environment.getExternalStorageDirectory());
static String backgroundPath;
static ImageView layout;

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

    layout = (ImageView) findViewById(R.id.background);
    SharedPreferences setting = getSharedPreferences(SAVE_VALUES,0);
    String backgrounImage = setting.getString("backgroundImage","bc_lip");
    backgroundPath = saveDirectory + "/galaxyv2Img/" + backgrounImage + ".jpg";
    Drawable background = Drawable.createFromPath(backgroundPath);
    layout.setBackgroundDrawable(background);

    UpdateDatabaseCsv tt = new UpdateDatabaseCsv(MainActivity.this);
    tt.open();
    tt.close();

}

@Override
public int getContentView() {
    return R.layout.activity_main;
}


@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Intent i = new Intent(this, SettingActivity .class);
        startActivity(i);
    }

    return super.onOptionsItemSelected(item);
}

我的更新活动:

public class UpdateActivity extends BaseMenuActivity {
public static final String SAVE_VALUES = "SaveFile";
public final String downloadDirectory = Environment.getExternalStorageDirectory() + "/galaxyv2/download/";
ProgressDialog barProgressDialog;
Button btnStartProgress;
FtpGetter ftp;
int progressBarStatus = 0;
String ftpLink, ftpPassword, ftpHost, ftpUser;
FilesManager filesManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_update);
    filesManager = new FilesManager(this);
    Intent intent = getIntent();
    String value = intent.getStringExtra("todo");
    ftp = new FtpGetter();
}

@Override
public int getContentView() {
    return R.layout.activity_update;
}

MainActivity 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">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/background"
    android:scaleType="fitXY"
    />



<!-- ExpandableListview to display slider menu -->
<ExpandableListView
    android:id="@+id/list_slidermenu"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"        
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background">
</ExpandableListView>

更新活动 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">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/update_start_update"
        android:id="@+id/button_update_start"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    </RelativeLayout>


<!-- ExpandableListview to display slider menu -->
<ExpandableListView
    android:id="@+id/list_slidermenu"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background">
</ExpandableListView>

问题是我不能使用旧的 UpdateActivity xml 和 RelativeLayout

【问题讨论】:

  • 所以您想在所有活动中重复使用菜单?
  • 是的,这正是我想做的事情

标签: android android-activity expandablelistview navigation-drawer main-activity


【解决方案1】:

您应该使用有关横向菜单的登录名创建一个活动并调用 BaseMenuActivity.class(例如)。您希望拥有此菜单的所有活动都应从 de BaseMenuActivity 扩展,并且内容视图应具有相同的组件。

也许你应该在父活动中实现一个像这样的抽象函数:

public abstract int getContentView();

并替换您的:

setContentView(R.layout.main_layout)

为:

setContentView(getContentView());

在您的子活动中实现此功能

 @Override
    public int getContentView() {
        return R.layout.layout;
    }

此代码允许您从孩子分配布局 xml,并且父母可以访问它。如果您不实现此功能,则无法从父 Activity 访问视图以配置导航抽屉。

【讨论】:

  • 正如你告诉我的那样,我创建了一个“BaseMenuActivity 扩展 Activity”,现在我有了: MainActivity 扩展 BaseMenuActivity 我的菜单函数和变量在 BaseMenuActivity 中,但是当我使用 Intent 时出现错误,例如 new意图(CurrentActivity.class,NewActivity.class)
  • 你应该只为“this”或getApplicationContext()更改CurrentActivity.class;
  • 它在我的 MainActivity 上运行良好,但是当我想更改活动时,setUpDrawer 函数会为前十个在线创建错误,我不明白为什么它适用于 Main 而不适用于其他人
  • 如果你想改变 BaseActivity 中的 setUpDrawer 函数或其他函数,你应该在子 Activity 中重写。如果您想更改您的可扩展列表视图数据(例如)创建和抽象函数,例如名为 configureMenuData 的 getContentView,并在子进程中实现有关配置可扩展列表的代码,例如创建适配器等。您必须清楚代码对所有活动都是通用的,并且该代码将可定制。玩这个,然后再试一次
  • 如果您觉得制作起来并不容易,您可以使用处理该问题的库。如果您愿意,请使用您的新课程和逻辑更新您的帖子,我会尽力帮助您
【解决方案2】:

考虑浏览Google I/O 2014 Source Code - 他们已经使用 BaseActivity 类实现了 NavigationDrawer。

您也可以尝试使用一些可以帮助您轻松实现 NavigationDrawer 的库,请查看 Android ArsenalSliding Panels 部分。

【讨论】:

    【解决方案3】:

    如果您想为每个活动自定义包含布局,我认为您应该使用以下内容:

    //update_activity.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">
    
        <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
    
        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
    
            <!-- As the main content view, the view below consumes the entire
                 space available using match_parent in both dimensions. -->
    
    
                <RelativeLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    >
                    //change this with your views for each activity
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/customImageView"
                        />
                </RelativeLayout>
    
    
    
            <!-- android:layout_gravity="start" tells DrawerLayout to treat
                 this as a sliding drawer on the left side for left-to-right
                 languages and on the right side for right-to-left languages.
                 If you're not building against API 17 or higher, use
                 android:layout_gravity="left" instead. -->
            <!-- The drawer is given a fixed width in dp and extends the full height of
                 the container. -->
           <!-- ExpandableListview to display slider menu -->
    <ExpandableListView
        android:id="@+id/list_slidermenu"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background">
    
        </android.support.v4.widget.DrawerLayout>
    </RelativeLayout>
    

    //UpdateActivity.class

    public class UpdateActivity extends BaseMenuActivity {
    public static final String SAVE_VALUES = "SaveFile";
    public final String downloadDirectory = Environment.getExternalStorageDirectory() + "/galaxyv2/download/";
    ProgressDialog barProgressDialog;
    Button btnStartProgress;
    FtpGetter ftp;
    int progressBarStatus = 0;
    String ftpLink, ftpPassword, ftpHost, ftpUser;
    FilesManager filesManager;
    ImageView customImageView,
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_update);
        filesManager = new FilesManager(this);
        Intent intent = getIntent();
        String value = intent.getStringExtra("todo");
        ftp = new FtpGetter();
        customImageView = (ImageView) findViewById(R.id.customImageView);
     }
    
    @Override
    public int getContentView() {
        return R.layout.activity_update;
    }
    

    在具有“@+id/container”名称的RelativeLayout 中,您可以添加任何您想要的内容并为每个活动自定义并在子活动中实例化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2014-08-22
      • 2014-10-29
      • 1970-01-01
      相关资源
      最近更新 更多