【求助】SlidingMenu 左右滑动菜单与viewpage冲突问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了【求助】SlidingMenu 左右滑动菜单与viewpage冲突问题相关的知识,希望对你有一定的参考价值。

使用SlidingMenu 开源的包实现了左侧滑动菜单的功能,结合ViewPage完成了效果。viewPage 滑动到第一页的时候再滑动就会出现左侧的菜单。现在需要ViewPage滑动到最后一页从右侧出现另一个SlidingMenu菜单。mSlidingMenuRight.setSecondaryMenu(R.layout.fragment_r_menu); getSupportFragmentManager().beginTransaction() .replace(R.id.menu_r, new RightMenuFragment()).commit();使用这个方法添加了 右侧的菜单,但是现在Viewpage 里面的没法滑了 一旦左滑就出现了左侧的菜单。右滑就出现了右侧的菜单。冲突了。 求助解决方法》》》》》》》》》

参考技术A 文档我下载看了。实现的很不错,就是中间的title 无法固定。他自己定义的Menu还是局限性比较大。 但是这个的确是个很好的解决方法。谢谢了 查看原帖>>

麻烦采纳,谢谢!本回答被提问者采纳

利用开源SlidingMenu框架实现左右侧滑菜单的功能

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends SlidingFragmentActivity
{

	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private List<Fragment> mFragments = new ArrayList<Fragment>();

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		// 初始化SlideMenu
		initRightMenu();
		// 初始化ViewPager
		initViewPager();

	}

	private void initViewPager()
	{
		mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
		MainTab01 tab01 = new MainTab01();
		MainTab02 tab02 = new MainTab02();
		MainTab03 tab03 = new MainTab03();
		mFragments.add(tab01);
		mFragments.add(tab02);
		mFragments.add(tab03);
		/**
		 * 初始化Adapter
		 */
		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
		{
			@Override
			public int getCount()
			{
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int arg0)
			{
				return mFragments.get(arg0);
			}
		};
		mViewPager.setAdapter(mAdapter);
	}

	private void initRightMenu()
	{
		
		Fragment leftMenuFragment = new MenuLeftFragment();
		setBehindContentView(R.layout.left_menu_frame);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.id_left_menu_frame, leftMenuFragment).commit();
		SlidingMenu menu = getSlidingMenu();
		menu.setMode(SlidingMenu.LEFT_RIGHT);
		// 设置触摸屏幕的模式
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		menu.setShadowWidthRes(R.dimen.shadow_width);
		menu.setShadowDrawable(R.drawable.shadow);
		// 设置滑动菜单视图的宽度
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
//		menu.setBehindWidth()
		// 设置渐入渐出效果的值
		menu.setFadeDegree(0.35f);
		// menu.setBehindScrollScale(1.0f);
		menu.setSecondaryShadowDrawable(R.drawable.shadow);
		//设置右边(二级)侧滑菜单
		menu.setSecondaryMenu(R.layout.right_menu_frame);
		Fragment rightMenuFragment = new MenuRightFragment();
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.id_right_menu_frame, rightMenuFragment).commit();
	}

	public void showLeftMenu(View view)
	{
		getSlidingMenu().showMenu();
	}

	public void showRightMenu(View view)
	{
		getSlidingMenu().showSecondaryMenu();
	}
}

 

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainTab01 extends Fragment
{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		return  inflater.inflate(R.layout.main_tab_01, container, false);
	
	}

}

 

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainTab02 extends Fragment
{

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View messageLayout = inflater.inflate(R.layout.main_tab_02, container, false);
		return messageLayout;
	}

}

 

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainTab03 extends Fragment
{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View newsLayout = inflater.inflate(R.layout.main_tab_03, container, false);
		return newsLayout;
	}

}

 

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.Arrays;
import java.util.List;

public class MenuLeftFragment extends Fragment
{
	private View mView;
	private ListView mCategories;
	private List<String> mDatas = Arrays.asList("聊天", "发现", "通讯录", "朋友圈", "订阅号");
	private ListAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		if (mView == null)
		{
			initView(inflater, container);
		}
		return mView;
	}

	private void initView(LayoutInflater inflater, ViewGroup container)
	{
		mView = inflater.inflate(R.layout.left_menu, container, false);
		mCategories = (ListView) mView
				.findViewById(R.id.id_listview_categories);
		mAdapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, mDatas);
		mCategories.setAdapter(mAdapter);
	}
}

 

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MenuRightFragment extends Fragment
{
	private View mView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		if(mView == null)
		{
			mView = inflater.inflate(R.layout.right_menu, container, false);
		}
		return mView ;
	}
}

 activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:background="@drawable/title_bar">

        <ImageButton
            android:id="@+id/id_iv_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="3dp"
            android:background="@drawable/showright_selector"
            android:onClick="showLeftMenu"/>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="微信"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold"/>

        <ImageButton
            android:id="@+id/id_iv_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="3dp"
            android:background="@drawable/showleft_selector"
            android:onClick="showRightMenu"/>
    </RelativeLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/id_viewpager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>

</LinearLayout>

 left_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:background="@drawable/title_bar" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="功能"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <ListView
        android:id="@+id/id_listview_categories"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAlignment="center" />

</LinearLayout>

 left_menu_frame.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/id_left_menu_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

 main_tab_01.xml   main_tab_02.xml   main_tab_03.xml 实现如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ly_main_weixin" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fcfcfc" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="this is first tab !" android:textColor="#000000" android:textSize="30sp" /> </LinearLayout>

 right_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eee"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:background="@drawable/title_bar" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="设置"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_first_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="个人信息"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="二维码名片"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="腾讯微博"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_last_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="添加朋友"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_first_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="我的帐号"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/friends_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="聊天背景"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_first_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="通用"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="插件"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="黑名单"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="隐私"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="系统通知"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_last_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="流量同级"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_first_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="帮助与反馈"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_last_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="关于微信"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/friends_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="清空聊天记录"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:layout_marginLeft="20sp"
                    android:layout_marginRight="20sp"
                    android:background="@drawable/exit_btn"
                    android:gravity="center"
                    android:text="退出登录"
                    android:textColor="#fff"
                    android:textSize="16sp" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

 right_menu_frame.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/id_right_menu_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

 shadow,xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:endColor="#ff444444"
        android:startColor="#00000000" />

</shape>

 

效果图:

以上是关于【求助】SlidingMenu 左右滑动菜单与viewpage冲突问题的主要内容,如果未能解决你的问题,请参考以下文章

Android 在 jfeinstein SlidingMenu 中使用 LEFT_RIGHT

android 怎样用HorizontalScrollView左右弹性的菜单

利用开源SlidingMenu框架实现左右侧滑菜单的功能

SlidingMenu 侧滑菜单的用法

Android自定义ViewGroup打造各种风格的SlidingMenu

Android自己定义ViewGroup打造各种风格的SlidingMenu