SwipeRefreshLayout 與 CoordinatorLayout 嵌套刷新
1. 動態設置SwipeRefreshLayout的是否能夠刷新 setEnable(boolean isEnable);
2. 設置SwipRefreshLayout刷新圖標的位置 setProgressViewOffset(true, -20, 100);android
首先看 SwipeRefreshLayout 與 CoordinatorLayout 佈局:
1) SwipeRefreshLayout 做爲最外層佈局
2) CoordinatorLayout 做爲第二次佈局
3) CoordinatorLayout 中嵌套 AppBarLayout 及 ListView 或 RecyclerView 注意 app:layout_behavior="@string/appbar_scrolling_view_behavior",
因爲 CoordinatorLayout 是 FrameLayout 子類所以 app:layout_behavior 爲必要屬性,不然沒法正常顯示
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:addStatesFromChildren="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">git
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coord_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">app
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false">ide
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:theme="@style/AppTheme.AppBarOverlay"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="10dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">佈局
<android.support.v4.view.ViewPager
android:id="@+id/toolbar_viewpager"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:clipChildren="false"
app:layout_collapseParallaxMultiplier="1" />this
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">.net
</android.support.v7.widget.Toolbar>xml
</android.support.design.widget.CollapsingToolbarLayout>ip
</android.support.design.widget.AppBarLayout>utf-8
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
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.v4.widget.SwipeRefreshLayout>
3.設置 SwipeRefreshLayout
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
mSwipeRefreshLayout.setOnRefreshListener(this);
//設置樣式刷新顯示的位置
mSwipeRefreshLayout.setProgressViewOffset(true, -20, 100);
mSwipeRefreshLayout.setColorSchemeResources(R.color.swiperefresh_color1, R.color.swiperefresh_color2, R.color.swiperefresh_color3, R.color.swiperefresh_color4);
4.監聽 AppBarLayout Offset 變化,動態設置 SwipeRefreshLayout 是否可用
appBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (verticalOffset >= 0) {
mSwipeRefreshLayout.setEnabled(true);
} else {
mSwipeRefreshLayout.setEnabled(false);
}
}
});
Demo:https://git.oschina.net/xlxq-PHS/FunUI.git
寫的很差你們多指正