Android:由于片段onClick而未调用onTouch活动

时间:2017-02-20 22:35:16

标签: android android-fragments

我有一个片段,当你点击它时显示一些文字,我希望我的活动能够拖动整个片段视图。我为包含片段的FrameLayout和片段的onClickListener放了一个onTouchListener。但我的onTouchListener永远不会被触发。如何从onClick传递onTouchEvent?

以下是我的代码的相关部分

activity_question.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/linearlayout_question">

    <include
        layout="@layout/toolbar_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/framelayout_question_fragmentcontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

QuestionActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question);

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout_question);
    linearLayout.setOnTouchListener(new View.OnTouchListener() {

        private float dX, dY;
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:

                    dX = view.getX() - event.getRawX();
                    dY = view.getY() - event.getRawY();
                    break;

                case MotionEvent.ACTION_MOVE:

                    view.animate()
                            .x(event.getRawX() + dX)
                            .y(event.getRawY() + dY)
                            .setDuration(0)
                            .start();
                    break;
                default:
                    return false;
            }
            return true;
        }
    });

    if (findViewById(R.id.framelayout_question_fragmentcontainer) != null) {
        if (savedInstanceState != null) {
            return;
        }

        // Create a new Fragment to be placed in the activity layout
        if (DataService.hasNext()) {
            Trivia trivia = DataService.getNext();
            triviaList.add(trivia);
            QuestionFragment questionFragment = QuestionFragment.newInstance(trivia.getQuestion(), trivia.getAnswer());
            currentPosition = 0;

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.framelayout_question_fragmentcontainer, questionFragment).commit();
        }
    }

QuestionFragment

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_question, container, false);
    TextView textViewQuestion = (TextView) view.findViewById(R.id.textview_question_question);
    textViewQuestion.setText(mQuestion);

    final TextView textViewAnswer = (TextView) view.findViewById(R.id.textview_question_answer);
    textViewAnswer.setText(mAnswer);

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textViewAnswer.setVisibility(View.VISIBLE);
        }
    });
    return view;
}

1 个答案:

答案 0 :(得分:0)

您不需要onClick - 您只能使用onTouch,它应该可以消除您的问题。在onTouch内部,您还可以使用GestureDetector检测点击,或者只是检测MotionEvent.ACTION_DOWNMotionEvent.ACTION_UP,因为它适合您。

如果您想使用GestureDetecor,请执行:

gestureDetector = new GestureDetector(this, new SingleTapConfirm());

并在onTouch内部:

if (gestureDetector.onTouchEvent(arg1)) {
   // tap detected
   return true;
}