当我离开滑动滑动标签时左侧导航抽屉打开

时间:2017-09-25 13:51:01

标签: android android-fragments tabs navigation android-navigation-drawer

我使用材料设计滑动标签(smartTabLayout),在这里:https://github.com/ogaclejapan/SmartTabLayout

并且,材料设计侧导航抽屉(android驻留菜单),这里:https://github.com/SpecialCyCi/AndroidResideMenu

我面临的问题是:当我从任何标签向左滑动时,导航抽屉会打开,但是当我向右滑动时它很好。

Tab on the extreme right

This is what happens when I swipe left. The drawer opens up

以下是代码:

MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    private SectionsPageAdapter sectionsPageAdapter;
    private ViewPager viewPager;
    Toolbar toolbar;
    ResideMenu resideMenu;
    SmartTabLayout smartTabLayout;
    private MainActivity mContext;
    private ResideMenuItem equalizer;
    private ResideMenuItem ringtonemaker;
    private ResideMenuItem identifysong;
    private ResideMenuItem themes;
    private ResideMenuItem rateus;


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

        mContext = MainActivity.this;
        setUpMenu();


        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        sectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        smartTabLayout = (SmartTabLayout) findViewById(R.id.viewpagertab);
        smartTabLayout.setViewPager(viewPager);



    }


    private void setupViewPager(ViewPager viewPager) {

        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Albums(), "Albums");
        adapter.addFragment(new Artists(), "Artists");
        adapter.addFragment(new Songs(), "Songs");
        adapter.addFragment(new Playlists(), "Playlists");
        adapter.addFragment(new Genres(), "Genres");
        adapter.addFragment(new My_Files(), "My Files");
        viewPager.setAdapter(adapter);
        viewPager.setPageTransformer(true, new CubeInTransformer());

    }

        private void setUpMenu() {

        // attach to current activity;
        resideMenu = new ResideMenu(this);
        resideMenu.setBackground(R.drawable.city);
        resideMenu.attachToActivity(this);
        //valid scale factor is between 0.0f and 1.0f. leftmenu'width is 150dip.
        resideMenu.setScaleValue(0.6f);

        // create menu items;
        equalizer = new ResideMenuItem(this, R.drawable.city, "Equalizer");
        ringtonemaker = new ResideMenuItem(this, R.drawable.city, "Ringtone Maker");
        identifysong = new ResideMenuItem(this, R.drawable.city, "Identify Song");
        themes = new ResideMenuItem(this, R.drawable.city, "Theme");
        rateus = new ResideMenuItem(this, R.drawable.city, "Rate us");

        equalizer.setOnClickListener(this);
        ringtonemaker.setOnClickListener(this);
        identifysong.setOnClickListener(this);
        themes.setOnClickListener(this);
        rateus.setOnClickListener(this);

        resideMenu.addMenuItem(equalizer, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(ringtonemaker, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(identifysong, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(themes, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(rateus, ResideMenu.DIRECTION_LEFT);

        // You can disable a direction by setting ->
        resideMenu.setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);

    }



    @Override
    public void onClick(View view) {

    }
}

滑动标签适配器:

class SectionsPageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> FragmentList = new ArrayList<>();
    private final List<String> FragmentTitleList = new ArrayList<>();

    public void addFragment(Fragment fragment, String Title){

        FragmentList.add(fragment);
        FragmentTitleList.add(Title);
    }


    public SectionsPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return FragmentTitleList.get(position);
    }

    @Override
    public Fragment getItem(int position) {
       return FragmentList.get(position);
    }

    @Override
    public int getCount() {
        return FragmentList.size();
    }
}

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/city"

tools:context="com.musicplayer.musicana.MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="30dp"
    android:title="Musicana"
    app:titleTextColor="#ffffff">

</android.support.v7.widget.Toolbar>

<com.ogaclejapan.smarttablayout.SmartTabLayout
    android:id="@+id/viewpagertab"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_below="@+id/toolbar"
    app:stl_indicatorAlwaysInCenter="false"
    app:stl_indicatorWithoutPadding="false"
    app:stl_indicatorInFront="false"
    app:stl_indicatorInterpolation="smart"
    app:stl_indicatorGravity="center"
    app:stl_indicatorColor="#40C4FF"
    app:stl_indicatorThickness="30dp"
    app:stl_indicatorWidth="110dp"
    app:stl_indicatorCornerRadius="10dp"
    app:stl_overlineColor="#4D000000"
    app:stl_overlineThickness="0dp"
    app:stl_underlineColor="#ffffff"
    app:stl_underlineThickness="0dp"
    app:stl_dividerColor="#ffffff"
    app:stl_dividerThickness="0dp"
    app:stl_defaultTabBackground="?attr/selectableItemBackground"
    app:stl_defaultTabTextAllCaps="false"
    app:stl_defaultTabTextColor="#ffffff"
    app:stl_defaultTabTextSize="16sp"
    app:stl_defaultTabTextHorizontalPadding="30dp"
    app:stl_defaultTabTextMinWidth="0dp"
    app:stl_distributeEvenly="false"
    app:stl_clickable="true"
    app:stl_titleOffset="24dp"
    app:stl_drawDecorationAfterTab="false"
    />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/viewpagertab"
    />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我可以看到here方法dispatchTouchEvent没有可能设置拖动区域以避免ViewPager触摸拦截。因此,您可以分叉此repo并添加该功能。或者您可以覆盖活动dispatchTouchEvent

boolean menuDragging = false;  

public boolean isInsideMenuDragArea(MotionEvent ev) {
    return ev.getX() < 200; // THIS IS PIXELS, NOT DP!
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (menuDragged) return resideMenu.dispatchTouchEvent(ev);
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
         if (!isInsideMenuDragArea(ev)) {
              return super.dispatchTouchEvent(ev);
         } else {
              menuDragging = true;
         }    
    } else if (ev.getAction() ==  MotionEvent.ACTION_UP) {
         menuDragging = false;
    }
    return resideMenu.dispatchTouchEvent(ev);

}

我认为这足以理解我的意思

修改

如果要在向左滑动第一个选项卡时打开侧面导航栏

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    public void onPageSelected(int position) {
        if (position == 0) resideMenu.removeIgnoredView(viewPager);
        else resideMenu.addIgnoredView(viewPager);
    }
});

<强> EDIT2: 将其添加到您的活动

boolean resideMenuEnabled = true;

public void setResideMenuEnabled(boolean enabled) {
     if (resideMenuEnabled != enabled) {
          if (enabled) resideMenu.removeIgnoredView(viewPager);
          else resideMenu.addIgnoredView(viewPager);
          resideMenuEnabled = enabled;
     }
}

并像这样使用

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    public void onPageSelected(int position) {
        if (position == 0) setResideMenuEnabled(true);
        else setResideMenuEnabled(false);
    }
});