在Fragment中,使用CoordinatorLayout+TabLayout布局,会遇到recyclerview 给遮挡的问题,修改完成,效果图如下:
一.先上布局代码
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@color/white" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/sl_layout" > <android.support.design.widget.AppBarLayout android:id="@id/id_stickynavlayout_topview" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/APPTheme.AppBarOverlay" app:elevation="0.5dp" android:fitsSystemWindows="true"> <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="@dimen/title_bar" android:minHeight="@dimen/title_bar" android:theme="@style/MyToolbar" app:titleTextAppearance="@style/ToolbarTextAppearance.Title" app:layout_scrollFlags="scroll|enterAlways" > <TextView android:textColor="@color/white" android:id="@+id/textView_toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:ellipsize="end" android:singleLine="true" android:textSize="20sp" tools:text="首页" /> </android.support.v7.widget.Toolbar> <RelativeLayout android:background="@color/white" android:padding="12dp" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|enterAlways" > <ImageView android:id="@+id/iv_head" android:src="@mipmap/zh_touxiang" android:layout_width="80dp" android:layout_height="80dp" /> <TextView android:textColor="@color/text_title" android:paddingLeft="12dp" android:layout_centerVertical="true" android:layout_toRightOf="@id/iv_head" android:text="等级 : 总代理" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:textColor="@color/cl_default" android:gravity="center" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:text="我要升级" android:layout_width="80dp" android:layout_height="30dp" /> </RelativeLayout> <android.support.design.widget.TabLayout android:id="@id/id_stickynavlayout_indicator" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" app:tabGravity="fill" app:tabMode="fixed" app:tabTextColor="#A2A2A2" app:tabIndicatorColor="@color/white" app:tabSelectedTextColor="#000000" > </android.support.design.widget.TabLayout> </android.support.design.widget.AppBarLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:scrollbars="none" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!--appbar_scrolling_view_behavior--> <android.support.v4.view.ViewPager android:background="#fff" android:id="@id/id_stickynavlayout_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> </LinearLayout></android.support.design.widget.CoordinatorLayout>本项目用的recylerview是封装了上拉加载和下拉刷新的SuperRecyclerView,这样会造成列表会给底部的tab遮挡,解决方案如下:
1.在tablyout的fragment中,加入设置recylerview的边距代码
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ry_PRimary.getLayoutParams(); params.setMargins(0,0,0, DensityUtils.dp2px(mContext,60)); ry_primary.setLayoutParams(params);这样会让recylerview的item显示正常,第二次切换的时候,在重新把边距设置为0,就不会出现,recylerview距离底部有距离的情况。2.第二种方式比较简单,直接给SuperRecyclerView添加下拉刷新,就能解决item给tab遮挡的问题ry_primary.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { lists.clear(); for (int i=0; i < 10; i++){ lists.add(" " + i); } adapter.setData(lists); } });
新闻热点
疑难解答