我有一个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
所以,我有ScrollView一个孩子 - 一个LinearLayout有两个孩子:a TextView和a ViewPager.ViewPager包含许多元素的布局,这就是为什么我需要垂直滚动的能力.只有页面ViewPager可以水平滚动(即:我只想在其中水平滑动ViewPager).那一个TextView不能水平滚动,但应该与我一起滚动ViewPager.
简单?没有.
我在StackOverflow中看到了非常类似的问题(这里,这里,这里和这里).所有建议的解决方案都不适合我:(
我看到的是这个 < - 我的甜蜜UI :),但我无法垂直滚动:(
在ViewPager中嵌入ScrollViews不是一个选项 - UI的设计禁止这样做.
也许这是我用编程方式填充视图寻呼机中的每个页面的东西?嗯...
任何建议,将不胜感激.
我的代码:
activity_main.xml中:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/linearLayoutGeneral"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvText"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:text="Test text" />
<android.support.v4.view.ViewPager
android:id="@+android:id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
ViewPager中的每个页面都有这样的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" …Run Code Online (Sandbox Code Playgroud) android android-fragments android-scrollview android-viewpager
我使用Android兼容包版本4在我的应用程序中显示pdf页面.我使用PagerAdapter和ViewPager来显示水平滚动视图等pdf页面.
现在的问题在于分页相关的东西.我可以通过检测viewpager中的子进程根据这个线程android:ViewPager和HorizontalScrollVIew来停止分页,但是当用户触摸该视图之外时,我该如何才能启用它.我使用了以下CodeViewPager代码.
public class CustomViewPager extends ViewPager {
private boolean enabled;
private int childId;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
public void setChildId(int childId) {
this.childId = childId;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
boolean result = false;
View scroll = getChildAt(childId);
if (scroll != null) {
Rect rect = new Rect();
CommonLogic.logMessage("PDF Page Rectangle ", TAG, Log.VERBOSE);
scroll.getHitRect(rect);
if (rect.contains((int) event.getX(), (int) event.getY())) {
setPagingEnabled(false);
result = true;
}
} …Run Code Online (Sandbox Code Playgroud) 是否可以在EditText中滚动ScrollView?
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- scroll inside this EditText -->
<EditText android:id="@+id/et_scrollhere"
android:lines="6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Voorbeeld"
android:layout_margin="5dp"
android:scrollbars="vertical"
android:inputType="textMultiLine"/>
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
或者是否可以在纵向模式下将EditText嵌入键盘?像WhatsApp这样的东西,EditText是可滚动的.
我对Android开发很新,目前我正在使用viewpager,试图了解我如何处理触摸/点击事件.我整个上午都在寻找这个问题的答案,我似乎无法找到任何可以帮助我的东西.
基本上我有一个带有5个片段的viewpager,它们都使用相同的布局.我想要做的就是能够检测静止视图上何时有敲击/触摸(我不希望在拖动视图时发生任何检测).
从我收集的内容来看,ViewPager默认拦截所有触摸事件似乎存在问题.我相信这可以通过使用:
myPager.requestDisallowInterceptTouchEvent(true);
Run Code Online (Sandbox Code Playgroud)
起初我涉足OnTouchListener,并尝试记录onTouch方法是否被击中 - 它似乎没有.我也看到GestureDetector类作为这个问题的解决方案被抛出很多但坦率地说我感到不知所措,我不确定我应该在这里使用哪些类/接口.
我想做的就是在使用ViewPager时选择简单的触摸/点击事件.
目前我正在使用Android开发者网站的动画教程代码的修改版本.这是我在MainActivity.java文件中的内容:
package com.example.pbinteractive;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 5;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* …Run Code Online (Sandbox Code Playgroud) 当触摸事件来自嵌套视图(例如 HorizontalScrollView 等)时,如何防止在androidx ViewPager2 中滑动?
我希望通过覆盖可以实现这一点,ViewPager2.onInterceptTouchEvent()但是由于 ViewPager2final不能被子类化。
现有的解决方案(见下表)处理与旧ViewPager它可以被继承。
正如我尝试过的其中一个答案中所建议的那样:
private ViewPager2 viewPager2;
...
HorizontalScrollView nestedScrollView = pagerView.findViewById(R.id.scrollview);
nestedScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
viewPager2.requestDisallowInterceptTouchEvent(true);
return false;
}
});
// nestedScrollView - the HorizontalScrollView inside a ViewPager2 item
// viewPager2 - the ViewPager2 instance
Run Code Online (Sandbox Code Playgroud)
但这似乎没有任何效果。
ViewPager2 具有从 ViewGroup 继承的onInterceptTouchEvent()但我没有看到在最终类上覆盖它的方法。
重新实现整个 ViewPager2 似乎不是一个明智的解决方案——原始类有 1607 行长,而且它使用的类不是由包导出的。
我看过的其他类似问题:
我需要设计以下屏幕,我需要你的建议:

说明:
标题是静态/固定的,我不需要对它做任何事情.
黄色:这是有趣的部分,我需要设计一个ViewPager类似的屏幕,能够向左/右滚动最多4个屏幕.
红色:在每个屏幕中,我需要添加一个可滚动的表格/网格,如果它不适合屏幕大小.
绿色:可以使用屏幕底部的绿色按钮或通过滚动来完成页面切换ViewPager.
问题是:这种行为可以使用ViewPager或者我应该使用Fragments吗?如果Fragment是要走的路,那么我将如何使用滑动手势实现页面切换?如果它是一个ViewPager然后如何添加内部滚动以及如何使用底部的按钮控制它?
任何帮助将不胜感激,谢谢.
我有一个基于Android-ViewPagerIndicator的应用程序.
我想创建HorizontalScrollView一个Fragment.视图将包含一些图片.我的问题是,当我尝试滚动时,我的滚动视图不起作用,我滚动所有片段而不仅仅是我想要的视图.希望我的问题是可以理解的!:)
这是代码的一部分:
.XML
<HorizontalScrollView android:id="@+id/horizontalScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:fadingEdgeLength="10dp">
<LinearLayout
android:id="@+id/harokLayout"
android:background="#EEEEEE"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:src="@drawable/app2" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app1" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app2" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app1" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app2" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app1" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app2" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app1" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app2" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app1" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app2" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="@drawable/app1" android:layout_height="wrap_content" …Run Code Online (Sandbox Code Playgroud) android android-ui android-layout android-fragments android-viewpager
我的问题很简单.我可以在DrawerLayout的内容菜单中使用HorizontalScrollView吗?我的DrawerLayout看起来像这样:
<android.support.v4.widget.DrawerLayout
android:id="@+id/pnlMenu"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Main content view -->
<ListView
android:id="@+id/lst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:fastScrollEnabled="true"
android:focusable="true"
android:focusableInTouchMode="true"
tools:listitem="@layout/item_layout" >
</ListView>
<!-- Content of menu -->
<FrameLayout
android:id="@+id/drawerFrame"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clickable="true"
android:background="@color/black" >
<fragment
android:id="@+id/frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.test.TestFragment" />
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
在片段内部我有HorizontalScrollView但是当我尝试触摸它时没有任何事情发生,因为抽屉布局跟随我的手指.我认为禁用内容菜单中的触摸事件并仅在单击主内容视图时使DrawerLayout可关闭将解决我的问题.真的吗?如果没有,有人可以告诉我,我该怎么办?
谢谢.