[英]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.