这是一个非常有趣的话题这个问题。并准备做一定量的习惯材料。
首先,放下HorizonalScrollView,我认为它不会帮助你。每个项目的布局应该是这样的(伪代码,你可以自己构建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从适配器内)的每个topContent
与topContentView.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;
};
};
}
会更清楚,如果你可以发布你想要 – stinepike 2013-04-23 21:21:16
确定患病把东西在一起什么的图像现在 – Osman 2013-04-23 21:24:33
去了,虽然我的应用程序,看看我能找到一个有类似的东西,Gmail应用程序有它。这是一个图像的链接,为您提供了一个想法,在您将项目滑动到一边后,它会显示一个撤消或归档按钮,位置为http://mobilenewspedia.com/wp-content/uploads/2012 /10/9044_Gmail-660x350.jpg – Osman 2013-04-23 21:31:04