2013-04-23 25 views
0

所以这不是一个关于代码的技术问题,而是更多的一些想法。我对android开发并不熟悉,因此我不完全熟悉它提供的许多功能。自定义水平滚动动画来跟踪在Android的视图上的手指

我有一个在屏幕上的项目列表,你可以上下滚动查看不同的项目。

当我决定让项目能够左右滑动以显示像panal这样的设置时,我的问题出现了,它们下面有几个opton(每个项目都有它自己的设置,而不是所有的设置屏幕物品交易)。我四处寻找不同的方式来做到这一点,但似乎找不到一个可行的方法。我到目前为止的想法是在屏幕上向左或向右设置一个HorizonalScrollView,其中包含我的项目,但是当我将文本区域放在HorizonalScrollView中时,它只占用屏幕的一半,所以我可以并排放置两个,不是我想要的,最终的结果不会是我想象的。我真的想要一个解决方案,允许设置在该项目下,所以当你推开它的方式显示设置。

有没有更好的方法,或者我应该继续努力使我的HorizonalScrollView工作,任何指南或想法如何我可以去这个将不胜感激。

虽然我的应用程序看我是否能找到一个类似的东西,但gmail应用程序已经有了。这是一个图像的链接,给你一个想法,在你把物品滑到一边后,它会在物品的位置出现撤消或存档按钮,就好像它们隐藏在你擦掉的列表项下enter image description here

+0

会更清楚,如果你可以发布你想要 – stinepike 2013-04-23 21:21:16

+0

确定患病把东西在一起什么的图像现在 – Osman 2013-04-23 21:24:33

+0

去了,虽然我的应用程序,看看我能找到一个有类似的东西,Gmail应用程序有它。这是一个图像的链接,为您提供了一个想法,在您将项目滑动到一边后,它会显示一个撤消或归档按钮,位置为http://mobilenewspedia.com/wp-content/uploads/2012 /10/9044_Gmail-660x350.jpg – Osman 2013-04-23 21:31:04

回答

2

这是一个非常有趣的话题这个问题。并准备做一定量的习惯材料。

首先,放下Horizo​​nalScrollView,我认为它不会帮助你。每个项目的布局应该是这样的(伪代码,你可以自己构建XML =]):

FrameLayout 
    RelativeLayout id:topContent background:someSolidColor 
      // inside this RelativeLayout, the stuff that is visible on the ListView 
    RelativeLayout id:bottomContent 
      // inside this RelativeLayout, the stuff that is behind the content 
/FrameLayout 

这样你会从字面上把一件事在其他的顶部。另请注意,topContent具有纯色的背景。如果您不指定任何背景,则两个RelativeLayouts都将可见。另外请注意,我使用RelativeLayout,只是因为我喜欢它们,而且我喜欢它们的灵活性,但这取决于列表视图的内容和您的设置。

现在当事情变得有趣时,您需要使用手势检测器来检测手指滑动,并使用该值在id:topContent上生成边距偏移量。

您可以创建这样一个TouchListener:

public class MySlideListener extends View.OnTouchListener{ 

    private View v; 
    private GestureDetector gestureDetector; 

    public MySlideListener (View v){ 
     this.v = v; 
     gestureDetector = new GestureDetector(v.getContext(), myGestureListener); 
    } 

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

    private SimpleOnGestureListener myGestureListener = new SimpleOnGestureListener(){ 
     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){ 

      // now here we make the view scroll 
      MarginLayoutParams lp = (MarginLayoutParams) view.getLayoutParams(); 
      lp.leftMargin = distanceX; 
      lp.rightMargin = -distanceX; 

      // You might need to call view.requestLayout(); 
      // but first give it a try without it 

      // This part of the method for processing the horizontal 
      // offset can (and should) be further developed to add some 
      // 'snap-in' or transparency functionality to make the whole 
      // concept work better. 
      // But this code should give you a proof of concept on how to deal with stuff. 

      // The important part is that now you have a call back that have access 
      // to the view during onScroll. 

      // Also might be necessary to enable/disable the bottomContent view 
      // in order for it to be not clickable whilst not visible. 

      return true; 
     } 
    } 
} 

,然后设定一个新的听众,为您的ListView(可能是getView从适配器内)的每个topContenttopContentView.setOnTouchListener(new MySlideListener(topContentView));

请保持记住,我输入了所有这些代码,并且100%未经测试!

编辑:

上面的代码是正确的方向,但它是一个100%的未经检验的事。 现在下面的代码,我刚刚编译,测试,并且此代码工作!

该类也更高效一点,因为您只能创建一个,并将同一实例应用于在适配器上创建的所有项目。您可以看到它正在让视图在触摸事件上滚动。

public class MySlideListener implements View.OnTouchListener { 

    private View view; 
    private ListView listView; 
    private GestureDetector gestureDetector; 

    public MySlideListener(ListView lv) { 
     listView = lv; 
     gestureDetector = new GestureDetector(lv.getContext(), myGestureListener); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     view = v; 
     gestureDetector.onTouchEvent(event); 
     return true; 
    } 

    private SimpleOnGestureListener myGestureListener = new SimpleOnGestureListener() { 

    private int origLeft, origRight; 

    public boolean onDown(MotionEvent e) { 
     MarginLayoutParams lp = (MarginLayoutParams) view.getLayoutParams(); 
     origLeft = lp.leftMargin; 
     origRight = lp.rightMargin; 
     return true; 
    }; 

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
     listView.requestDisallowInterceptTouchEvent(true); 
     MarginLayoutParams lp = (MarginLayoutParams) view.getLayoutParams(); 
     lp.leftMargin = (int) (origLeft + (e2.getRawX() - e1.getRawX())); 
     lp.rightMargin = (int) (origRight - (e2.getRawX() - e1.getRawX())); 
     view.requestLayout(); 
     return true; 
    }; 
    }; 
} 
+0

这太好了,你真的超越了。我将开始试图实现这样的事情,但非常感谢! – Osman 2013-04-23 21:49:50

+0

嗨。别客气。说实话,我厌倦了阅读SO人问如何解决NullPointerException,并且很高兴看到像你这样的真正问题。我注意到有几个事件传递/处理GestureDetector和SimpleGestureListener缺少。我只是更新了代码。如果它不能马上工作,你可以在这里查看someones实现http://stackoverflow.com/questions/10886963/why-is-my-simpleongesturelistener-not-functioning – Budius 2013-04-23 21:58:03

+0

不知道你是否会再次看到这一点,但我有视图不移动的问题,我必须更新屏幕上的视图或什么?我已经验证了我的利润率正在向左右滚动,因此我的问题是该更改没有在屏幕上注册? – Osman 2013-04-24 01:17:30