开发者

Using a Viewflipper with Gridview

I've created a calendar which works fine, using a GridView which has an OnClickListener. Now I wrapped the two GridViews in a ViewFlipper. The ViewFlipper has an OnTouchListener which also works fine, I can change the view by using ontouch when dragging. The problem is though that I have to drag on the EMTPY space in the Activity in order to use the ViewFlipper. When I drag on the GridView, nothing happends at all. But I can click on the GridView for OnClickListener.

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 code:

@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 s开发者_JAVA技巧tuff 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;
        }


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


I had the same issue with a ViewFlipper and ScrollViews. Try adding the onTouchListener to your GridView aswell and it should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜