Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 使用NavigationDrawer的多个活动(不是片段).如何显示当前所选的活动?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在确定我们将使用导航抽屉或汉堡菜单之前,我有一个应用程序有很多活动.我不想使用片段重做整个应用程序,所以我决定采用这个答案中使用的方法
Same Navigation Drawer in different Activities

编辑:现在,这一个https://stackoverflow.com/a/23477100/1371585

我创建了一个名为NavDrawerBaseActivity的基本活动.这是代码

public class NavDrawerBaseActivity extends MyBaseActivity {

    public DrawerLayout mNavDrawerLayout;
    public ListView mDrawerList;
    public String[] mMenuItems;

    @Override
    protected void onCreate(Bundle savedInstanceStatE) {
        setContentView(R.layout.navdrawer_activity);

        mNavDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mMenuItems = getresources().getStringArray(R.array.optionsmenu_array);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.drawer_list_item,mMenuItems);
        mDrawerList.setAdapter(adapter);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener(this));

        super.onCreate(savedInstanceStatE);
    }

    private class DrawerItemClickListener implements
            ListView.onItemClickListener {

        private DrawerItemClickListener(Context context) {
            mContext = context;
        }

        @Override
        public void onItemClick(AdapterView<?> parent,View view,int position,long id) {

            String textClicked = ((TextView) view).getText().toString();
            view.setSELEcted(true);

            if (textClicked.equalsIgnoreCase(mContext
                    .getString(R.String.optionsmenu_library))) {


                Intent libraryIntent = new Intent(mContext,LibraryActivity.class);
                libraryIntent.putExtra("navdrawerposition",position);
                startActivity(libraryIntent);
                mNavDrawerLayout.closeDrawers();
                mDrawerList.setItemchecked(position,truE); //Not working


            } else if (textClicked.equalsIgnoreCase(mContext
                    .getString(R.String.optionsmenu_setTings))) {
                // TODO: open activity and close the drawer
            } else if (textClicked.equalsIgnoreCase(mContext
                    .getString(R.String.optionsmenu_logout))) {
                // TODO: open activity and close the drawer
            } 
        }

        private Context mContext;
    }
}

这是布局文件navdrawer_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://scheR_346_11845@as.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    >

    <FrameLayout
        android:id="@+id/activity_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:BACkground="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" 
        />


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

每个活动都扩展了NavDrawerBaseActivity,并且不使用setContentView,如下所示:

public class LibraryActivity extends NavDrawerBaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceStatE) {
        super.onCreate(savedInstanceStatE);

        // Not setTing content view here,since its already set in
        // NavDrawerBaseActivity
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.activity_framE);
        // InflaTing the Camera activity layout
        LayoutInflater layoutInflater = (LayoutInflater) getSystemservice(Context.LAYOUT_INFLATER_serviCE);
        View activityView = layoutInflater
                .inflate(R.layout.library_page,null,falsE);
        // Adding the custom layout of this activity to frame layout set in
        // NavDrawerBaseActivity.
        frameLayout.addView(activityView);


//      Only this part of the code is doing what I want.

//      int drawerSELEctedPosition = geTintent().getIntextra(mNavDrawerPosExtraName,-1);
//      if(drawerSELEctedPosition > -1){
//          mDrawerList.setItemchecked(drawerSELEctedPosition,truE);
//      }
    }

}

我的问题:如何正确突出NavDrawer视图中的当前活动?@H_530_5@mDrawerList.setItemchecked(position,truE);在启动Intent之前或启动之后它无效.

奇怪的部分是:如果我当前在Activity1中,打开NavDrawer并选择Activity2.我登陆Activity2,打开NavDrawer并看到未选中“Activity2”.我单击BACk按钮,登陆Activity1,打开NavDrawer并看到“Activity2”被选中.

这意味着setItemchecked可以工作,但不会在启动的新活动中工作.

目前,我将该职位作为Intent额外传递,并专门设置检查位置,如LibraryActivity中的注释部分.
这有效但似乎是一种解决方法.请告诉我在NavDrawerBaseActivity类中是否有正确/更好的方法,而不是在扩展它的每个Activity中.

解决方法

不要在每个活动中放置导航抽屉,这会破坏扩展NavDrawerBaseActivity类的目的.因为所有其他活动都扩展了这个基类,所以它们应该自动继承它的所有功能.因此,只将抽屉放在NavDrawerBaseActivity中.然后,在您的xml抽屉中,您可以指定每个按钮操作,如:

<Button
        style="@style/drawerBtn"
        android:id="@+id/activity1Btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick">

或者只需在抽屉上设置onClick监听器,例如:

@H_991_12@myDrawer.setOnClickListener().........etc

然后在你的onClick处理程序(在NavDrawerBaseActivity类中)你可以简单地检查按下了哪个按钮并打开相关的活动,例如:

//remember,if you used the onClickListener you have to use @Override at the top of this function
public void onClick(View item) {

    //lets see which button on the drawer was pressed....item is the item that triggered the click
    switch (item.getId()) {
        case R.id.activity1Btn:
            Intent intent1 = new Intent(this,Activity1.class);
            startActivity(intent1);
            break;
        case R.id.activity2Btn:
            Intent intent2 = new Intent(this,Activity2.class);
            startActivity(intent2);
            break;
        case R.id.activity3Btn:
            Intent intent3 = new Intent(this,Activity3.class);
            startActivity(intent3);
            break;
    }
}

请记住,Activity1,Activity2,Activity3必须扩展NavDrawerBaseActivity,然后必须扩展Activity或扩展另一个类,而后者又扩展Activity ……依此类推.然后,您还可以在此开关中设置mDrawerList.setItemchecked(position,truE)之类的内容.所以简而言之,让所有抽屉的东西只在这个类中发生,只需通过扩展它来“实现”这个类,记住这个类包含所有“子”类/活动中共同的所有功能,它们都继承了这个行为

编辑:

如果要突出显示抽屉中的项目,则必须在项目上使用setSELEcted(true).如果要在drawables文件夹中创建选择样式,可以定义自定义选择状态.然后,将此样式设置为列表项示例的背景:

<!-- drawable/myStyles.xml-->
<SELEctor xmlns:android="http://scheR_346_11845@as.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="true" android:drawable="@color/blue"/> <!--item SELEcted state--> 
    <item android:state_enabled="true" android:state_focused="true" android:drawable="@color/blue"/> <!--item SELEcted state--> 
    <item android:state_enabled="true" android:state_SELEcted="true" android:drawable="@color/blue"/> <!--item SELEcted state--> 
    <item android:state_focused="false" android:state_pressed="false" android:drawable="@color/gray"/> <!--item NOT SELEcted state--> 
</SELEctor>

你的抽屉项目:

<LinearLayout
    android:id="@+id/my_drawer_item"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:BACkground="@drawable/myStyles">

然后在switch / if-else语句中将所选项设置为MynewselectedView.setSELEcted(true).您可能必须手动取消选择旧的,如myOldSELEctedItem.setSELEcted(false)

然后单击/选择侦听器,在其中切换/ if-else语句:

@Override
public void onItemClick(AdapterView<?> parent,long id) {
     view.setSELEcted(true)  

     //....the rest of your code here
}

最后,我建议您首先尝试这个,因为如果您使用普通的listView,这将是最简单的路线:

<ListView android:id="@+id/my_list"
    android:choiceMode="singleChoice" 
    android:listSELEctor="@android:color/blue" />

EDIT2:

现在保留所选项目状态……所以似乎抽屉上的每个动作都重新实现了不理想的基类.我们需要以某种方式保留实例状态,因此它几乎就像一个单例.我已经尝试覆盖onSaveInstanceState并使用singleInstance启动器状态,但它们不起作用.因此,在过渡期间,我提出了将当前选择保存在内存中作为静态变量的解决方案:

private static int mCurrentSELEctionIndex = 0; //this is defined at the top of your class with your default SELEcted screen ie the first item in the list.

//then in setContentView after assigning the drawer layout you set the currentSELEction
@Override
public void setContentView(final int layoutResID) {
    //...... first assign the layouts ie mDrawerList = findViewByLayout(R.id.myDrawerList) etc

    mDrawerList.setSELEction(mCurrentSELEctionIndeX); 
      // OR:
    mDrawerList.setItemchecked(mCurrentSELEctionIndex,truE);
}

//then in onClick()
public void onItemClick(AdapterView<?> parent,long id) {
    mCurrentSELEction = position;
    item.setSELEcted(true);
}

大佬总结

以上是大佬教程为你收集整理的android – 使用NavigationDrawer的多个活动(不是片段).如何显示当前所选的活动?全部内容,希望文章能够帮你解决android – 使用NavigationDrawer的多个活动(不是片段).如何显示当前所选的活动?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。