【问题标题】:Horizontal swiping in stackview在stackview中水平滑动
【发布时间】:2014-01-12 18:12:02
【问题描述】:

我在一个需要 stackview 的应用程序中。但是我必须实现水平滑动才能在 stackview 项目之间切换,并且 UI 看起来就像正常的一样。请帮我做。

我已经检查过这个link

但其中的 UI 只是一个封面流程。我想让 UI 看起来和 stackview 一样。

【问题讨论】:

    标签: java android android-ui stackview


    【解决方案1】:

    我创建一个自定义堆栈视图

      public class custom_stackview extends StackView{
    
        float x1, x2, y1, y2, dx, dy;
        public custom_stackview(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
        }
        public custom_stackview(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
            super.onTouchEvent(event);
    
            switch(event.getAction()) {
                case(MotionEvent.ACTION_DOWN):
                    x1 = event.getX();
                    y1 = event.getY();
                    break;
               case(MotionEvent.ACTION_MOVE):
               case(MotionEvent.ACTION_UP) :{
                x2 = event.getX();
                y2 = event.getY();
                dx = x2 - x1;
                dy = y2 - y1;
    
                // Use dx and dy to determine the direction
                if (Math.abs(dx) > Math.abs(dy)) {
                    if (dx > 0) {// direction = "right";
                        showNext();
                    } else {
    
                        showPrevious();
    
    
                    }
    
                }
                }
             // Log.v("hiiiiiiiiiii", direction+re);
            }
            return true;
    
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stubLog.v("hiiiiiiiiiii","touched");
            Log.v("hiiiiiiiiiii","toucheddddddddd");
            //boolean re =false;
    
    
            return false;
        }
    
    }
    

    用作

     <package_name.custom_stackview
            android:id="@+id/stackview"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:loopViews="true"
            />
    

    【讨论】:

    • +1 几乎完整的解决方案 - 我需要修复以下代码以使其表现良好 - 删除 case(MotionEvent.ACTION_MOVE): 并添加 default: break;,否则它会在移动\滑动时继续移动视图.
    【解决方案2】:

    最近遇到了一个很棒的小技巧。 在您的布局 XML 中使用:

    <StackView
        android:animateLayoutChanges="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:rotation = "-90" />
    

    然后在项目的 XML 中:

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:rotation = "90" />
    

    您可以根据需要使用数字来改变方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多