滑动时是否有办法阻止片段1进入片段2?

时间:2019-05-09 11:30:40

标签: android android-fragments android-framelayout

当我向右/向左滑动时,有没有办法停止片段1进入片段2 我最近注意到,如果我向左滑动,片段会从1变为2。

我在网上搜索,但找不到答案

编辑:

我的适配器代码

    public ViewPager viewPager;
    private ViewPagerAdapter adapter



    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.AddFragment(new Fragment_Sign_In_To_Firebase(), "Firebase Sign In");
    adapter.AddFragment(new Fragment_Create_Account(), "Create Account");
    adapter.AddFragment(new Fragment_Verify_Email(), "Verify Email");


    viewPager = (ViewPager) findViewById(R.id.viewpager_id_chat);
    viewPager.setAdapter(adapter);
    viewPager.arrowScroll(View.FOCUS_RIGHT);

ViewPaggerAdapter类:

public class ViewPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> lstFragment = new ArrayList<>();
private final List<String> lstTitles = new ArrayList<>();

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

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

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

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

public void AddFragment(Fragment fragment, String title) {
    lstFragment.add(fragment);
    lstTitles.add(title);
}
}

1 个答案:

答案 0 :(得分:1)

正如我所见,您的问题是对您要搜索的内容没有正确的查询,我认为您是在谈论ViewPager

 public class CustomViewPager extends ViewPager {

    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    } }

在您的XML中使用它

<mypackage.CustomViewPager 
    android:id="@+id/myViewPager" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" />

或简单添加此行

myViewPager.beginFakeDrag();