在Fragment中的RecyclerView数据增加后,android的ViewPager高度发生了变化

时间:2017-02-23 14:30:42

标签: android android-viewpager android-recyclerview

我在ViewPager的片段中使用了RecyclerView,但是在RecyclerView的数据增加之后出现了问题,ViewPager向上移动了。
最后,我通过外部使用ScrollView解决了这个问题,并在ScrollView中设置了set android:fillViewport =“true”。

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<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_group_purchase"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="sizu.mingteng.com.yimeixuan.main.home.others.grouppurchase.activity.GroupPurchaseActivity">
    <!--工具栏-->
    <include layout="@layout/toolbar_group_purchase" />

    <com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
        android:id="@+id/refresh_layout_home_group_purchase"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:tr_enable_loadmore="false"
        app:tr_head_height="120dp"
        app:tr_overscroll_bottom_show="false"
        app:tr_overscroll_top_show="false"
        app:tr_wave_height="120dp">


        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar_group_purchase"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <!--广告栏-->
                <android.support.design.widget.CollapsingToolbarLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_scrollFlags="scroll">

                    <com.youth.banner.Banner
                        android:id="@+id/banner_home_group_purchase"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/banner_height" />
                </android.support.design.widget.CollapsingToolbarLayout>
                <!--类别Tab-->
                <include layout="@layout/layout_tab_more" />
            </android.support.design.widget.AppBarLayout>

            <!--分类视图-->
            <android.support.v4.view.ViewPager
                android:id="@+id/vp_home_group_purchase"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </android.support.design.widget.CoordinatorLayout>


    </com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout>

</LinearLayout>

在这里我初始化了我的ViewPager

private void setTabLayoutAndViewPager() {
    mTabPagerAdapter = new GroupPurchaseHomeTabPagerAdapter(getSupportFragmentManager(), mFragments, mTabs);
    vpHomeGroupPurchase.setAdapter(mTabPagerAdapter);
    mCurrentItem = vpHomeGroupPurchase.getCurrentItem();
    tabLayout.setupWithViewPager(vpHomeGroupPurchase);
}

1 个答案:

答案 0 :(得分:0)

尝试调用此方法:

yourViewPager.getAdapter().notifyDataSetChanged();

更多信息: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyDataSetChanged()

抱歉,我无法添加评论。

更新:

    private void setTabLayoutAndViewPager() {
        mTabPagerAdapter = new GroupPurchaseHomeTabPagerAdapter(getSupportFragmentManager(), mFragments, mTabs);
        mTabPagerAdapter.notifyDataSetChanged();
        vpHomeGroupPurchase.setAdapter(mTabPagerAdapter);
        mCurrentItem = vpHomeGroupPurchase.getCurrentItem();
        tabLayout.setupWithViewPager(vpHomeGroupPurchase);
    }

试试这个。 如果它现在将解决问题,请说明如何向适配器添加数据。

糟糕!应用它。

     private void setTabLayoutAndViewPager() {
                    mTabPagerAdapter = new GroupPurchaseHomeTabPagerAdapter(getSupportFragmentManager(), mFragments, mTabs);
                    if (vpHomeGroupPurchase.getAdapter() != null) {
                        vpHomeGroupPurchase.getAdapter().notifyDataSetChanged();
                    }
                    vpHomeGroupPurchase.setAdapter(mTabPagerAdapter);
                    mCurrentItem = vpHomeGroupPurchase.getCurrentItem();
                    tabLayout.setupWithViewPager(vpHomeGroupPurchase);
                }