机器人:向左或向右滑动滑意见 [英] android: swipe left or right to slide views

查看:203
本文介绍了机器人:向左或向右滑动滑意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图。在点击一个项目,为项目的详细信息视图打开。这种布局有许多部件,如文本视图, ImageView的按钮等。现在我想滑的项目这个细节视图来显示下一个项目在列表中的详细信息视图。同样previous项目为从左到右。

我不能够实现视图滑动我已经做了如何获取该列表中的preV /下一个项目。但实际滑动的问题

我试过gesturedetector像<一个href="http://stackoverflow.com/questions/7198587/android-swipe-left-to-right-and-right-to-left">Android:刷卡左到右,从右到左

和其他一些例子。但是,当我试着滑,有没有效果。我看不到任何目视滑动的。

如何解决这一问题?

试过这种code,但仍然没有滑动发生

 公共类ProductActivity扩展活动实现OnGestureListener {


     @
     覆盖
     保护无效的onCreate(包savedInstanceState){
         super.onCreate(savedInstanceState);
         的setContentView(R.layout.product_details);
         setContent();
     }

     私人无效setContent(){
         捆绑额外= getIntent()getExtras()。
         TextView的标题=(TextView中)findViewById(R.id.title);
         title.setText(extras.getString(标题));
         TextView的递减=(TextView中)findViewById(R.id.desc);
         desc.setText(extras.getString(说明));
         按钮团购=(按钮)findViewById(R.id.buy);
         串标价=($+的String.Format(%2克,extras.getDouble(价格))+立即购买&GT;&GT;);
         buy.setText(标价);
         ImageView的形象=(ImageView的)findViewById(R.id.productimage);
         Utils.imageLoader.DisplayImage(extras.getString(图像),图像);

     }




     私有静态最终诠释SWIPE_MIN_DISTANCE = 6; // 120;
     私有静态最终诠释SWIPE_MAX_OFF_PATH = 125; // 250;
     私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 100; // 200;
     私人GestureDetector gestureScanner;

     @
     覆盖
     公共布尔的onTouchEvent(MotionEvent我){
         返回gestureScanner.onTouchEvent(箱);
     }

     // @覆盖
     公共布尔onDown(MotionEvent E){
         // viewA.setText( - +DOWN+ - );
         返回true;
     }

     // @覆盖
     公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
         浮动velocityY){
         尝试 {
             如果(Math.abs(e1.getY() -  e2.getY())&GT; SWIPE_MAX_OFF_PATH)
                 返回false;
             //从右向左轻扫
             如果(e1.getX() -  e2.getX()&GT; SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityX)&GT; SWIPE_THRESHOLD_VELOCITY){
                 Toast.makeText(getApplicationContext(),左刷卡,
                     Toast.LENGTH_SHORT).show();

             }否则,如果(e2.getX() -  e1.getX()&GT; SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)&GT; SWIPE_THRESHOLD_VELOCITY){
                 Toast.makeText(getApplicationContext(),右刷卡,
                     Toast.LENGTH_SHORT).show();

             }否则,如果(e1.getY() -  e2.getY()&GT; SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)&GT; SWIPE_THRESHOLD_VELOCITY){
                 Toast.makeText(getApplicationContext(),向上滑动,
                     Toast.LENGTH_SHORT).show();

             }否则,如果(e2.getY() -  e1.getY()&GT; SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)&GT; SWIPE_THRESHOLD_VELOCITY){
                 Toast.makeText(getApplicationContext(),向下滑动
                     Toast.LENGTH_SHORT).show();

             }
         }赶上(例外五){
             //什么
         }

         返回true;
     }

     @
     覆盖
     公共无效onLong preSS(MotionEvent E){
         吐司mToast = Toast.makeText(getApplicationContext(),龙preSS
             Toast.LENGTH_SHORT);
         mToast.show();
     }

     // @覆盖
     公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,
         浮动distanceY){
         // viewA.setText( - +SCROLL+ - );
         返回true;
     }

     // @覆盖
     公共无效OnShow中preSS(MotionEvent E){
         // viewA.setText( - +SHOW $ P $干燥综合征+ - );
     } // @覆盖

     公共布尔onSingleTapUp(MotionEvent E){
         吐司mToast = Toast.makeText(getApplicationContext(),单塔,
             Toast.LENGTH_SHORT);
         mToast.show();
         返回true;
     }

 }
 

解决方案

我想到了一个新的答案会是一个好主意,因为旧的是如此的不同。

我将首先一点背景: 这里的想法是使用水平滚动视图移动到下一组控制在pretty的方式。因此,当用户停止滚动滚动视图向左或向右,我们要改变的图片和文字,然后将滚动回到中间的preparation下一个刷卡。有人已经回答了如何监听时滚动视图站 我已经包括了如何使用它来获取你正在寻找的效果几乎是工作的例子。

我希望这有助于出来,随意问你是否有问题了。

  @覆盖
 保护无效onScrollChanged(INT X,INT Y,诠释oldX,INT oldY){
     如果(Math.abs(Y  -  oldY)&GT; SlowDownThreshold){
         currentlyScrolling = TRUE;
     } 其他 {
         currentlyScrolling = FALSE;
         如果(!currentlyTouching){
             //滚动停止...办理这里
             //检查它是否是一个向左或向右轻扫通过比较新老滚动位置
             如果(Y&GT; oldY){
                 //滚动向右移动
                 //所以我们会做这样的事情
                 setContents(指标+ 1);
                 //我们找出滚动的中间
                 INT middleHScroll =(Hscrollview.getMeasuredWidth()/ 2) - (Hscrollview.getMeasuredWidth()/ 2)
                 //我们再滚动视图返回到中间
                 HScroll.scrollTo(middleHScroll);
             } 其他 {
                 //滚动移动左
                 //我们得到设置图片和文字内容的previous指数
                 setContents(指数 -  1);
                 //我们找出滚动的中间
                 INT middleHScroll =(Hscrollview.getMeasuredWidth()/ 2) - (Hscrollview.getMeasuredWidth()/ 2)
                 //我们再滚动视图返回到中间
                 HScroll.scrollTo(middleHScroll);
             }
             super.onScrollChanged(X,Y,oldX,oldY);
         }
     }
 }
 

在XML应该是沿着这些路线的东西。还有大量的工作要做,以得到这个工作,但我希望这让你在正确的方向。

 &LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:方向=垂直&GT;

        &LT;的TextView
            机器人:ID =@ + ID / textview1
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=@可绘制/ title_bar
            机器人:重力=center_horizo​​ntal
            机器人:文本=产品&GT;&GT;产品范围/&GT;

        &LT; RelativeLayout的
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT&GT;

            &LT; RelativeLayout的
                机器人:ID =@ + ID / RelativeLayout1
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:方向=垂直
                机器人:weightSum =2&GT;

                &LT; Horizo​​ntalScrollView
                    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                    机器人:ID =@ + ID / Horizo​​ntalScrollView1
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_alignParentLeft =真
                    机器人:layout_alignParentTop =真正的&GT;


                    &LT;的LinearLayout
                        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:layout_marginRight =70dp
                        机器人:方向=横向&GT;



                        &LT; ImageView的
                            机器人:ID =@ + ID / productimage3
                            机器人:layout_width =WRAP_CONTENT
                            机器人:layout_height =WRAP_CONTENT
                            机器人:layout_marginLeft =70dp
                            机器人:layout_marginRight =20dp
                            机器人:layout_weight =1
                            机器人:背景=@可绘制/ product_detail_gradient
                            机器人:=了minHeight100dp
                            机器人:=了minWidth100dp/&GT;

                        &LT; ImageView的
                            机器人:ID =@ + ID / productimage2
                            机器人:layout_width =WRAP_CONTENT
                            机器人:layout_height =WRAP_CONTENT
                            机器人:layout_marginLeft =70dp
                            机器人:layout_marginRight =70dp
                            机器人:layout_weight =1
                            机器人:背景=@可绘制/ product_detail_gradient
                            机器人:=了minHeight100dp
                            机器人:=了minWidth100dp/&GT;

                        &LT; ImageView的
                            机器人:ID =@ + ID / productimage1
                            机器人:layout_width =WRAP_CONTENT
                            机器人:layout_height =WRAP_CONTENT
                            机器人:layout_marginLeft =70dp
                            机器人:layout_marginRight =70dp
                            机器人:layout_weight =1
                            机器人:背景=@可绘制/ product_detail_gradient
                            机器人:=了minHeight100dp
                            机器人:=了minWidth100dp/&GT;

                    &LT; / LinearLayout中&GT;
                &LT; / Horizo​​ntalScrollView&GT;

                &LT; RelativeLayout的
                    机器人:ID =@ + ID /说明
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_alignParentBottom =真
                    机器人:layout_alignParentLeft =真
                    机器人:layout_below =@ + ID / Horizo​​ntalScrollView1
                    机器人:layout_weight =1
                    机器人:背景=@可绘制/ black_img_bg&GT;

                    &LT;的TextView
                        机器人:ID =@ + ID /标题
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:TEXTSIZE =15dip
                        机器人:TEXTSTYLE =黑体&GT;
                    &LT; / TextView的&GT;

                    &LT;的TextView
                        机器人:ID =@ + ID /递减
                        机器人:layout_width =FILL_PARENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:layout_below =@ + ID /标题&GT;
                    &LT; / TextView的&GT;

                    &LT;按钮
                        机器人:ID =@ + ID / addtocart
                        机器人:layout_width =80dip
                        机器人:layout_height =WRAP_CONTENT
                        机器人:layout_alignParentBottom =真
                        机器人:layout_alignParentLeft =真
                        机器人:背景=@可绘制/ button_green_lhs
                        机器人:重力=左| center_vertical
                        机器人:的onClick =addToCartOrBuy
                        机器人:文本=放入购物车
                        机器人:TEXTSIZE =10dip/&GT;

                    &LT;按钮
                        机器人:ID =@ + ID /买入
                        机器人:layout_width =150dip
                        机器人:layout_height =WRAP_CONTENT
                        机器人:layout_alignParentBottom =真
                        机器人:layout_toRightOf =@ + ID / addtocart
                        机器人:背景=@可绘制/ button_orange_mid
                        机器人:的onClick =addToCartOrBuy
                        机器人:文本=买入
                        机器人:TEXTSIZE =10dip/&GT;

                    &LT;按钮
                        机器人:ID =@ + ID / tellafriend
                        机器人:layout_width =80dip
                        机器人:layout_height =WRAP_CONTENT
                        机器人:layout_alignParentBottom =真
                        机器人:layout_alignParentRight =真
                        机器人:layout_toRightOf =@ + ID /买入
                        机器人:背景=@可绘制/ button_green_rhs
                        机器人:重力=右| center_vertical
                        机器人:的onClick =tellAFriend
                        机器人:文本=告诉朋友
                        机器人:TEXTSIZE =10dip/&GT;
                &LT; / RelativeLayout的&GT;
            &LT; / RelativeLayout的&GT;
        &LT; / RelativeLayout的&GT;

    &LT; / LinearLayout中&GT;
 

I have a list view. On click of an item a detailed view for the item is open. This layout has many widgets like text view, ImageView Buttons etc. Now I want to slide this detail view of the items to show the detail view of next item in the list. Similarly previous item for left to right.

I am not able to implement the view sliding I have done how to get the prev / next items in the list. But the actual sliding is the issue

I tried gesturedetector like in Android: Swipe left to right and right to left

and some other examples. But when I try to slide, there is no effect. I don't see any visual sliding at all.

How to fix this?

Tried this code but still no slide happening

 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;
     }

 }

解决方案

I thought a new answer would be a good idea since the old one is so different.

I'll start with a bit of background: The idea here is to use a horizontal scroll view to move to the next set of controls in a pretty way. So when the user stop scrolling the scroll view to the left or right, we want to change the pictures and text, and then move the scroll back to the middle in preparation for the next swipe. Someone has already answered how to listen for when a scroll view stops I've included an almost working example of how you might use this to get the effect you are looking for.

I hope this helps out and feel free to ask if you have anymore problems.

 @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);
         }
     }
 }

The xml should be something along these lines. There is still plenty of work to do to get this working but I hope this puts you in the right direction.

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

这篇关于机器人:向左或向右滑动滑意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆