Android ScrollView嵌套ViewPager不显示和出现空白部分 解决方法

上效果图

device-2020-06-08-174856

有3种解决方法先大概说一下:

     1.设置ScrollView的fillViewPort为true。然后给viewpager设置固定的高度值。(局限性很大,ScrollView不能跟着一起滑动了,只有viewpager能)。

     2.网上还有一种方法,重写viewpager,但是会留下空白,不留空白的会发现很卡。(亲测)https://blog.csdn.net/qq_27400335/article/details/81116982

     3.自己捣鼓的一套方法,既不留空白,也不会卡。只是转变了一下思路。先说一下,下面帖代码,这种方式也可以解决tablayout的吸顶。  

        这种方法去掉了ScrollView,然后转用了谷歌给我们提供的控件,

CoordinatorLayout  
AppBarLayout
CollapsingToolbarLayout

       通过这三个组合使用,然后再把自己的viewpager拿出来,像往常一样使用即可。既不会出现问题,也不难以理解,倒还可以增加一些动画效果。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite"
       >
       
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <LinearLayout
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            这里面是设置自己的头部,根据自己的来进行设置
           </LinearLayout>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
           这里是tabLayout。
            <com.qmuiteam.qmui.widget.QMUITabSegment
                android:id="@+id/tabLayout"
                android:layout_marginTop="30dp"
                android:background="@color/colorWhite"
                android:layout_width="match_parent"
                android:layout_height="40dp"/>
    </com.google.android.material.appbar.AppBarLayout>
            <androidx.viewpager.widget.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

,如有不懂欢迎底部留言

已标记关键词 清除标记
应该已经用fragmentPagerAdapter设置好了fragment,可是viewpager set了adapter后,什么都没有。 先上图: ![图片说明](https://img-ask.csdn.net/upload/201603/07/1457362917_372384.png) ![图片说明](https://img-ask.csdn.net/upload/201603/07/1457362927_190250.png) 下面是content_main,在其中添加了viewpager的组件 ``` <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:showIn="@layout/activity_main" tools:context=".MainActivity" android:id="@+id/drawerlayout"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"/> <ListView android:id="@+id/listview" android:layout_width="240dp" android:layout_height="match_parent" android:background="#ff6666" android:layout_gravity="left"> </ListView> </android.support.v4.widget.DrawerLayout> ``` 下面是FragmentPagerAdapter: ``` public class MyFragmentPagerAdapter extends FragmentPagerAdapter { List<Fragment> list; String[] titles; public Fragment getItem(int position) { return list.get(position); } public int getCount() { return list.size(); } MyFragmentPagerAdapter(FragmentManager fm,List<Fragment> list,String[] titles) { super(fm); this.list=list; this.titles=titles; } public CharSequence getPageTitle(int position) { return titles[position]; } } ``` 下面是mainActivity中的与viewpager有关的部分: ``` List<Fragment> listFragment=new ArrayList<Fragment>(); listFragment.add(f1); listFragment.add(f2); listFragment.add(f3); listFragment.add(f4); listFragment.add(f5); viewPager=(ViewPager)super.findViewById(R.id.viewpager); fragmentPagerAdapter=new MyFragmentPagerAdapter(getSupportFragmentManager(),listFragment,new String[]{"标签1","标签2","标签3","标签4","标签5"}); viewPager.setAdapter(fragmentPagerAdapter); tabLayout=(TabLayout)super.findViewById(R.id.tablayout); for(int i=0;i<5;i++) { tabLayout.addTab(tabLayout.newTab()); } tabLayout.setTabMode(TabLayout.MODE_FIXED); tabLayout.setupWithViewPager(viewPager); ``` 下面是填充viewpager第一页的fragment1的代码: ``` public class Fragment1 extends Fragment { RecyclerView recyclerView; MyAdapter myAdapter; public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanced) { View v=inflater.inflate(R.layout.fragment1,container,false); myAdapter=new MyAdapter(getActivity()); recyclerView=(RecyclerView)v.findViewById(R.id.recyclerview); LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity()); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(myAdapter); return v; } } ``` 从上图已可见,这部分内容是不显示的,以下是viewpager第二页的fragment2: ``` public class Fragment2 extends Fragment { public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanced) { View v=inflater.inflate(R.layout.fragment2,container,false); return v; } } ``` 后面的fragment与此fragment类似,就不贴了。viewpager中什么都没有,但是从上面MainActivity中的setupWithViewPager的tabLayout就可以获取到标题。 请问应该怎么解决
如图所示:![图片说明](https://img-ask.csdn.net/upload/201704/24/1492995631_318407.png) 我在ScrollView里面嵌套了可以左右滑动的Viewpagerviewpager里面是上下滑动的 RecycleView,目前发现的问题是我给RecycleVIew设置的加载头显示不出来,最后一行也是显示不完全。感觉是滑动冲突的原因,但是网上没有系统的解决问题办法。请大家给点切实可行的解决办法吧 下面附上我的xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/rl_map_all" android:layout_width="match_parent" android:layout_height="match_parent"> <!--底部图层--> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!--底部图层--> <include layout="@layout/fragment_map" /> <!--右侧侧滑栏--> <include layout="@layout/map_slide_right" /> </android.support.v4.widget.DrawerLayout> <com.yinglan.scrolllayout.ScrollLayout android:id="@+id/scroll_down_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00550033" android:visibility="gone" app:allowHorizontalScroll="true" app:exitOffset="0dp" app:isSupportExit="true" app:maxOffset="300dp" app:minOffset="50dp" app:mode="open"> <!--多条记录悬浮--> <com.yinglan.scrolllayout.content.ContentScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/dimen1800" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical"> <com.example.administrator.im_demo.ui.view.navigationbar.indicators.MagicIndicator android:id="@+id/magic_indicator" android:layout_width="match_parent" android:layout_height="@dimen/dimen120" android:background="@color/tab_title" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager_search_result" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <TextView android:id="@+id/tv_next" android:layout_width="match_parent" android:layout_height="@dimen/dimen110" android:background="@color/white" android:clickable="true" android:gravity="center" android:text="总共XX条记录" android:textColor="@color/white" android:textSize="@dimen/dimen32" android:visibility="gone" /> </RelativeLayout> </com.yinglan.scrolllayout.content.ContentScrollView> </com.yinglan.scrolllayout.ScrollLayout> <android.support.v7.widget.Toolbar android:id="@+id/toolbar_mapall" android:layout_width="match_parent" android:layout_height="50dp" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" android:titleTextColor="@android:color/white" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/ThemeOverlay.AppCompat.ActionBar" android:visibility="gone"/> </RelativeLayout> ``` ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/white"> <com.wan7451.wanadapter.mylibrary.WanRecycleView android:id="@+id/recycleview_float_company" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> ``` ``` 下面是几个引用的view: /* * * * sufly0001@gmail.com Modify the code to enhance the ease of use. * * * * Copyright (C) 2015 Ted xiong-wei@hotmail.com * * 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.yinglan.scrolllayout.content; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.ViewParent; import android.widget.ScrollView; import com.yinglan.scrolllayout.ScrollLayout; public class ContentScrollView extends ScrollView { public interface OnScrollChangedListener { void onScrollChanged(int l, int t, int oldl, int oldt); } private OnScrollChangedListener listener; public ContentScrollView(Context context) { super(context); } public ContentScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ContentScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setOnScrollChangeListener(OnScrollChangedListener listener) { this.listener = listener; } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); listener.onScrollChanged(l, t, oldl, oldt); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); ViewParent parent = this.getParent(); while (parent != null) { if (parent instanceof ScrollLayout) { ((ScrollLayout) parent).setAssociatedScrollView(this); break; } parent = parent.getParent(); } } @Override public boolean onTouchEvent(MotionEvent ev) { ViewParent parent = this.getParent(); if (parent instanceof ScrollLayout) { if (((ScrollLayout) parent).getCurrentStatus() == ScrollLayout.Status.OPENED) return false; } return super.onTouchEvent(ev); } } ``` ``` package com.wan7451.wanadapter.mylibrary; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import com.handmark.pulltorefresh.library.PullToRefreshBase; /** * Created by Hello on 2015/6/30. */ public class WanRecycleView extends PullToRefreshBase<RecyclerView> { public WanRecycleView(Context context) { super(context); } public WanRecycleView(Context context, AttributeSet attrs) { super(context, attrs); } public WanRecycleView(Context context, Mode mode) { super(context, mode); } public WanRecycleView(Context context, Mode mode, AnimationStyle animStyle) { super(context, mode, animStyle); } @Override public Orientation getPullToRefreshScrollDirection() { return Orientation.VERTICAL; } @Override protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) { RecyclerView view = new RecyclerView(context, attrs); view.setId(R.id.recycleView); return view; } @Override protected boolean isReadyForPullEnd() { int lastVisiblePosition = getRefreshableView().getChildAdapterPosition(getRefreshableView().getChildAt(getRefreshableView().getChildCount() -1)); if (lastVisiblePosition >= getRefreshableView().getAdapter().getItemCount()-1) { return getRefreshableView().getChildAt(getRefreshableView().getChildCount() - 1).getBottom() <= getRefreshableView().getBottom(); } return false; } @Override protected boolean isReadyForPullStart() { if (getRefreshableView().getChildCount() <= 0) return true; int firstVisiblePosition = getRefreshableView().getChildAdapterPosition(getRefreshableView().getChildAt(0)); if (firstVisiblePosition == 0) return getRefreshableView().getChildAt(0).getTop() == getRefreshableView().getPaddingTop(); else return false; } } ``` ```
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页