向下滚动会触发刷新而不是显示工具栏
Scrolling down triggers refresh instead of revealing the toolbar
我开始使用新的 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>
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>
SwipeRefreshLayout 在 FrameLayout 中,因为我的应用程序中的片段中有更多元素。
如有任何帮助,我们将不胜感激。
更新:此问题已在支持库 v23.1.1 中修复。
当 AppBarLayout
偏移量发生变化时,您似乎必须 enable/disable SwipeRefreshLayout
,使用 AppBarLayout.OnOffsetChangedListener
。
见
可能为时已晚,但它可能会帮助任何新来者,我想出了一个简单的解决方案。通过创建扩展 SwipeRefreshLayout
的自定义 class
,您可以在 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);
}
}
我开始使用新的 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>
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>
SwipeRefreshLayout 在 FrameLayout 中,因为我的应用程序中的片段中有更多元素。
如有任何帮助,我们将不胜感激。
更新:此问题已在支持库 v23.1.1 中修复。
当 AppBarLayout
偏移量发生变化时,您似乎必须 enable/disable SwipeRefreshLayout
,使用 AppBarLayout.OnOffsetChangedListener
。
见
可能为时已晚,但它可能会帮助任何新来者,我想出了一个简单的解决方案。通过创建扩展 SwipeRefreshLayout
的自定义 class
,您可以在 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);
}
}