2011-09-28 17 views
1

我創建了一個工作正常的日曆,使用了具有OnClickListener的GridView。 現在我將兩個GridViews包裝在ViewFlipper中。 ViewFlipper有一個OnTouchListener,它也能正常工作,我可以在拖動時使用ontouch來更改視圖。問題是,我必須拖動活動中的EMTPY空間才能使用ViewFlipper。當我拖動GridView時,根本沒有任何事情發生。但是我可以點擊GridView的OnClickListener。在Gridview中使用Viewflipper

XML:

<ViewFlipper android:id="@+id/details" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
     <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal"> 
    <GridView 
     android:id="@+id/weeks" 
     android:numColumns="1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="8"> 
    </GridView> 
    <GridView 
     android:id="@+id/calendar" 
     android:numColumns="7" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 
    </GridView> 
</LinearLayout> 

的Android代碼:

@Override 
public boolean onTouch(View arg0, MotionEvent arg1) { 
    // Get the action that was done on this touch event 
    switch (arg1.getAction()) 
    { 
     case MotionEvent.ACTION_DOWN: 
     { 
      // store the X value when the user's finger was pressed down 
      downXValue = arg1.getX(); 
      break; 
     } 

     case MotionEvent.ACTION_UP: 
     { 
      // Get the X value when the user released his/her finger 
      currentX = arg1.getX();  


      // going backwards: pushing stuff to the right 
      if (currentX - downXValue < -(arg0.getWidth()/3)) 
      { 
       mdh.nextMonth(); 
       calendar.add(Calendar.MONTH, 1); 
       currentMonth.setText(new SimpleDateFormat("MMMM yyyy").format(calendar.getTime())); 
       cAdapter.notifyDataSetChanged(); 
       updateWeeks(); 
       // Set the animation 
       vf.setInAnimation(arg0.getContext(), R.anim.push_left_in); 
        vf.setOutAnimation(arg0.getContext(), R.anim.push_left_out); 
        // Flip! 
        vf.showPrevious(); 
      } 

      // going forwards: pushing stuff to the left 
      if (currentX - downXValue > arg0.getWidth()/3) 
      { 
       mdh.previousMonth(); 
       calendar.add(Calendar.MONTH, -1); 
       currentMonth.setText(new SimpleDateFormat("MMMM yyyy").format(calendar.getTime())); 
       cAdapter.notifyDataSetChanged(); 
       updateWeeks(); 
       // Set the animation 
       vf.setInAnimation(arg0.getContext(), R.anim.push_right_in); 
       vf.setOutAnimation(arg0.getContext(), R.anim.push_right_out); 
        // Flip! 
       vf.showNext(); 
      } 
      break; 
     } 
+0

我認爲這種行爲是正常的,因爲您的監聽器已在viewflipper上註冊。由於它包含網格視圖,因此它大部分不是「可見的」,因此不可觸摸。您可能希望在網格視圖上註冊兩個偵聽器,並將不同的手勢與它們關聯以觸發翻轉或其他動作。 – Sephy

回答

0

我有同樣的問題與ViewFlipper和ScrollViews。 嘗試添加onTouchListener到您的GridView以及它應該工作。

+0

好的,我確實在我的gridview中添加了ontouchlistener,現在它工作正常!下一個問題是:我怎樣才能用手指移動屏幕,以便在釋放手指之前可以看到下一個屏幕? – Carnal

+0

看看這個blogpost:http://android-developers.blogspot.com/2011/08/horizo​​ntal-view-swiping-with-viewpager.html –

2
gridview.setOnTouchListener(new OnTouchListener(){ 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     return detector.onTouchEvent(event); 
    } 
});