当前位置: 首页 > 知识库问答 >
问题:

如何实现Android应用程序的滑动菜单?(闭门)

惠泳
2023-03-14

抱歉,我知道它被问了很多次,但我没有找到一个好的幻灯片菜单教程

我尝试了一个(简单的)ListView:
1.尝试像谷歌
新尝试(1)
新尝试(2)
谷歌风格

-

活动主

private static Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
}

@Override
public void onStart(){
    super.onStart();

    final String[] values = getResources().getStringArray(R.array.nav_drawer_items);

    ((ListView) findViewById(R.id.left_drawer)).setAdapter(new ArrayAdapter<String>(this, R.layout.menu_list_item, values));

    NavigationDrawerSetup nds=new NavigationDrawerSetup((ListView) findViewById(R.id.left_drawer), (DrawerLayout) findViewById(R.id.drawer_layout), values, getActionBar(), context);

    nds.confiugreDrawer();
}

导航抽屉安装程序

public class NavigationDrawerSetup extends Activity{

protected ListView drawerView;
protected String[] drawerList;
protected DrawerLayout drawerLayout;
protected ActionBar actionBar;
protected Activity currentActivity;

protected ActionBarDrawerToggle mDrawerToggle;

public NavigationDrawerSetup(ListView mDrawerView, DrawerLayout mDrawerLayout, String[] mDrawerList, ActionBar actionBar, Context currentActivity) {
    drawerView=mDrawerView;
    drawerLayout=mDrawerLayout;
    drawerList=mDrawerList;
    this.actionBar=actionBar;
    this.currentActivity=(Activity) currentActivity;
}

public void confiugreDrawer() {
    //Android doesn't accept the ListView.setAdapter here
    drawerView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
            case 0:
                Intent menuToMain = new Intent(currentActivity, ActivityMain.class);
                menuToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(menuToMain);
                break;
            case 1:
                Intent menuToInfo = new Intent(currentActivity, ActivityInfo.class);
                menuToInfo.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(menuToInfo);
                break;
            case 2:
                Intent menuToTest = new Intent(currentActivity, ActivitySwitchTest.class);
                menuToTest.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(menuToTest);
                break;
            default:
                new RalaAlertToast(currentActivity, "default");
                break;
            }
        }
    });

    mDrawerToggle = new ActionBarDrawerToggle(currentActivity, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            actionBar.setTitle(R.string.app_name);
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            actionBar.setTitle(R.string.app_name);
        }
    };

    drawerLayout.setDrawerListener(mDrawerToggle);

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionAPI14();
}

@TargetApi(14)
private void actionAPI14() {
    actionBar.setHomeButtonEnabled(true);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

activity_main.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" >

<!-- The main content view -->

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/popuptestview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="@string/view" />

    <EditText
        android:id="@+id/et_subnetTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/popuptestview"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:gravity="right"
        android:hint="@string/app_name"
        android:inputType="textEmailAddress" >

        <requestFocus />
    </EditText>
</RelativeLayout>

<!-- The navigation drawer -->

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:fadingEdge="vertical" />

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

菜单列表项。xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/listitem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="@dimen/activity_vertical_margin"
android:textSize="@dimen/menu" />

日志

03-30 17:33:50.645: E/AndroidRuntime(3840): FATAL EXCEPTION: main
03-30 17:33:50.645: E/AndroidRuntime(3840): java.lang.NullPointerException
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.app.Activity.startActivityForResult(Activity.java:3401)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.app.Activity.startActivityForResult(Activity.java:3362)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.app.Activity.startActivity(Activity.java:3598)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.app.Activity.startActivity(Activity.java:3566)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at rala.testing.NavigationDrawerSetup$1.onItemClick(NavigationDrawerSetup.java:59)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.widget.AbsListView.performItemClick(AbsListView.java:1102)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2790)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.widget.AbsListView$1.run(AbsListView.java:3465)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.os.Handler.handleCallback(Handler.java:730)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.os.Looper.loop(Looper.java:137)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.app.ActivityThread.main(ActivityThread.java:5303)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at java.lang.reflect.Method.invokeNative(Native Method)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at java.lang.reflect.Method.invoke(Method.java:525)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at dalvik.system.NativeStart.main(Native Method)

共有1个答案

水麒
2023-03-14

像这样试试,

public class MainActivity extends Activity {

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

        final SlidingMenu menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.5f);
        menu.attachToActivity(MainActivity.this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.activity_menu);
    }

活动菜单。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"
    android:background="#00FFFF"
    tools:context=".MainActivity" >

</RelativeLayout>

这是你的活动手册。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"
    android:clickable="true"
    tools:context=".MainActivity" >
</RelativeLayout>

完成此操作后,只需向左滑动屏幕,您将获得另一个活动。

 类似资料:
  • 本文向大家介绍Android利用滑动菜单框架实现滑动菜单效果,包括了Android利用滑动菜单框架实现滑动菜单效果的使用技巧和注意事项,需要的朋友参考一下 之前我向大家介绍了史上最简单的滑动菜单的实现方式,相信大家都还记得。如果忘记了其中的实现原理或者还没看过的朋友,请先去看一遍之前的文章Android仿人人客户端滑动菜单的侧滑特效实现代码,史上最简单的侧滑实现 ,因为我们今天要实现的滑动菜单框架

  • 我不知道怎么才能停止滑动菜单。我想删除可以滑动的幻灯片菜单。我试着用代码注释。但我无法停止扮演slidemenu。 下面是我的代码。你能帮助我吗?非常感谢!:)

  • 目前,我正在尝试插入一个下拉式滑动菜单(不确定它的名称)。其想法是用户触摸箭头或菜单的任何区域,然后向上拖动以打开菜单。但我不知道如何或在哪里可以找到这方面的教程。

  • 本文向大家介绍Android实现侧滑菜单DrawerLayout,包括了Android实现侧滑菜单DrawerLayout的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Android实现侧滑菜单的具体代码,供大家参考,具体内容如下 点击左侧滑动 效果如下 代码实现过程: 1.导入框架build.gradle中 2.xml文件 主要的界面放在DrawerLayout 中,需要强调的是

  • 我正在我的应用程序中使用带有ActionBarSherlock的滑动菜单。动作条和滑动菜单工作良好,但我希望滑动菜单只滑动内容,而不是动作条像最新版本的youtube应用程序。 这在SlidingMenu中是可能的吗?如果是,请说明如何 提前致谢 下面是我的actionbar和slidingmenu代码

  • 本文向大家介绍微信小程序左滑动显示菜单功能的实现,包括了微信小程序左滑动显示菜单功能的实现的使用技巧和注意事项,需要的朋友参考一下 效果图如下所示: view css js 源码下载:https://download.csdn.net/download/qq_30641447/10475865