2017-02-20 59 views
1

我有一個片段,當您單擊它時會顯示一些文本,並且我希望我的活動能夠拖動整個片段視圖。我爲包含該片段的FrameLayout放置了一個onTouchListener,併爲該片段放置了一個onClickListener。但我的onTouchListener永遠不會被觸發。我如何從onClick傳遞onTouchEvent?Android:活動onTouch由於片段onClick而未調用

下面是我的代碼的相關部分

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; 
} 

回答

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; 
}