我有一个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
我正在尝试使用一个ViewPager
内部ScrollView
,但ViewPager
不会出现.如果我删除ScrollView
了ViewPager
显示正常.
我用以下方法创建了一个简单的测试项目:
main.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewpager" />
</ScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
活动类:
public class ScrollViewWithViewPagerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(new MyPagerAdapter(this));
}
}
class MyPagerAdapter extends PagerAdapter {
private Context ctx;
public MyPagerAdapter(Context context) {
ctx = context;
}
@Override
public int getCount() {
return 2;
}
@Override …
Run Code Online (Sandbox Code Playgroud) 我需要有一个ViewPager
内部a ScrollView
但是ViewPager
当它进入时不会出现ScrollView
,当我不使用时一切都好ScrollView
.
我已经在stackoverflow或其他网站上看到过这样的问题,所有这些问题都得到了解答,你必须把它们放到android:fillViewport="true"
你的滚动视图来修复问题,但是这个解决方案对我来说ViewPager
不起作用,仍然没有出现即使我有android:fillViewport="true"
我的ScrollView
.
我想在android api中有些东西发生了变化,这个解决方案不再适用了,有没有人知道我怎么可能ViewPager
出现在一个ScrollView
?
更新:一个有效的ScrollView布局XML:
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/itemsViewPager2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud) 所以,我有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
我fragmentPagerAdapter viewpager
里面有几个片段类.因此,这viewpager
可以通过滑动水平向左和向右滚动.问题是,在每个页面上,我的值超出了页面大小,我需要垂直滚动才能看到它们.但我无法启用任何垂直滚动.我删除了所有scrollview
的东西,让我的代码进入工作状态.(我想要像Android应用程序商店那样:水平滚动分类,垂直滚动查看应用程序.)
这是我的代码:
MyFragmentPagerAdapter:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
需要垂直滚动的片段活动:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:text="Last fill-up spent :"
android:textSize="15dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp"
android:text="Month total spent :"
android:textSize="15dp" />
<TextView
android:id="@+id/currentMoneySpentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/textView1"
android:textSize="15dp" />
<TextView
android:id="@+id/monthTotalSpentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" …
Run Code Online (Sandbox Code Playgroud)