使用继承的活动导航抽屉实现

Posted

技术标签:

【中文标题】使用继承的活动导航抽屉实现【英文标题】:Navigation drawer implementation with activities using inheritance 【发布时间】:2016-05-20 15:35:15 【问题描述】:

在我的应用程序中,我必须对所有活动使用导航抽屉。所以我创建了一个名为DrawerActivity 的基础活动。我在DrawerActivity 中编写了导航抽屉的代码,然后从DrawerActivity 扩展了UserDashBoardActivity

问题是DrawerActivity 属性没有在UserDashBoardActivity 中执行。我无法在UserDashBoardActivity 中与DrawerActivity 互动。在所有活动中,抽屉菜单都在ActionBar 中。

这是我的DrawerActivity

public class DrawerActivity extends ActionBarActivity 

      @Override
            protected void onCreate(Bundle savedInstanceState) 
                super.onCreate(savedInstanceState);
                setContentView(R.layout.drawer_list_view);

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

                //mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

                mDrawerList.setAdapter(new ArrayAdapter<String>(
                        this, R.layout.drawer_list_items, mDrawerItems));
                mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);

                mDrawerToggle = new ActionBarDrawerToggle(this,
                        mDrawerLayout, R.drawable.ic_menu,
                        R.string.drawer_open, R.string.drawer_close) 
                    public void onDrawerOpened(View view) 
                        invalidateOptionsMenu();
                    

                    public void onDrawerClosed(View view) 
                        invalidateOptionsMenu();
                    
                ;
                mDrawerLayout.setDrawerListener(mDrawerToggle);
            

            @Override
            protected void onPostCreate(Bundle savedInstanceState) 
                super.onPostCreate(savedInstanceState);
                mDrawerToggle.syncState();
            

            @Override
            public void onConfigurationChanged(Configuration newConfig) 
                super.onConfigurationChanged(newConfig);
                mDrawerToggle.onConfigurationChanged(newConfig);
            

            @Override
            public boolean onPrepareOptionsMenu(Menu menu) 
                boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

                for (int index = 0; index < menu.size(); index++) 
                    MenuItem menuItem = menu.getItem(index);
                    if (menuItem != null) 
                        // hide the menu items if the drawer is open
                        menuItem.setVisible(!drawerOpen);
                    
                

                return super.onPrepareOptionsMenu(menu);
            


            @Override
            public boolean onOptionsItemSelected(MenuItem item) 
                if (mDrawerToggle.onOptionsItemSelected(item)) 
                    return true;
                
                return super.onOptionsItemSelected(item);
            

            private class DrawerItemClickListener implements ListView.OnItemClickListener 

                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) 
                    switch (position) 
                        case 0: 
                            Intent intent = new Intent(DrawerActivity.this, UserDashBoardActivity.class);
                            startActivity(intent);
                            break;
                        
                        case 1: 
                            Intent intent = new Intent(DrawerActivity.this, AdmissionActivity.class);
                            startActivity(intent);
                            break;
                        
                        default:
                            break;
                    
                    mDrawerLayout.closeDrawer(mDrawerList);
                
            

这是我的UseDashBoardActivity

public class UserDashBoardActivity extends DrawerActivity 


    private Context context;
    private ImageButton searchBtn;
    private ImageButton favBtn;
    private ImageButton profileBtn;
    private ImageButton reminderBtn;
    private ImageButton logoutBtn;
    private ImageButton notificationBtn;
    private ImageView seatchIcon;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    @Override
    protected void onStart() 
        super.onStart();
        AppActivityStatus.setActivityStarted();
        AppActivityStatus.setActivityContext(context);
    

    @Override
    protected void onPause() 
        super.onPause();
        AppActivityStatus.setActivityStoped();

    

    @Override
    protected void onResume() 
        super.onResume();
        AppActivityStatus.setActivityStarted();
    

    @Override
    protected void onStop() 
        super.onStop();
        AppActivityStatus.setActivityStoped();
    


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

    // delete the selected event from event list added here
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 

        switch (item.getItemId()) 
            case R.id.action_notify:
                return true;

            case R.id.action_favourite:
                return true;
            case R.id.action_navigation:

        

        return super.onOptionsItemSelected(item);
    


    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        getSupportActionBar().setCustomView(R.layout.action_bar_layout);
        setContentView(R.layout.user_dash_board);
        context = getApplicationContext();
        searchBtn = (ImageButton) findViewById(R.id.search_btn);
        favBtn = (ImageButton) findViewById(R.id.fav_btn);
        profileBtn = (ImageButton) findViewById(R.id.profile_btn);
        reminderBtn = (ImageButton) findViewById(R.id.reminder_btn);
        notificationBtn = (ImageButton) findViewById(R.id.notification_btn);
        logoutBtn = (ImageButton) findViewById((R.id.logout_btn));
        final EditText Search = (EditText)findViewById(R.id.emailAddress);
        mDrawerList = (ListView)findViewById(R.id.drawer_layout);
        searchBtn.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View arg0) 
                Intent regAct = new Intent(getApplicationContext(), SearchActivity.class);
                // Clears History of Activity
                regAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(regAct);
            
        );

        favBtn.setOnClickListener(new View.OnClickListener()
            @Override
            public void onClick(View arg0)
                Intent tabAct = new Intent(getApplicationContext(),TabHostActivity.class);
                tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(tabAct);
            
        );

        profileBtn.setOnClickListener(new View.OnClickListener()
            @Override
            public void onClick(View arg0) 
                Intent tabAct = new Intent(getApplicationContext(),AboutCollegeActivity.class);
                tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(tabAct);

            
        );

    

这是我的操作栏 xml

<?xml version="1.0" encoding="utf-8"?>
<menu

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appmunu="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.after2.svirtzone.after2_gradle.UserDashBoardActivity">
    <item
        android:id="@+id/action_notify"
        android:icon="@drawable/mail_icon"
        appmunu:showAsAction="always"
        android:title="Notification" />
    <item
        android:id="@+id/action_favourite"
        android:icon="@drawable/favourite_icon"
        appmunu:showAsAction="always"
        android:title="Favourite" />

    <item
//this is the menu button for navigation drawer
        android:id ="@+id/action_navigation"
        android:icon="@drawable/ic_menu"
        appmunu:showAsAction = "always"
        android:title="navigation"
        android:layout_gravity="left"/>
</menu>

这是navigationdrawer listview的xml布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_
    android:layout_>
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/activity_frame"
        android:layout_
        android:layout_ ></FrameLayout>
    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_
        android:layout_
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

这个布局是 UserDashboard 布局

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:layout_gravity="center"
    android:background="@color/appblue"
    android:orientation="vertical">

    <EditText
        android:id="@+id/emailAddress"
        android:layout_
        android:layout_
        android:background="@drawable/edit_text_style"
        android:gravity="center|start"
        android:hint="@string/edittext_hint"
        android:inputType="textEmailAddress"
        android:maxLength="40"
        android:textSize="18sp"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/search_icon_btn"
        android:layout_
        android:layout_
        android:layout_marginLeft="580dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="-90dp"
        android:padding="5dp"
        android:src="@drawable/search_icon" />


    <ImageButton
        android:id="@+id/search_btn"
        android:layout_
        android:layout_
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="210dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/search_blue"
        android:gravity="center" />

    <TextView
        android:id="@+id/searchCollege"
        android:layout_
        android:layout_
        android:layout_marginLeft="80dp"
        android:layout_marginRight="100dp"
        android:layout_marginTop="20dp"
        android:text="@string/search_college"
        android:textColor="@color/green"
        android:textSize="30sp" />

    <ImageButton
        android:id="@+id/fav_btn"
        android:layout_
        android:layout_
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="200dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="-305dp"
        android:background="@drawable/fav_blue"
        android:gravity="center" />

    <TextView
        android:id="@+id/myFavourites"
        android:layout_
        android:layout_
        android:layout_marginLeft="500dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:text="@string/my_favourites"
        android:textColor="@color/green"
        android:textSize="30sp" />

</LinearLayout>

UserDashboard 活动按原样执行,但我需要DrawerActivity 的属性与此活动一起执行。怎么做?

【问题讨论】:

提示:回调接口 你能简单解释一下吗 稍等我提供一个简单的示例代码 对不起我的错误。刚才我才知道您正在将 DrawerActivity 扩展到所有其他 Activity。解决方案 在 DrawerActivity 中保护您的变量,以便它们在基类(扩展)中可供您使用。然后,您可以在任何基本活动中访问它们。如果我对问题的理解有误,请告诉我。 用户 0X0nosugar 也给出了一个不错的解决方案。另见 【参考方案1】:

抽屉布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_
    android:layout_
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_
            android:layout_
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            />

		<!--you need to add everything to this frameLayout then only you will be able to access Navigation DrawerLayout-->
        <FrameLayout
            android:id="@+id/fragment"
            android:layout_
            android:layout_></FrameLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/navigation_list"
        android:layout_
        android:layout_
        android:layout_gravity="start"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:fitsSystemWindows="true"
        android:background="@color/white"
        />


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

带抽屉的Activity:(只显示重要代码)

public abstract class BaseDrawerLayoutActivity extends AppCompatActivity implements OnItemClickListener 

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.base_drawer_layout_activity);
    //rest of the things
    //NOTE: loadNavMenu is abstract so that you can implement it in other activities as per you need, otherwise remove this and load it here itself
    loadNavMenu();


protected abstract void loadNavMenu();

// call this method whenever you enter new activity and load the fragment here. 
public void showFragment(Fragment object, String title) 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, object);
    transaction.commit();

那些需要抽屉菜单的活动可以继承上面的类。不只对 FrameLayout 内的所有布局进行充气。

public class MainActivity extends BaseDrawerLayoutActivity 
public static final String TAG = OLMSActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    //the method below will call the BaseDrawerLayoutActivity method and change the fragment for you to see.
    showFragment(new MyProfileFragment(), "My Profile");


@Override
protected void loadNavMenu() 
    NavItems.clear();
    //load your menu items


private void loadNavFavourites()
    //todo

MyProfileFragment 只是一个带有片段的类,这个调用在其 OnCreateView 方法中扩展了一个布局。

【讨论】:

看到我的问题是导航抽屉没有打开,我可以访问基本的 calss 属性,但导航抽屉是唯一的问题【参考方案2】:

让父 Activity 添加所需的 ViewGroup 作为 FrameLayout 的子。为此,请对 DrawerActivity 使用以下方法:

protected void addToContentView(int layoutResID)

    // as part of onCreate() we had already
    // setContentView(R.layout.activity_drawer);

    if (layoutResID >= 0)
    
        Toast.makeText(this, "activity content found", Toast.LENGTH_LONG).show();
        FrameLayout fl = (FrameLayout)findViewById(R.id.activity_frame);
        View.inflate(this, layoutResID, fl);
    
    else
    
        Toast.makeText(this, "activity content missing", Toast.LENGTH_LONG).show();
    

那么,在子ActivityonCreate()方法中:

@Override
protected void onCreate(Bundle savedInstanceState)

    super.onCreate(savedInstanceState);

    // call this instead of setContentView() 
    addToContentView(R.layout.activity_user_dashboard);

    // put here your code as before...

【讨论】:

以上是关于使用继承的活动导航抽屉实现的主要内容,如果未能解决你的问题,请参考以下文章

单击工具栏上的汉堡包图标不会打开导航抽屉

多个活动的导航抽屉,无需选择导航抽屉活动

如何实现一个滑动整个活动内容的 Android 导航抽屉?

导航抽屉主页按钮在打开另一个活动并返回后不打开抽屉

多个活动上的 Android 导航抽屉

如何实现导航抽屉布局?