Android:任何活动的导航抽屉菜单

时间:2015-04-16 07:57:54

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

我使用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>

UpdateActivity 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

3 个答案:

答案 0 :(得分:0)

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

也许您应该在父活动中实现这样的抽象函数:

public abstract int getContentView();

并替换你的:

setContentView(R.layout.main_layout)

有:

setContentView(getContentView());

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

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

此代码允许您从子项分配布局xml,父级可以访问它。如果未实现此操作,则无法从父活动访问配置导航抽屉的视图。

答案 1 :(得分:0)

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

您也可以尝试使用一些可以帮助您轻松实现NavigationDrawer的库,查看Android Arsenal 滑动面板部分。

答案 2 :(得分:0)

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

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

在RelativeLayout里面用&#34; @ + id / container&#34; name,您可以添加任何所需的内容并为每个活动进行自定义,并在子活动中进行实例化。