2012-10-19 149 views
4

我有一個列表視圖。點擊某個項目後,該項目的詳細視圖將打開。這種佈局有許多小部件,如文本視圖,ImageView Buttons等。現在我想滑動這些項目的詳細視圖來顯示列表中下一項的詳細視圖。同樣以前的項目爲從左到右。android:向左或向右滑動滑動視圖

我不能實現視圖滑動我已經完成了如何獲得列表中的前/後項目。但實際的滑動是問題

我試圖gesturedetector像Android: Swipe left to right and right to left

和其他一些例子。但是當我嘗試滑動時,沒有任何效果。我沒有看到任何視覺滑動。

如何解決這個問題?

嘗試這種代碼,但仍然沒有滑動發生

public class ProductActivity extends Activity implements OnGestureListener { 


    @ 
    Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.product_details); 
     setContent(); 
    } 

    private void setContent() { 
     Bundle extras = getIntent().getExtras(); 
     TextView title = (TextView) findViewById(R.id.title); 
     title.setText(extras.getString("Title")); 
     TextView desc = (TextView) findViewById(R.id.desc); 
     desc.setText(extras.getString("Desc")); 
     Button buy = (Button) findViewById(R.id.buy); 
     String priceTag = ("$" + String.format("%.2g", extras.getDouble("Price")) + " Buy Now >>"); 
     buy.setText(priceTag); 
     ImageView image = (ImageView) findViewById(R.id.productimage); 
     Utils.imageLoader.DisplayImage(extras.getString("Image"), image); 

    } 




    private static final int SWIPE_MIN_DISTANCE = 6; // 120; 
    private static final int SWIPE_MAX_OFF_PATH = 125; // 250; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 100; // 200; 
    private GestureDetector gestureScanner; 

    @ 
    Override 
    public boolean onTouchEvent(MotionEvent me) { 
     return gestureScanner.onTouchEvent(me); 
    } 

    // @Override 
    public boolean onDown(MotionEvent e) { 
     // viewA.setText("-" + "DOWN" + "-"); 
     return true; 
    } 

    // @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 
     try { 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
       return false; 
      // right to left swipe 
      if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(getApplicationContext(), "Left Swipe", 
        Toast.LENGTH_SHORT).show(); 

      } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(getApplicationContext(), "Right Swipe", 
        Toast.LENGTH_SHORT).show(); 

      } else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(getApplicationContext(), "Swipe up", 
        Toast.LENGTH_SHORT).show(); 

      } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(getApplicationContext(), "Swipe down", 
        Toast.LENGTH_SHORT).show(); 

      } 
     } catch (Exception e) { 
      // nothing 
     } 

     return true; 
    } 

    @ 
    Override 
    public void onLongPress(MotionEvent e) { 
     Toast mToast = Toast.makeText(getApplicationContext(), "Long Press", 
      Toast.LENGTH_SHORT); 
     mToast.show(); 
    } 

    // @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
     float distanceY) { 
     // viewA.setText("-" + "SCROLL" + "-"); 
     return true; 
    } 

    // @Override 
    public void onShowPress(MotionEvent e) { 
     // viewA.setText("-" + "SHOW PRESS" + "-"); 
    } // @Override 

    public boolean onSingleTapUp(MotionEvent e) { 
     Toast mToast = Toast.makeText(getApplicationContext(), "Single Tap", 
      Toast.LENGTH_SHORT); 
     mToast.show(); 
     return true; 
    } 

} 
+0

我終於跟着這個鏈接:http://misha.beshkin.lv/android-swipe-gesture-implementation/。這正是我所期待的。感謝所有幫助 – png

回答

2

我以爲新的答案是一個好主意,因爲舊的答案是如此不同。

我將從一點背景開始: 這裏的想法是使用水平滾動視圖以漂亮的方式移動到下一組控件。因此,當用戶停止向左或向右滾動滾動視圖時,我們想要更改圖片和文本,然後將滾動移回中間以準備下一次滑動。有人已經回答瞭如何在滾動視圖停止時收聽 我已經包含了一個幾乎可行的示例,說明如何使用它來獲得所需的效果。

我希望這可以幫助你,並隨時問你是否有任何問題。

@Override 
protected void onScrollChanged(int x, int y, int oldX, int oldY) { 
    if (Math.abs(y - oldY) > SlowDownThreshold) { 
     currentlyScrolling = true; 
    } else { 
     currentlyScrolling = false; 
     if (!currentlyTouching) { 
      //scrolling stopped...handle here 
      //Check if it was a left or right swipe by comparing the new and old scroll locations 
      if (y > oldY) { 
       //Scroll moved Right 
       //So we would do something like 
       setContents(Index + 1); 
       //We find out where the middle of the scroll is 
       int middleHScroll = (Hscrollview.getMeasuredWidth()/2) - (Hscrollview.getMeasuredWidth()/2) 
       //We then return the scroll view to the middle 
       HScroll.scrollTo(middleHScroll); 
      } else { 
       //Scroll moved Left 
       //We get set the pictures and text to the previous Index of the contents 
       setContents(Index - 1); 
       //We find out where the middle of the scroll is 
       int middleHScroll = (Hscrollview.getMeasuredWidth()/2) - (Hscrollview.getMeasuredWidth()/2) 
       //We then return the scroll view to the middle 
       HScroll.scrollTo(middleHScroll); 
      } 
      super.onScrollChanged(x, y, oldX, oldY); 
     } 
    } 
} 

xml應該是沿着這些線的東西。要做到這一點還有很多工作要做,但我希望這能讓你走向正確的方向。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textview1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/title_bar" 
      android:gravity="center_horizontal" 
      android:text="PRODUCTS >> PRODUCT DETAILS" /> 

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <RelativeLayout 
       android:id="@+id/RelativeLayout1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
       android:weightSum="2" > 

       <HorizontalScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/HorizontalScrollView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" > 


        <LinearLayout 
         xmlns:android="http://schemas.android.com/apk/res/android" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginRight="70dp" 
         android:orientation="horizontal" > 



         <ImageView 
          android:id="@+id/productimage3" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginLeft="70dp" 
          android:layout_marginRight="20dp" 
          android:layout_weight="1" 
          android:background="@drawable/product_detail_gradient" 
          android:minHeight="100dp" 
          android:minWidth="100dp" /> 

         <ImageView 
          android:id="@+id/productimage2" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginLeft="70dp" 
          android:layout_marginRight="70dp" 
          android:layout_weight="1" 
          android:background="@drawable/product_detail_gradient" 
          android:minHeight="100dp" 
          android:minWidth="100dp" /> 

         <ImageView 
          android:id="@+id/productimage1" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginLeft="70dp" 
          android:layout_marginRight="70dp" 
          android:layout_weight="1" 
          android:background="@drawable/product_detail_gradient" 
          android:minHeight="100dp" 
          android:minWidth="100dp" /> 

        </LinearLayout> 
       </HorizontalScrollView> 

       <RelativeLayout 
        android:id="@+id/description" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_alignParentBottom="true" 
        android:layout_alignParentLeft="true" 
        android:layout_below="@+id/HorizontalScrollView1" 
        android:layout_weight="1" 
        android:background="@drawable/black_img_bg" > 

        <TextView 
         android:id="@+id/title" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="15dip" 
         android:textStyle="bold" > 
        </TextView> 

        <TextView 
         android:id="@+id/desc" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:layout_below="@+id/title" > 
        </TextView> 

        <Button 
         android:id="@+id/addtocart" 
         android:layout_width="80dip" 
         android:layout_height="wrap_content" 
         android:layout_alignParentBottom="true" 
         android:layout_alignParentLeft="true" 
         android:background="@drawable/button_green_lhs" 
         android:gravity="left|center_vertical" 
         android:onClick="addToCartOrBuy" 
         android:text="Add To Cart" 
         android:textSize="10dip" /> 

        <Button 
         android:id="@+id/buy" 
         android:layout_width="150dip" 
         android:layout_height="wrap_content" 
         android:layout_alignParentBottom="true" 
         android:layout_toRightOf="@+id/addtocart" 
         android:background="@drawable/button_orange_mid" 
         android:onClick="addToCartOrBuy" 
         android:text="Buy" 
         android:textSize="10dip" /> 

        <Button 
         android:id="@+id/tellafriend" 
         android:layout_width="80dip" 
         android:layout_height="wrap_content" 
         android:layout_alignParentBottom="true" 
         android:layout_alignParentRight="true" 
         android:layout_toRightOf="@+id/buy" 
         android:background="@drawable/button_green_rhs" 
         android:gravity="right|center_vertical" 
         android:onClick="tellAFriend" 
         android:text="Tell A Friend" 
         android:textSize="10dip" /> 
       </RelativeLayout> 
      </RelativeLayout> 
     </RelativeLayout> 

    </LinearLayout> 
+0

謝謝很多的幫助。幾個問題:什麼是SlowDownThreshold,Hscrollview等謝謝 – png

+0

當用戶停止滾動時,他們並不總是將他們的手指從屏幕上移開。這意味着滾動實際上仍然在移動,只是非常緩慢。 SlowDownThreshold是滾動在應用程序考慮滾動完成之前必須移動的速度。 HScroll是水平滾動佈局。 Horizo​​ntalScrollview Hscroll =(Horizo​​ntalScrollview)findViewById(R.id.Horizo​​ntalScrollView1)。這樣,每次我們想要與水平滾動視圖進行交互時,我們不必使用findViewById。 ps對於遲到的2天回答感到抱歉。 – Reefa

+0

非常感謝。這onscrollchanged是哪個類的方法。我沒有看到它的活動。應該實現任何接口 – png

1

如果我理解正確的話,你只需要簡單地用手勢監聽器,然後調用該方法用於填充的所有文本的意見和imageviews你有下一個信息實例。

我發現這個例子在我學習手勢識別時非常有用。

首先手勢監聽器添加到您的公共MyClass類

import android.widget.Toast; 
public class myClass extends Activity implements OnGestureListener { 

然後將下面的順利,之後,使我們可以聽 每個觸摸事件。

private static final int SWIPE_MIN_DISTANCE = 6; //120; 
    private static final int SWIPE_MAX_OFF_PATH = 125; //250; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 100; //200; 
    private GestureDetector gestureScanner;@ 
    Override 
    public boolean onTouchEvent(MotionEvent me) { 
     return gestureScanner.onTouchEvent(me); 
    } 
    //@Override 
    public boolean onDown(MotionEvent e) { 
     //viewA.setText("-" + "DOWN" + "-"); 
     return true; 
    } 
    //@Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     try { 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
       return false; 
      // right to left swipe 
      if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show(); 

      } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show(); 

      } else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(getApplicationContext(), "Swipe up", Toast.LENGTH_SHORT).show(); 

      } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(getApplicationContext(), "Swipe down", Toast.LENGTH_SHORT).show(); 

      } 
     } catch (Exception e) { 
      // nothing 
     } 

     return true; 
    }@ 
    Override 
    public void onLongPress(MotionEvent e) { 
     Toast mToast = Toast.makeText(getApplicationContext(), "Long Press", Toast.LENGTH_SHORT); 
     mToast.show(); 
    } 
    //@Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
     //viewA.setText("-" + "SCROLL" + "-"); 
     return true; 
    } 
    //@Override 
    public void onShowPress(MotionEvent e) { 
     //viewA.setText("-" + "SHOW PRESS" + "-"); 
    } //@Override 
    public boolean onSingleTapUp(MotionEvent e) { 
     Toast mToast = Toast.makeText(getApplicationContext(), "Single Tap", Toast.LENGTH_SHORT); 
     mToast.show(); 
     return true; 
    } 

你可以把你用它來調用相關的監聽器哪裏敬酒是內上一個或下一個實例的方法。

你可以通過改變這些變量

private static final int SWIPE_MIN_DISTANCE = 6; //120; 
private static final int SWIPE_MAX_OFF_PATH = 125; //250; 
private static final int SWIPE_THRESHOLD_VELOCITY = 100; //200; 

我希望這有助於走出調整揮筆的sensotivity。

編輯:

對不起,混合起來,你的onCreate應包括gestureScanner =新GestureDetector(本);

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.product_details); 
    setContent(); 
    gestureScanner = new GestureDetector(this); 
} 

這應該讓它工作,除了視覺效果。您可以嘗試使用ScrollView來輔助幻燈片。

+0

我建議使用滾動視圖。設置3組圖像和文本控件。然後,當用戶滾動到滾動視圖的頂部時,設置新元素並將其滾動到中間。讓滾動視圖執行艱苦的工作,並允許圖像和文本視圖的無縫重繪。 – Reefa

+0

我試過了。請參閱我的更新。但我沒有看到不能滑動的效果,也沒有烤麪包來了。我想我缺少一些基本的東西。 – png

+0

我編輯了我的答案,讓你的聽衆開始工作。 – Reefa