Lon*_*ngi 23 android swiperefreshlayout android-coordinatorlayout
我开始使用新的CoordinatorLayout,我遇到了这个问题:

正如您所看到的,当我尝试向下滚动工具栏部分可见时,并且recylcerview位于顶部位置时,它会触发刷新事件而不是拉下工具栏.用户必须再次向上滚动以显示它.
布局XML看起来像这样:activity_main.xml:
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.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">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
style="@style/customActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/navigation_items" />
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
fragment_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="8dp"
android:scrollbars="none" />
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
Ther SwipeRefreshLayout在FrameLayout中,因为我的应用程序中的片段中有更多元素.
任何帮助,将不胜感激.
k0s*_*0sh 10
也许为时已晚,但它可能对任何新人有所帮助,我想出了一个简单的解决方案.通过创建class扩展的自定义,SwipeRefreshLayout您可以在任何AppBaLayout存在的地方使用它,遵循以下代码
public class MySwipeLayout extends SwipeRefreshLayout implements AppBarLayout.OnOffsetChangedListener {
private AppBarLayout appBarLayout;
public MySwipeLayout(Context context) {
super(context);
}
public MySwipeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (getContext() instanceof Activity) {
appBarLayout = (AppBarLayout) ((Activity) getContext()).findViewById(R.id.appbar);
appBarLayout.addOnOffsetChangedListener(this);
}
}
@Override
protected void onDetachedFromWindow() {
if(appBarLayout!=null){
appBarLayout.removeOnOffsetChangedListener(this);
appBarLayout = null;
}
super.onDetachedFromWindow();
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
this.setEnabled(i == 0);
}
}
Run Code Online (Sandbox Code Playgroud)
更新:此问题已在支持库v23.1.1中修复.
好像你必须启用/禁用SwipeRefreshLayout当AppBarLayout偏移变化,使用AppBarLayout.OnOffsetChangedListener.
| 归档时间: |
|
| 查看次数: |
6975 次 |
| 最近记录: |