Android ViewPager内部滚动视图内部滚动布局
我有一个布局,其中包括SwipeRefreshLayout、ScrollView和ViewPager SwipeRefreshLayout用于刷新ViewPager,而ScrollView是因为ViewPager可能有不适合显示的内容 当我尝试向下滚动时,什么也没有发生,但我可以看到有更多的内容要显示。当我向上滚动时,将调用SwiperFresh 我的XML:Android ViewPager内部滚动视图内部滚动布局,android,android-layout,android-viewpager,android-scrollview,swiperefreshlayout,Android,Android Layout,Android Viewpager,Android Scrollview,Swiperefreshlayout,我有一个布局,其中包括SwipeRefreshLayout、ScrollView和ViewPager SwipeRefreshLayout用于刷新ViewPager,而ScrollView是因为ViewPager可能有不适合显示的内容 当我尝试向下滚动时,什么也没有发生,但我可以看到有更多的内容要显示。当我向上滚动时,将调用SwiperFresh 我的XML: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
style="@style/TabLayout"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingStart="16dp" />
<com.gmail.bathingrad.school.widget.SwipeRefreshView
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.gmail.bathingrad.school.widget.SwipeRefreshView>
</LinearLayout>
编辑:
经过一些研究,我发现问题是因为ScrollView不知道ViewPager中最大项目的高度。我正在寻找一种动态更改ViewPager高度的方法 经过思考、写作和测试,我找到了一个有效的解决方案。
我在ViewPager的每个页面中都添加了一个扩展的ScrollView,然后在ScrollView中添加了一个具有上下文和布局的构造函数
<com.gmail.bathingrad.school.widget.SwipeRefreshView
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</com.gmail.bathingrad.school.widget.SwipeRefreshView>
我重写了onScrollChanged方法,并检查ScrollView是否已滚动到顶部。如果不是,我禁用了SwipeRefreshLayout
<com.gmail.bathingrad.school.widget.SwipeRefreshView
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</com.gmail.bathingrad.school.widget.SwipeRefreshView>
ViewPagerAdapter实例化项方法:
@Override
public Object instantiateItem(ViewGroup container, int position) {
LinearLayout[] schemeLayout = readScheme.generateScheme();
ScrollView[] scrollView = new ScrollView[5];
// SwipeRefreshLayout
SwipeRefreshLayout swipeRefresh = (SwipeRefreshLayout) container.getParent();
swipeRefresh.setEnabled(true);
scrollView[position] = new ScrollView(context, swipeRefresh);
scrollView[position].addView(schemeLayout[position]);
container.addView(scrollView[position]);
return scrollView[position];
}
这管用
调用swipeRefresh.setEnabled的原因(true)
是因为当用户向下滚动然后滑动到另一个页面时,SwiperFresh被禁用
希望有人会发现这一点很有用谷歌通过发布在谷歌示例github projet上的示例来解决这个问题
创建扩展原始布局的视图:
/*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.swiperefreshmultipleviews;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
/**
* A descendant of {@link android.support.v4.widget.SwipeRefreshLayout} which supports multiple
* child views triggering a refresh gesture. You set the views which can trigger the gesture via
* {@link #setSwipeableChildren(int...)}, providing it the child ids.
*/
public class MultiSwipeRefreshLayout extends SwipeRefreshLayout {
private View[] mSwipeableChildren;
public MultiSwipeRefreshLayout(Context context) {
super(context);
}
public MultiSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Set the children which can trigger a refresh by swiping down when they are visible. These
* views need to be a descendant of this view.
*/
public void setSwipeableChildren(final int... ids) {
assert ids != null;
// Iterate through the ids and find the Views
mSwipeableChildren = new View[ids.length];
for (int i = 0; i < ids.length; i++) {
mSwipeableChildren[i] = findViewById(ids[i]);
}
}
// BEGIN_INCLUDE(can_child_scroll_up)
/**
* This method controls when the swipe-to-refresh gesture is triggered. By returning false here
* we are signifying that the view is in a state where a refresh gesture can start.
*
* <p>As {@link android.support.v4.widget.SwipeRefreshLayout} only supports one direct child by
* default, we need to manually iterate through our swipeable children to see if any are in a
* state to trigger the gesture. If so we return false to start the gesture.
*/
@Override
public boolean canChildScrollUp() {
if (mSwipeableChildren != null && mSwipeableChildren.length > 0) {
// Iterate through the scrollable children and check if any of them can not scroll up
for (View view : mSwipeableChildren) {
if (view != null && view.isShown() && !canViewScrollUp(view)) {
// If the view is shown, and can not scroll upwards, return false and start the
// gesture.
return false;
}
}
}
return true;
}
// END_INCLUDE(can_child_scroll_up)
// BEGIN_INCLUDE(can_view_scroll_up)
/**
* Utility method to check whether a {@link View} can scroll up from it's current position.
* Handles platform version differences, providing backwards compatible functionality where
* needed.
*/
private static boolean canViewScrollUp(View view) {
if (android.os.Build.VERSION.SDK_INT >= 14) {
// For ICS and above we can call canScrollVertically() to determine this
return ViewCompat.canScrollVertically(view, -1);
} else {
if (view instanceof AbsListView) {
// Pre-ICS we need to manually check the first visible item and the child view's top
// value
final AbsListView listView = (AbsListView) view;
return listView.getChildCount() > 0 &&
(listView.getFirstVisiblePosition() > 0
|| listView.getChildAt(0).getTop() < listView.getPaddingTop());
} else {
// For all other view types we just check the getScrollY() value
return view.getScrollY() > 0;
}
}
}
// END_INCLUDE(can_view_scroll_up)
}
android.R.id.list和android.R.id.empty是列表或回收器视图的id。该视图与具有相同ID的两个列表完美配合
你可以在网站上看到真实的例子
<com.example.android.swiperefreshmultipleviews.MultiSwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- your code -->
</com.example.android.swiperefreshmultipleviews.MultiSwipeRefreshLayout>
mSwipeRefreshLayout = (MultiSwipeRefreshLayout) view.findViewById(R.id.swiperefresh);
mSwipeRefreshLayout.setSwipeableChildren(android.R.id.list, android.R.id.empty);