禁用片段中的水平滚动

时间:2017-05-31 10:44:11

标签: android android-fragments

我已经实现了一个ViewPager来在片段之间滑动。问题是我想要禁用分页,直到满足某个条件。我在下面给你看的代码工作得很好,但它不允许片段布局中的垂直滚动,我只想禁用水平滚动,我的意思是片段之间的转换。

我有一个包含自定义ViewPager的布局,如下所示:

layout_pager

<LinearLayout 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:orientation="vertical" tools:context=".MyActivity">

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

和另一个用于定义实现ScrollView的Fragment内容的布局:

layout_fragment

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MyActivity">

    <LinearLayout>
        <!-- Some TextViews, Images, Buttons, etc -->
    </LinearLayout>
</ScrollView>

活动只是分配适配器

MyActivity

public class MyActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_pager);
        CustomViewPager viewPager = (CusotmViewPager) findViewById(R.id.viewpager);

        CustomAdapter adapter = new CustomAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
    }
}

CustomViewPager有一个标志,指示是否允许分页

CustomViewPager

public class CustomViewPager extends ViewPager {

    private boolean enabled;

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return enabled ? super.onInterceptTouchEvent(ev) : false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return enabled ? super.onTouchEvent(event) : false;
    }

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

最后,片段拦截触摸事件以检查条件是否满足以启用pagintaion。

MyFragment

public class MyFragment extends Fragment {

    public MyFragment() {}

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.layout_fragment, container, false);
        final MyActivity myActivity = (MyActivity) getActivity();

        rootView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_DOWN) {
                    View customViewPagerRoot = myActivity.findViewById(R.id.viewpager);
                    CustomViewPager viewPager = (CustomViewPager) customViewPagerRoot.findViewById(R.id.viewpager);
                    if (condition to allow the pagination) {
                        viewPager.setPagingEnabled(true);
                    } else {
                        viewPager.setPagingEnabled(false);
                    }
                }

                return true;
            }
        });

        return rootView;
    }
}

我希望我已经正确解释了这个问题,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

禁用片段

之间的滑动
viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

希望此信息可以帮助您