所以,我有这个 ScrollView
和一个 child - 一个 LinearLayout
有两个 child :一个 TextView
和一个 ViewPager
。 ViewPager
包含许多元素的布局,这就是为什么我需要垂直滚动的能力。只有 ViewPager
中的页面可以水平滚动(即:我只想在 ViewPager
中水平滑动)。那个 TextView
不能水平滚动,但应该与我的 ViewPager
一起滚动。
简单?没有。
我在 StackOverflow 看到了非常相似的问题(here、here 和 here 和 here)。 建议的解决方案都不适合我:(
我看到的是this <- 我可爱的 UI :),但是我不能垂直滚动 :(
在 ViewPager 中嵌入 ScrollViews 不是一个选项 - UI 的设计禁止这样做。
也许这与我以编程方式填充 View 寻呼机中的每个页面有关?嗯……
如有任何建议,我们将不胜感激。
我的代码:
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>
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"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layoutData"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
这样一个页面中的单个元素的布局:
<?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="40dp"
android:gravity="center"
android:orientation="vertical"
android:background="@android:color/white" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:background="@android:color/black"
android:textColor="@android:color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
单页Fragment也很简单:
public class DayFragment extends Fragment {
private static final String TAG = DayFragment.class.getSimpleName();
public String tag;
LinearLayout data;
View mView;
final int ROWS_NUM = 60;
public DayFragment() {
}
/**
* (non-Javadoc)
*
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
mView = (LinearLayout) inflater.inflate(R.layout.day, container, false);
setUpControls();
generateData();
String text = getArguments().getString("text");
Log.d(TAG, "creating view with text: " + text);
return mView;
}
private void setUpControls() {
data = (LinearLayout) mView.findViewById(R.id.layoutData);
}
private void generateData() {
for (int i = 0; i < ROWS_NUM; i++) {
View v = createRow(i);
data.addView(v);
}
}
private View createRow(int num) {
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.row, null);
TextView tv = (TextView) v.findViewById(R.id.textView);
tv.setText("Data nr: " + num);
return v;
}
public static DayFragment newInstance(String text) {
Log.d(TAG, "newInstance with text: " + text);
DayFragment f = new DayFragment();
f.tag = text;
// Supply text input as an argument.
Bundle args = new Bundle();
args.putString("text", text);
f.setArguments(args);
return f;
}
}
最佳答案
我遇到了 ViewPager
行为怪异的问题,我发现这是因为有时 ScrollView
会重新获得焦点,而 ViewPager
然后失去焦点。如果您在 ScrollView
中有一个 ViewPager
并且您希望它在您触摸它时始终保持焦点并且 ScrollView
永远不会获得焦点,设置 requestDisallowInterceptTouchEvent
即可。这有帮助吗?
mViewPager= (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(adapter);
mViewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mViewPager.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
关于android - ScrollView 内的 ViewPager - 垂直滚动不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12669725/