2012-10-19 4 views
4

나는 목록보기가 있습니다. 항목을 클릭하면 해당 항목에 대한 상세보기가 열립니다. 이 레이아웃에는 텍스트보기, ImageView Buttons 등과 같은 많은 위젯이 있습니다. 이제 항목의 세부 뷰를 슬라이드하여 목록의 다음 항목에 대한 상세 뷰를 표시하고 싶습니다. 마찬가지로 왼쪽에서 오른쪽으로 이전 항목.android : 슬라이드보기에서 왼쪽 또는 오른쪽으로 스 와이프

나는 슬라이딩 뷰를 구현할 수 없다. 목록에서 이전/다음 항목을 얻는 방법을 수행했다. 그러나 슬라이딩 실제 같은 내가 gesturedetector 시도 문제

입니다 Android: Swipe left to right and right to left

및 다른 예이다. 그러나 슬라이드하려고 할 때 아무런 효과가 없습니다. 나는 어떤 시각적 인 미끄러짐도 전혀 보지 못한다.

해결 방법?

이 코드 있지만 여전히 슬라이드 난 당신이 단순히 모든 텍스트 뷰를 채우는 데 사용하는 메소드를 호출 한 다음 제스처 리스너를 사용하고 필요로하고 당신이 가지고 imageviews 제대로 이해하면

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은 가로 스크롤 레이아웃입니다. HorizontalScrollview Hscroll = (HorizontalScrollview) findViewById (R.id.HorizontalScrollView1)입니다. 이렇게하면 수평 스크롤 뷰와 상호 작용할 때마다 findViewById를 사용할 필요가 없습니다. ps는 2 일 늦은 대답에 대해 유감스럽게 생각합니다. – Reefa

+0

고마워요. 이 onscrollchanged 클래스 메서드입니다. 나는 활동을 위해 그것을 보지 않는다. 모든 인터페이스를 구현해야합니다. – png

1

을 발생하지를 시도 정보의 다음 인스턴스.

제스처 인식에 대해 배웠을 때이 예제가 매우 유용하다는 것을 알았습니다.

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

그런 다음 다음 잘 후에 간다 myClass가 우리가 각 터치 이벤트에 대한 을들을 수 있도록

먼저 공개 클래스에 제스처 리스너를 추가합니다.

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

나는 이것을 시도했다. Pls 내 업데이 트를 참조하십시오. 하지만 그나마 미끄러지는 효과는 보이지 않고 유명인도 나오지 않습니다. 나는 뭔가를 놓치고 있다고 생각한다. – png

+0

리스너가 시작하도록 내 대답을 편집했습니다. – Reefa

관련 문제