具有垂直滚动条和左右滑动手势检测的Android片段视图

时间:2016-01-06 05:50:48

标签: java android android-fragments

我正在实现一个简单的笑话应用程序,其功能是向左和向右滑动以查看下一个或上一个笑话。一个笑话可能很长,因此我需要textview上的滚动条。但是,看起来像ontouch监听器和滚动条相互搞乱,滚动后滑动不起作用。有什么想法吗?

<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="#FFF"
                tools:context="com.bontututu.bontu.JokeFragment">

    <ScrollView
        android:id="@+id/textAreaScroller"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="0px"
        android:layout_y="25px"
        android:scrollbars="vertical">

        <TextView
            android:id="@+id/txtJoke"
            style="@style/jokeText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@+id/txtSwipeLeft"
            android:layout_toRightOf="@+id/txtSwipeRight"
            android:gravity="center"
            android:text=""/>
    </ScrollView>

    <TextView
        android:id="@+id/txtSwipeRight"
        style="@style/swipeIndicatorStyle"
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="☛"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/txtSwipeLeft"
        style="@style/swipeIndicatorStyle"
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="☚"
        android:visibility="gone"/>

</RelativeLayout>

片段代码的一部分如下。

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        final View view = inflater.inflate(R.layout.fragment_joke, container, false);
        _textViewJoke = (TextView) view.findViewById(R.id.txtJoke);
        _textViewLeftSwipeIndicator = (TextView) view.findViewById(R.id.txtSwipeLeft);
        _textViewRightSwipeIndicator = (TextView) view.findViewById(R.id.txtSwipeRight);
        _mainScrollView = (ScrollView) view.findViewById(R.id.textAreaScroller);
        getJokes();
        displayCurrentJoke();
        view.setOnTouchListener(new OnSwipeTouchListener(getActivity())
        {
            @Override
            public void onSwipeRight()
            {
                _mainScrollView.requestDisallowInterceptTouchEvent(true);
                if (_jokes != null)
                {
                    _currentJokeIndex--;
                    displayCurrentJoke();
                }
            }

            @Override
            public void onSwipeLeft()
            {
                _mainScrollView.requestDisallowInterceptTouchEvent(true);
                if (_jokes != null)
                {
                    _currentJokeIndex++;
                    displayCurrentJoke();
                }
            }

            @Override
            public void onSwipeTop()
            {
                _mainScrollView.requestDisallowInterceptTouchEvent(false);
            }

            @Override
            public void onSwipeBottom()
            {
                _mainScrollView.requestDisallowInterceptTouchEvent(false);
            }

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                return gestureDetector.onTouchEvent(event);
            }
        });
        return view;
    }
}

0 个答案:

没有答案