Sim*_*mon 2 android facebook android-fragments android-activity swiperefreshlayout
当您刷新 facebook 页面时,SwipeRefreshLayout似乎被放置在活动类中并从屏幕顶部下降:
看起来RecyclerView像是在它下面的东西被刷新了。您还需要一直滚动到屏幕顶部,然后SwipeRefreshLayout才会自行激活 - 因此,如果ImageView工具栏中的 没有出现在屏幕顶部,那么当您向下滑动时,您实际上只是在滚动RecyclerView. 在下面的截图中,我们可以看到顶部ImageView是不可见的,所以我们只是滚动我们的 recyclerview。
我正在尝试通过在 xml 中执行以下操作来在我的应用程序中实现类似的东西,这看起来类似于我上面发布的第一个 Facebook 屏幕截图 - 我的活动具有从顶部出现的“swipeRefresh circle icon”并遵循标准布局 xml设置toolbar一个ImageView:
主要活动 XML:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinatorlayout"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/appbar"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
android:background="@color/white"
app:expandedTitleTextAppearance="@color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<ImageView
android:id="@+id/backdrop"
android:contentDescription="@string/photo"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="192dp"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)
但是,SwipeRefreshLayout它完全关注活动,包括Fragment我在FrameLayout嵌套到我的主要活动 xml 中膨胀的任何滚动操作。在Fragment我充气是RecyclerView其上下滚动类似于Facebook的RecyclerView。
为了摆脱这个问题,我知道我可以SwipeRefreshLayout从我的Activityxml 中删除它并将其与我的Fragmentxml一起放置,但是我的 SwipeRefresh 圆圈图标只会从屏幕的末尾而不是顶部开始。
不知道 Facebook 如何设法停止SwipeRefreshLayout获得所有滚动操作的全部焦点 - 有谁知道他们是如何实现的SwipeRefreshLayout?
这是我的 Fragment xml 代码:
<android.support.design.widget.CoordinatorLayout
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"
android:id="@+id/snackbarPosition">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/post_container"
android:background="#E0E0E0">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progresswheel"
android:layout_margin="32dp"
android:layout_centerHorizontal="true"
android:visibility="gone"
tools:visibility="visible"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
好的 - 我终于想通了。
这并不容易,但解决方案实际上是取出片段类并将其替换为普通的recyclerview。
在 XML 中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinatorlayout"
android:fitsSystemWindows="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/appbar"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
android:background="@color/white"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
android:fitsSystemWindows="true"
app:expandedTitleTextAppearance="@color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:id="@+id/photo"
app:layout_collapseMode="parallax"
android:src="@drawable/hand_gestures"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent"
android:id="@+id/recyclerview">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)
然后,您需要按照此线程上的解决方案进行操作:Android: CollapsingToolbarLayout 和 SwipeRefreshLayout 被卡住以使 swipeToRefreshLayout 正常工作。
您可以通过实现以下内容来做到这一点 - 从线程复制:
在您的片段/活动中使其实现 AppBarLayout.OnOffsetChangedListener (现在当工具栏完全展开时启用刷新):
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (collapsingToolbarLayout.getHeight() + verticalOffset < 2 * ViewCompat.getMinimumHeight(collapsingToolbarLayout)) {
swipeRefreshLayout.setEnabled(false);
} else {
swipeRefreshLayout.setEnabled(true);
}
}
Run Code Online (Sandbox Code Playgroud)
覆盖 onPause() & onResume() 如@blackcj 答案:
@Override
public void onResume() {
super.onResume();
appBarLayout.addOnOffsetChangedListener(this);
}
@Override
public void onPause() {
super.onPause();
appBarLayout.removeOnOffsetChangedListener(this);
}
Run Code Online (Sandbox Code Playgroud)
然后将 LinearLayoutManager 设置为您的 recyclerView:
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
Run Code Online (Sandbox Code Playgroud)
对我来说,这就像一个魅力,首先 appBarlayout 被扩展,然后 swipeRefreshLayout 触发刷新。
| 归档时间: |
|
| 查看次数: |
3765 次 |
| 最近记录: |