2011-11-30 12 views
27

मैं स्क्रॉल व्यू युक्त व्यूफ्लिपर के साथ एक लेआउट बनाने की कोशिश कर रहा हूं। विचार पिछले/अगले स्क्रॉल व्यू पर जाने के लिए क्षैतिज स्वाइप का पता लगाने के लिए है। इसके अलावा, स्क्रॉल व्यू में एक और व्यूफ्लिपर है जिसमें छवि/दृश्य को लंबवत स्वाइप डिटेक्टर के साथ पिछले/अगले ImageView पर जाना है। जब मैं ScrollView को एक LinearLayout द्वारा प्रतिस्थापित करता हूं तो दोनों इशारा डिटेक्टर ठीक से काम करते हैं, लेकिन स्क्रॉलव्यू के साथ, कोई भी काम नहीं (इशारा श्रोताओं भी ट्रिगर्स नहीं होते हैं)। स्क्रॉलव्यू का उपयोग करने से मेरा इशारा डिटेक्टर अक्षम क्यों होता है? मैं इसे कैसे कारगर बना सकता हूं?इशारा पहचान और स्क्रॉलव्यू समस्या

गतिविधि

public class ProduitHome extends Activity{ 

    private Resources res; 
    float density; 

    private int position, parent_id;; 
    private int num_products; 

    private Produit produit; 
    private ImageDownloader mImageLoader; 

    private ViewFlipper product_viewflipper; 
    private ScrollView current_product_layout; 
    Animation next_product_out, next_product_in, previous_product_in, previous_product_out; 

    private GestureDetector galleryGestureDetector; 
    private View.OnTouchListener galleryGestureListener; 

    private GestureDetector productGestureDetector; 
    private View.OnTouchListener productGestureListener; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.produit_home); 

     num_products = GlobalData.map_list_produits.get(parent_id).size(); 

     product_viewflipper = (ViewFlipper) findViewById(R.id.product_viewflipper); 

     LayoutInflater inflater = getLayoutInflater(); 


     // Add num_products view to the viewflipper 

     for(int i=0; i<num_products; i++){ 
      ScrollView product_detail = (ScrollView) inflater.inflate(R.layout.produit_detail, null); 
      product_viewflipper.addView(product_detail); 
     } 


     // Set data and show current product 

     current_product_layout = (ScrollView) product_viewflipper.getChildAt(position); 
     product_viewflipper.setDisplayedChild(position); 

     setProductData(); 


     // Set swipe listener to switch product 

     productGestureDetector = new GestureDetector(new ProductGestureListener()); 
     productGestureListener = new View.OnTouchListener() 
     { 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       if (productGestureDetector.onTouchEvent(event)) 
       { 
        return true; 
       } 
       else{ 
        return false; 
       } 
      } 
     }; 

     product_viewflipper.setOnTouchListener(productGestureListener); 


     // Set switch product animation 

     next_product_out = AnimationUtils.loadAnimation(this, R.anim.next_product_out); 
     next_product_in = AnimationUtils.loadAnimation(this, R.anim.next_product_in); 
     previous_product_in = AnimationUtils.loadAnimation(this, R.anim.previous_product_in); 
     previous_product_out = AnimationUtils.loadAnimation(this, R.anim.previous_product_out); 

    } 


    class VerticalSwipeListener extends SimpleOnGestureListener { 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

      final int SWIPE_MIN_DISTANCE = 80; 
      final int SWIPE_MAX_OFF_PATH = 250; 
      final int SWIPE_THRESHOLD_VELOCITY = 200; 

      try { 
       if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH) 
        return false;     

       ViewFlipper gallery = (ViewFlipper)current_product_layout.findViewById(R.id.product_gallery); 

       if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { 
        gallery.showNext();      
       } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { 
        gallery.showPrevious(); 
       } 
       ((RadioGroup)current_product_layout.findViewById(R.id.gallery_nav)).check(gallery.getDisplayedChild()); 
      } catch (Exception e) { 
      } 
      return false; 
     } 
    } 


    class ProductGestureListener extends SimpleOnGestureListener { 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

      final int SWIPE_MIN_DISTANCE = 120; 
      final int SWIPE_MAX_OFF_PATH = 250; 
      final int SWIPE_THRESHOLD_VELOCITY = 200; 

      if(!Utils.IsOnline(ProduitHome.this)){ 
       SRPDialogs.show(ProduitHome.this, SRPDialogs.NOT_CONNECTED); 
      } 
      else{ 

       try { 
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
         return false; 
        if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 

         // show next product 

        } else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 

        // show previous product 

        } 
       } catch (Exception e) { 
       } 
      } 
      return false; 
     } 
    } 

    public void setProductData(){ 

     produit = GlobalData.map_produits.get(GlobalData.map_list_produits.get(parent_id).get(position).id); 

     TextView name = (TextView) current_product_layout.findViewById(R.id.name); 
     name.setText(produit.libelle); 

     // Load gallery 

     int nPics = produit.list_url_pic.size(); 

     if(nPics>0){ 

      ViewFlipper gallery = (ViewFlipper) current_product_layout.findViewById(R.id.product_gallery); 
      gallery.removeAllViews();   

      mImageLoader = new ImageDownloader(res, 
        ((BitmapDrawable)res.getDrawable(R.drawable.default_row_pic)).getBitmap(), 1);   

      final ViewFlipper.LayoutParams params_vf = new ViewFlipper.LayoutParams(ViewFlipper.LayoutParams.FILL_PARENT, ViewFlipper.LayoutParams.FILL_PARENT); 

      for(String url : produit.list_url_pic){ 

       // Add images to viewflipper 
       ImageView imageView_p = new ImageView(this); 
       imageView_p.setLayoutParams(params_vf); 
       imageView_p.setScaleType(ImageView.ScaleType.CENTER_CROP); 
       imageView_p.setTag(url); 
       imageView_p.setImageResource(R.drawable.default_row_pic); 
       mImageLoader.download(url, imageView_p); 
       gallery.addView(imageView_p); 
      } 

      // Swipe detector to switch picture in gallery 

      galleryGestureDetector = new GestureDetector(new VerticalSwipeListener()); 
      galleryGestureListener = new View.OnTouchListener() 
      { 
       public boolean onTouch(View v, MotionEvent event) 
       { 
        if (galleryGestureDetector.onTouchEvent(event)) 
        { 
         return true; 
        } 
        else{ 
         return false; 
        } 
       } 
      }; 

     } 
    } 
} 

जनक लेआउट

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/product_home" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:orientation="vertical" 
    android:background="@color/grey_bg"> 

    <!-- more stuff --> 

    <ViewFlipper android:id="@+id/product_viewflipper" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_below="@id/header_logo" /> 

    <!-- more stuff --> 

</RelativeLayout> 

ViewFlipper के बच्चों लेआउट

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:background="@color/grey_bg"> 

    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:orientation="vertical" 
     android:gravity="center_horizontal"> 

     <!-- more stuff --> 

     <RelativeLayout android:layout_below="@id/bg_content_top" 
      android:layout_above="@id/bg_content_bottom" 
      android:layout_width="300dp" android:layout_height="fill_parent" 
      android:background="@drawable/bg_content" 
      android:paddingRight="3dp" android:paddingLeft="3dp" 
      android:layout_centerHorizontal="true"> 

      <!-- more stuff --> 

      <RelativeLayout android:id="@+id/content" 
       android:layout_below="@id/title_container" 
       android:layout_above="@id/bg_content_bottom" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:paddingLeft="7dp" android:paddingRight="7dp" 
       android:paddingTop="10dp" android:paddingBottom="10dp">    

       <ViewFlipper android:id="@+id/product_gallery" 
        android:clickable="true" android:focusable="false" 
        android:layout_width="100dp" android:layout_height="150dp" 
        android:layout_marginRight="10dp" 
        android:layout_below="@id/title_container" 
        android:layout_toRightOf="@id/gallery_nav" /> 

       <!-- more stuff --> 

      </RelativeLayout> 

     </RelativeLayout> 

     <!-- more stuff --> 

    </LinearLayout> 

</ScrollView> 

उत्तर

53

मैं अपने गतिविधि में

@Override 
public boolean dispatchTouchEvent(MotionEvent ev){ 
    super.dispatchTouchEvent(ev);  
    return productGestureDetector.onTouchEvent(ev); 
} 

जोड़ने के लिए किया था।

+1

आपको इसे कहां जोड़ना था? –

+0

@mirroredAbstraction: गतिविधि में। – jul

+2

मैं टुकड़े टुकड़े का उपयोग कर रहा हूँ, Fragments में ऐसा करने के लिए किसी भी तरह से? –

10

मेरा जवाब सैम है मैं अपने आखिरी एक के रूप में छोड़कर मैं और अधिक स्पष्ट होने जा रहा हूं। HorizontalScrollView within ScrollView Touch Handling (तो यह एक वोट देने के अगर जाना:

बदलें

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:background="@color/grey_bg"> 

<your.packagename.CustomScrollView ... etc> 

करने के लिए एक वर्ग

public class CustomScrollView extends ScrollView { 
    private GestureDetector gestureDetector; 
    View.OnTouchListener gestureListener; 

    public CustomScrollView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      gestureDetector = new GestureDetector(new YScrollDetector()); 
      setFadingEdgeLength(0); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     return super.onTouchEvent(ev); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     //Call super first because it does some hidden motion event handling 
     boolean result = super.onInterceptTouchEvent(ev); 
     //Now see if we are scrolling vertically with the custom gesture detector 
     if (gestureDetector.onTouchEvent(ev)) { 
      return result; 
     } 
     //If not scrolling vertically (more y than x), don't hijack the event. 
     else { 
      return false; 
     } 
    } 

    // Return false if we're scrolling in the x direction 
    class YScrollDetector extends SimpleOnGestureListener { 
     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float  distanceY) { 
     try { 
      if (Math.abs(distanceY) > Math.abs(distanceX)) { 
       return true; 
      } else { 
       return false; 
      } 
     } catch (Exception e) { 
      // nothing 
     } 
     return false; 
    } 
} 

कि कोड शीर्ष जवाब यहाँ से आता बनाएं जवाब उपयोगी है)।

आप सीधा दिशा प्राप्त करना चाहते हैं तो बदल

if (Math.abs(distanceY) > Math.abs(distanceX)) { 

if (Math.abs(distanceY) < Math.abs(distanceX)) { 

को CustomScrollView केवल एक धुरी में स्वाइप, या तो क्षैतिज या लंबवत ऊपर कोड के 2 लाइनों के आधार अवरोधन । चूंकि यह केवल एक धुरी में स्वाइप करता है, बाकी की घटनाएं अपने बच्चों को पास कर दी जाएंगी, अब आप अपनी गतिविधि में अपने इशारा/स्पर्श श्रोता के साथ ईवेंट को संभाल सकते हैं।

आपको किसी भी संदर्भ/कोस्ट को ScrollView पर नए कस्टम एक (CustomScrollView) में बदलने की आवश्यकता होगी।

+1

स्क्रॉल करते समय, ऐप कभी भी आपकी ऑनस्कोल विधि नहीं चलाता है। – jul

0
parentScrollView.setOnTouchListener(new View.OnTouchListener() { 

       @Override 
       public boolean onTouch(View v, MotionEvent event) { 
        return productGestureDetector.onTouchEvent(event); 
       } 
      }); 

टच लिस्टनर पर अपना मुख्य स्क्रॉलव्यू सेट करें और इस कोड को मेरे लिए यह काम लागू करें। मैं आशा करता हूं कि आपके लिए सहायक होगा।

संबंधित मुद्दे