2011-09-28 49 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); 
    } 
});