我将CollapsingToolbarLayout,RecyclerView和SwipeRefreshLayout一起使用:
Xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/collapse_toolbar_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<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:fitsSystemWindows="true"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/toolbar_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<include
layout="@layout/activity_main_toolbar"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<cz.yetanotherview.webcamviewer.app.helper.EmptyRecyclerView
android:id="@+id/mainList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floating_action_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:layout_margin="16dp"
app:fabSize="mini"
android:src="@drawable/ic_action_edit"
android:onClick="assignSelectedWebCamsToCategory"/>
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/floating_action_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|end"
android:paddingRight="10dp"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
fab:menu_shadowColor="#37000000"
fab:menu_colorNormal="#DA4336"
fab:menu_colorPressed="#E75043"
fab:menu_colorRipple="#99FFFFFF"
fab:menu_icon="@drawable/fab_add"
fab:menu_buttonSpacing="10dp"
fab:menu_labels_textColor="@color/very_dark_grey"
fab:menu_labels_textSize="14sp"
fab:menu_labels_colorNormal="@color/white"
fab:menu_labels_colorPressed="@color/next_grey"
fab:menu_labels_colorRipple="#99FFFFFF"
fab:menu_labels_margin="8dp"
fab:menu_backgroundColor="@color/black_transparent">
<com.github.clans.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_content_import"
fab:fab_size="mini"
fab:fab_label="@string/pref_import_from_server"
fab:fab_colorNormal="@color/white"
app:fab_colorPressed="@color/next_grey"
app:fab_colorRipple="#99FFFFFF"
android:onClick="showSelectionDialog"/>
<com.github.clans.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_content_manually"
fab:fab_size="mini"
fab:fab_label="@string/create_manually"
fab:fab_colorNormal="@color/white"
app:fab_colorPressed="@color/next_grey"
app:fab_colorRipple="#99FFFFFF"
android:onClick="showAddDialog"/>
<com.github.clans.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_content_suggestion"
fab:fab_size="mini"
fab:fab_label="@string/submit_suggestion"
fab:fab_colorNormal="@color/white"
app:fab_colorPressed="@color/next_grey"
app:fab_colorRipple="#99FFFFFF"
android:onClick="showSuggestionDialog"/>
</com.github.clans.fab.FloatingActionMenu>
</android.support.design.widget.CoordinatorLayout>
<include
layout="@layout/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
代码:
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeRefreshLayout.setOnRefreshListener(this);
当折叠的工具栏布局为完全展开后的和滚动 View (recyclerview)在顶部上时,如何仅允许滑动刷新 Action 仅?类似于Google+或Inbox应用程序中的行为。
错误:
好:
最佳答案
更新:该问题已在最新版本的支持库(23.1.1+)中得到解决。如果您使用的支持库版本较旧,请升级或继续阅读。
如果您使用的支持库版本较旧,则将偏移量更改侦听器添加到AppBarLayout中,以启用或禁用滑动以相应刷新布局。此处提供其他代码:
https://gist.github.com/blackcj/001a90c7775765ad5212
相关更改:
public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {
...
private AppBarLayout appBarLayout;
private SwipeRefreshLayout mSwipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.contentView);
appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
//The Refresh must be only active when the offset is zero :
mSwipeRefreshLayout.setEnabled(i == 0);
}
@Override
protected void onResume() {
super.onResume();
appBarLayout.addOnOffsetChangedListener(this);
}
@Override
protected void onPause() {
super.onPause();
appBarLayout.removeOnOffsetChangedListener(this);
}
}