SwipeRefreshLayout内ScrollView内的ViewPager [英] ViewPager inside ScrollView inside SwipeRefreshLayout
问题描述
我有一个包含SwipeRefreshLayout,ScrollView和ViewPager的布局.
I have a layout which includes a SwipeRefreshLayout, a ScrollView and a ViewPager.
使用SwipeRefreshLayout刷新ViewPager和ScrollView是因为ViewPager可能包含不适合显示的内容.
The SwipeRefreshLayout is used to refresh the ViewPager and the ScrollView is because the ViewPager can have content that don't fits on display.
当我尝试向下滚动时,什么也没有发生,但是我可以看到还有更多内容要显示.当我向上滚动时,将调用SwipeRefresh.
When I try to scroll down nothing happens but I can see there is more content to show. When I scroll up the SwipeRefresh is invoked.
我的XML:
<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>
它的外观屏幕截图(最后一个紫色框在显示之外): http://i.stack.imgur.com/hlrdz.png
Screenshot of how it look like (the last purple box is outside of display): http://i.stack.imgur.com/hlrdz.png
经过一些研究,我发现问题是由于ScrollView不知道ViewPager中最大项目的高度而引起的.我正在寻找一种动态更改ViewPager高度的方法.
After some research I have found out that the problem is because of the fact that the ScrollView don't know the height of the largest item in the ViewPager. I'm searching for a way to change the height of the ViewPager dynamically.
推荐答案
经过一番思考,编写和测试,我找到了一个可行的解决方案. 我在ViewPager的每个页面中都放置了扩展的ScrollView,然后在ScrollView中添加了带有Context和SwipeRefreshLayout的构造函数.
After some thinking, writing and testing I have found a solution that works. I put a extended ScrollView in every page of the ViewPager and then in the ScrollView I added a constructor with Context and SwipeRefreshLayout.
我重写了onScrollChanged方法,并检查ScrollView是否滚动到顶部.如果不是,我禁用了SwipeRefreshLayout.
I overridden the method onScrollChanged and checked if the ScrollView was scrolled to the top. If it was not, I disabled the 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>
ScrollView.class:
ScrollView.class:
public class ScrollView extends android.widget.ScrollView {
SwipeRefreshLayout swipeRefresh;
public ScrollView(Context context, SwipeRefreshLayout swipeRefresh) {
super(context);
this.swipeRefresh = swipeRefresh;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
Log.i("School", "T: " + t);
if (t != 0) {
swipeRefresh.setEnabled(false);
} else if (t == 0) {
swipeRefresh.setEnabled(true);
}
}
}
ViewPagerAdapter InstantiateItem方法:
ViewPagerAdapter instantiateItem method:
@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);
,是因为存在以下问题:当用户向下滚动然后滑动到另一个页面时,swipeRefresh被禁用.
The reason why I call swipeRefresh.setEnabled(true);
is because of the problem that when the user scrolls down and then swipes to another page the swipeRefresh was disabled.
希望有人会发现它有用
这篇关于SwipeRefreshLayout内ScrollView内的ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!