[英]Android : ViewPager in ScrollView not scrolling if item exceed in ViewPager
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_first_time_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.vikas.donna.ui.activities.FirstTimeProfileActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="284dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_rectangle_fill_gradient_primary_white">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_first_time_profile"
android:layout_width="match_parent"
android:layout_height="40dp" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iv_profile_image"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_below="@id/toolbar_first_time_profile"
android:layout_centerHorizontal="true"
android:src="@drawable/userpic" />
<com.vikas.donna.customcomponents.customfonts.CustomTextView
android:id="@+id/tv_profile_name"
style="@style/font_work_sans_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_profile_image"
android:layout_centerHorizontal="true"
android:text="Vikas Rohilla"
android:textColor="@android:color/white"
android:textSize="22sp" />
<com.vikas.donna.customcomponents.customfonts.CustomTextView
android:id="@+id/tv_tag_line"
style="@style/font_work_sans_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_profile_name"
android:layout_centerHorizontal="true"
android:alpha="0.80"
android:text="I am software engineer"
android:textColor="@android:color/white"
android:textSize="11sp" />
<com.vikas.donna.customcomponents.customfonts.CustomTextView
android:id="@+id/customTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_tag_line"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:background="@drawable/shape_rectangle_fill_primary_trans"
android:drawableLeft="@drawable/red_dot"
android:drawablePadding="9dp"
android:paddingBottom="5dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingTop="5dp"
android:text="Busy in meeting till 5.00 pm"
android:textColor="@android:color/white"
android:textSize="12sp" />
</RelativeLayout>
</FrameLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_profile"
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabIndicatorColor="@android:color/transparent"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
如果视图分页器中的片段包含长列表,则它将不会滚动。 即使在添加Scrollview之后,它仍显示静止屏幕。 如果在ViewPager子片段中添加Scrollview而不是活动,那么它将仅滚动片段部分,而不滚动部分。 我现在应该怎么办
public class CustomScrollView extends ScrollView {
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, new YScrollDetector());
setFadingEdgeLength(0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev)
&& mGestureDetector.onTouchEvent(ev);
}
// Return false if we're scrolling in the x direction
class YScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
if (Math.abs(distanceY) > Math.abs(distanceX)) {
return true;
}
return false;
}
}
}
public class NestingViewPager extends ViewPager {
public NestingViewPager(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public NestingViewPager(final Context context) {
super(context);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v != this && v instanceof ViewPager) {
return true;
}
return super.canScroll(v, checkV, dx, x, y);
}
}
据我ViewPager
,您已经将水平ViewPager
放在垂直ScrollView
内,这本身就是一个有问题的安排:手势是要滚动ViewPager
还是ScrollView
? ViewPager
的Fragment
的ListView
使问题更加复杂:当用户触摸列表时,焦点应该放在ListView
还是ScrollView
?
您可以尝试以下方法之一:
1.尝试使用RecyclerView
和NestedScrollView
代替ListView
和ScrollView
。
2.或者,定义一个interface
,该interface
在被触摸时将焦点锁定在ListView
(反之亦然):
public interface FocusLock {
void lock();
void unlock();
}
然后创建一个自定义ListView
并重写其dispatchTouchEvent()
方法,如下所示:
public class NestedListView extends ListView{
private FocusLock mCallbackControl;
public void setCallback(FocusLock callbackControl) {
this.mCallbackControl = callbackControl;
}
....
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
mCallbackControl.lock();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mCallbackControl.unlock();
break;
}
return super.dispatchTouchEvent(event);
}
}
在Fragment
,必须按如下所示初始化ListView
:
NestedListView mNestedListView = findViewById(R.id.list);
mNestedListView.setCallback(new FocusLock() {
@Override
public void lock() {
mScrollView.requestDisallowInterceptTouchEvent(true);
}
@Override
public void unlock() {
mScrollView.requestDisallowInterceptTouchEvent(false);
}
});
希望这是有道理的。
尝试这个。 这些方法之一应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.