我实现了一个画廊,在里面我有很多从左到右的列表视图.出于某种原因,Gallery适用于所有视图,但不适用于listview.使用listview,当在库中滚动时,有时我会跳得很少.
任何人都知道如何解决这个问题?
一些注意事项:图库使用适配器来查找要显示的内容,然后基于适配器创建列表视图
谢谢
我有类似的问题.问题是ListView拦截了您的Gallery中的触摸事件,并修改了处理ListView垂直滚动的代码块中的视图位置.如果只有Gallery首先拦截了触摸事件...我认为这是Android源代码中的一个错误,但与此同时,您可以通过继承Gallery并使用您的子类来修复非平滑滚动.这样就可以了:
public class BetterGallery extends Gallery {
private boolean scrollingHorizontally = false;
public BetterGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public BetterGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BetterGallery(Context context) {
super(context);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
super.onInterceptTouchEvent(ev);
return scrollingHorizontally;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
scrollingHorizontally = true;
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
scrollingHorizontally = false;
}
return super.onTouchEvent(event);
}
}
Run Code Online (Sandbox Code Playgroud)
另外,在实现库的活动中设置类似的内容:
ListView listView = (ListView) view.findViewById(R.id.users);
listView.setAdapter(userListAdapter);
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
galleryView.onTouchEvent(event);
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
最后,将onTouchEvent()添加到活动本身:
@Override
public boolean onTouchEvent(MotionEvent event) {
return galleryView.onTouchEvent(event);
}
Run Code Online (Sandbox Code Playgroud)
最后一点说明......在完全实现之后,我发现从可用性的角度来看,最好使用我称之为AdaptableViewAnimator的自定义类来扩展ViewAnimator,当然只需要一些适配器功能,并将ListView嵌入其中它.它不会像ListView-inside-Gallery一样浮动.