簡體   English   中英

Android:由於片段onClick而未調用onTouch活動

[英]Android: Activity onTouch not called because of Fragment onClick

我有一個片段,當你點擊它時顯示一些文字,我希望我的活動能夠拖動整個片段視圖。 我為包含片段的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;
}

您不需要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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM