这个问题是一个非常有趣的话题。并准备好做一些定制的事情。
首先,删除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,只是因为我喜欢它们,而且我喜欢它们的灵活性,但这将取决于您的列表视图的内容和您的设置。
现在,当事情变得有趣时,您将需要一个 GestureDetector 来检测手指滑动,您将使用该值在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;
}
}
}
然后为每个topContent
ListView 设置一个新的侦听器(可能在适配器的 getView 内部)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;
};
};
}