关键词:
【中文标题】使用继承的活动导航抽屉实现【英文标题】: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();
那么,在子Activity
的onCreate()
方法中:
@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 导航抽屉?
...前致谢。***---------解决方案--------*:**我找到了解决方案。使用这个库很容易使用和 查看详情
多个活动的导航抽屉,无需选择导航抽屉活动
】多个活动的导航抽屉,无需选择导航抽屉活动【英文标题】:NavigationDrawerFormultipleActivitiesWithoutSelectingNavigationDraweractivity【发布时间】:2020-06-0201:14:37【问题描述】:我是安卓新手。我想实现一个由项目列表组成的导航抽屉,... 查看详情
如何实现导航抽屉布局?
...103:46:31【问题描述】:我想实现一个导航抽屉布局,我想使用一个切换栏来打开或关闭导航抽屉。你能把主要活动java和xml文件的代码发给我吗?【问题讨论】:【参考方案1】:我做了一个可以正常工作的新代码:Navigation_Header.x... 查看详情
单击工具栏上的汉堡包图标不会打开导航抽屉
...删除了活动中的所有onOptionsItemSelecteds,并使所有活动都继承自扩展AppComplatActivity和实现所有必要方法的基本活动。在 查看详情
如何在不使用片段的情况下访问所有活动中的导航抽屉?
】如何在不使用片段的情况下访问所有活动中的导航抽屉?【英文标题】:Howtoaccessnavigationdrawerinallactivitieswithoutusingfragments?【发布时间】:2014-03-2907:27:47【问题描述】:我想在所有活动中访问导航抽屉。我不想使用片段。相反... 查看详情
更新基本导航抽屉活动的参数
...上下文:我想在他登录后将userId传递给片段UserFragment。我使用了一个新项目Android类型“导航抽屉活动”。我使用LiveData,所以当用户登录时,我的livedata的观察者会从MainActivity更新idUse 查看详情
将导航抽屉活动扩展到其他活动
...扩展该活动并按照此问题Link中给出的答案在所有活动中使用菜单,但我的测试应用程序不断崩溃,这是我的代码:BaseActivity.javapublicclassBaseActivityex 查看详情
带有活动和子片段的导航抽屉
】带有活动和子片段的导航抽屉【英文标题】:NavigationdrawerwithActivityandchildFragments【发布时间】:2014-04-1412:41:12【问题描述】:我有一个ActivityA、一个ListFragmentP和2FragmentsQ和R。应用程序启动时,会创建A,它会加载P。根据用户点... 查看详情
想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉(代码片段)
我正在尝试最后两天在我的抽屉活动旁边添加片段,以便在整个应用程序中看到导航抽屉。我已尝试过stackoverflow和其他许多方法,但仍然没有成功。然后我必须从第一个片段移动到第二个片段,依此类推,直到需要导航抽屉。... 查看详情
导航抽屉异步任务
...问题,我想知道如何解决它。在我的应用程序中,我正在使用一个活动,进行简单的登录,启动一个asyncTask。在这个任务结束时,用户被重定向到另一个活动,这是应用程序的主活动。后者的任务是管理导航抽屉及其片段。每个... 查看详情
如何使用安全参数将数据从导航抽屉传递到活动?
】如何使用安全参数将数据从导航抽屉传递到活动?【英文标题】:HowtopassdatafromthenavigationdrawertoActivityusingsafe-args?【发布时间】:2019-10-0621:34:34【问题描述】:假设我们有一个这样的项目:classMainActivity:AppCompatActivity()privatelatein... 查看详情
在导航抽屉上调用不是片段活动的活动
】在导航抽屉上调用不是片段活动的活动【英文标题】:CallanActivitythatisnotafragmentactivityonNavigationDrawer【发布时间】:2022-01-1013:07:02【问题描述】:我是AndroidStudio的新手。我有一个导航抽屉,我试图调用一个不是片段活动的活动... 查看详情
单击导航抽屉的项目不会打开片段
...fragments【发布时间】:2020-01-1516:46:05【问题描述】:我想使用AndroidStudio(v.3.5)的默认导航抽屉活动。创建此默认活动(新项目->导航抽屉活动)后,我启动了此模板。如果我单击导航菜单的图标之一(例如“图库”),则NavHost... 查看详情
所有活动中的Android导航抽屉[重复]
】所有活动中的Android导航抽屉[重复]【英文标题】:AndroidNavdrawerinallactivities[duplicate]【发布时间】:2018-05-0901:19:49【问题描述】:如何在所有活动中显示我的导航抽屉在androidstudio中创建一个新项目并有一个导航抽屉活动后,我... 查看详情
如何在所有活动中使用Android Studio默认导航抽屉[重复]
】如何在所有活动中使用AndroidStudio默认导航抽屉[重复]【英文标题】:HowtouseAndroidStudiodefaultNavigationDrawerinallActivities[duplicate]【发布时间】:2016-03-1900:10:21【问题描述】:如何在其他Activity中使用Android默认Navigation?我不想使用返... 查看详情
如何从导航抽屉启动活动?
】如何从导航抽屉启动活动?【英文标题】:HowtolaunchactivityfromNavigationDrawer?【发布时间】:2014-06-3020:25:10【问题描述】:所以我在这里搜索了有关导航抽屉的分配,当我被指向另一个人问题的答案中的教程时。我这样做了。我... 查看详情
导航抽屉和Android中的活动
】导航抽屉和Android中的活动【英文标题】:NavigationDrawerandwithActivityinAndroid【发布时间】:2014-10-2911:46:33【问题描述】:我正在尝试thistutorial.中给出的导航抽屉(幻灯片菜单)上述链接和我的不同之处在于,我尝试调用活动而不... 查看详情
在所有活动问题中显示导航抽屉
】在所有活动问题中显示导航抽屉【英文标题】:DisplayingNavigationdrawerinallactivitiesproblem【发布时间】:2020-04-2011:54:58【问题描述】:我正在制作一个包含不同活动的导航抽屉,我想在所有活动中显示相同的导航抽屉,我采用相同... 查看详情