我有一个页面",上面有许多组件,并且谁的内容比设备的高度长.好吧,把所有的布局(整个页面)放在一个ScrollView
里面,没问题.
I have a 'page' that has a number of components on it, and who's content is longer than the height of the device. Fine, just put all of the layout (the entire page) inside a ScrollView
, no problem.
其中一个组件是 ViewPager
.这可以正确呈现,但是对滑动/甩动的响应没有正确执行,它很紧张并且并不总是有效.它似乎对 ScrollView
感到困惑",所以只有当你在一条精确的水平线上投掷时才能 100% 工作.
One of the components is a ViewPager
. This renders correctly, but the response to a swipe/fling is not performing correctly, it is jittery and doesn't always work. It seems to be getting 'confused' with the ScrollView
, so only works 100% when you fling in an exact horizontal line.
如果我删除 ScrollView
,ViewPager 会完美响应.
If I remove the ScrollView
, the ViewPager responds perfectly.
我已经四处搜索,并没有发现这是一个已知的缺陷.有其他人经历过吗?
I've had a search around and have not found this as a known defect. Has anyone else experienced this?
下面是一些示例代码供您测试,注释掉 ScrollView
以查看它是否正常工作.
Below is some example code for you to test with, comment out ScrollView
to see it working correctly.
活动:
package com.ss.activities;
import com.ss.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
public class PagerInsideScollViewActivity 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
public Object instantiateItem(View collection, int position) {
TextView tv = new TextView(ctx);
tv.setTextSize(50);
tv.setTextColor(Color.WHITE);
tv.setText("SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, " +
"SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, " +
"SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, " +
"SMILE DUDE, SMILE DUDE, SMILE DUDE");
((ViewPager) collection).addView(tv);
return tv;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public void startUpdate(View arg0) {
}
@Override
public void finishUpdate(View arg0) {
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="300dp" />
</LinearLayout>
</ScrollView>
进一步阅读发现滚动组件内部存在滚动组件问题.
Further reading has revealed that there are issues with scrolling components inside scrolling components.
一种解决方案是在包含的可滚动组件(在我的例子中是 ViewPager)区域禁用"ScrollView 的垂直滚动.
One solution is to 'disable' the vertical scrolling of the ScrollView on the area of the contained scrollable component, in my case a ViewPager.
此解决方案的代码在这里有详细说明(它简直太棒了!)
The code for this solution is detailed here (and its simply brilliant!)
这篇关于ScrollView 中的 ViewPager 无法正确滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!