我有一个ScrollView环绕我的整个布局,以便整个屏幕可滚动.我在这个ScrollView中的第一个元素是一个HorizontalScrollView块,它具有可以水平滚动的功能.我在horizontalscrollview中添加了一个ontouchlistener来处理触摸事件并强制视图"捕捉"到ACTION_UP事件上最近的图像.
所以我想要的效果就像股票android主屏幕,你可以从一个滚动到另一个,当你举起手指时,它会捕捉到一个屏幕.
这一切都很有效,除了一个问题:我需要从左到右几乎完全水平滑动,以便ACTION_UP进行注册.如果我至少垂直滑动(我认为许多人往往会在他们的手机上左右滑动),我会收到ACTION_CANCEL而不是ACTION_UP.我的理论是,这是因为横向视图滚动视图位于滚动视图内,并且滚动视图正在劫持垂直触摸以允许垂直滚动.
如何在水平滚动视图中禁用滚动视图的触摸事件,但仍允许在滚动视图中的其他位置正常垂直滚动?
这是我的代码示例:
public class HomeFeatureLayout extends HorizontalScrollView {
private ArrayList<ListItem> items = null;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private static final int SWIPE_MIN_DISTANCE = 5;
private static final int SWIPE_THRESHOLD_VELOCITY = 300;
private int activeFeature = 0;
public HomeFeatureLayout(Context context, ArrayList<ListItem> items){
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setFadingEdgeLength(0);
this.setHorizontalScrollBarEnabled(false);
this.setVerticalScrollBarEnabled(false);
LinearLayout internalWrapper = new LinearLayout(context);
internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
addView(internalWrapper);
this.items = items;
for(int i = 0; i< items.size();i++){
LinearLayout featureLayout = (LinearLayout) View.inflate(this.getContext(),R.layout.homefeature,null);
TextView header = (TextView) …Run Code Online (Sandbox Code Playgroud) android horizontalscrollview ontouchlistener android-scrollview
我正在尝试使用包含ScrollViews的ViewFlipper创建布局.我们的想法是检测水平滑动以移动到上一个/下一个ScrollView.此外,ScrollView包含另一个ViewFlipper,其中包含带有垂直滑动检测器的ImageView,可以转到上一个/下一个ImageView.当我用LinearLayout替换ScrollView时,两个手势检测器都能正常工作,但是使用ScrollView时,没有任何工作(手势监听器甚至不是触发器).为什么使用ScrollView会禁用我的手势检测器?我怎样才能使它工作?
活动
public class ProduitHome extends Activity{
private Resources res;
float density;
private int position, parent_id;;
private int num_products;
private Produit produit;
private ImageDownloader mImageLoader;
private ViewFlipper product_viewflipper;
private ScrollView current_product_layout;
Animation next_product_out, next_product_in, previous_product_in, previous_product_out;
private GestureDetector galleryGestureDetector;
private View.OnTouchListener galleryGestureListener;
private GestureDetector productGestureDetector;
private View.OnTouchListener productGestureListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.produit_home);
num_products = GlobalData.map_list_produits.get(parent_id).size();
product_viewflipper = (ViewFlipper) findViewById(R.id.product_viewflipper);
LayoutInflater inflater = getLayoutInflater();
// Add num_products view to the viewflipper
for(int i=0; i<num_products; i++){
ScrollView product_detail …Run Code Online (Sandbox Code Playgroud)