上章講了CoordinatorLayout的卡頓BUG,既然有BUG又沒解決,說實話沒必要講下去,但是做事總要有始有終,既然寫了就把它寫完吧,頂着BUG去寫。
四、CoordinatorLayout + 下拉重新整理
之前用過,但是在google找不到我心目中的重新整理方式,google上寫的都是在滑動控件上加一層重新整理,而且都是SwipeRefreshLayout。如果你是想實作這樣的效果,你直接在google上找代碼就是,一大堆。我要做的是在整個CoordinatorLayout 外加一層重新整理,入下圖的效果。

image.png
我們在CoordinatorLayout+ AppBarLayout + ViewPager這個例子上加下拉重新整理架構。
1. 準備
我使用的是PullToRefresh架構,但是我會告訴你,你要用SwipeRefreshLayout的做法也是一樣的,下拉架構的原理都是那樣。
既然是使用PullToRefresh,就要導入PullToRefresh的Module,用過的都知道,沒用過的希望先去了解一下。不懂導入Module的趕緊去學。
2.思路
在實作這樣一個架構,你想想,就需要在原來的布局中加入一層上拉下拉架構。簡單來說,我們要把CoordinatorLayout+ AppBarLayout + ViewPager放到PullToRefresh的中間部分,也就是PullToRefresh的PullToRefreshBase。那我們就需要先寫個PullToRefreshBase代表CoordinatorLayout+ AppBarLayout + ViewPager。
注意:
如果你不會用PullToRefresh架構,你肯定看懵逼我接下來的代碼,是以不了解這個架構的一定要先學會這個架構的簡單用法,比如你寫個demo讓PullToRefresh裝RecyclerView,等你大概會用了再看我接下去的步驟,我也不想寫太多基礎上的細節。
3.定義PullToRefreshBase
xml布局:
<com.example.kylinarm.coordinatorlayouttest.PullRefreshCoordTest
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pull"
>
</com.example.kylinarm.coordinatorlayouttest.PullRefreshCoordTest>
布局沒什麼好解釋的。
自定義PullToRefreshBase:
public class PullRefreshCoordTest extends PullToRefreshBase<CoordinatorView> {
private CoordinatorView coordinatorView;
public PullRefreshCoordTest(Context context) {
super(context);
}
public PullRefreshCoordTest(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PullRefreshCoordTest(Context context, Mode mode) {
super(context, mode);
}
public PullRefreshCoordTest(Context context, Mode mode, AnimationStyle animStyle) {
super(context, mode, animStyle);
}
@Override
public Orientation getPullToRefreshScrollDirection() {
return Orientation.VERTICAL;
}
@Override
protected CoordinatorView createRefreshableView(Context context, AttributeSet attrs) {
coordinatorView = new CoordinatorView(context);
return coordinatorView;
}
@Override
protected boolean isReadyForPullEnd() {
return false;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected boolean isReadyForPullStart() {
// 或者判斷CoordinatorView是否全部展開
return coordinatorView.getScrollY() == 0 && coordinatorView.getmVerticalOffset() >= 0;
}
public CoordinatorView getCoordinatorView() {
return coordinatorView;
}
}
介紹:
(1)CoordinatorView就是我們的CoordinatorLayout+ AppBarLayout + ViewPager,等下會放這個view具體處理的代碼。
(2)createRefreshableView()方法就是建立嵌套在PullToRefresh裡面的view,這是基礎。
(3)isReadyForPullStart(),這個方法很重要,是判斷什麼時候可以下拉重新整理,裡面是一個算法,這個算法我放到最後講。
4. 定義CoordinatorView(CoordinatorLayout+ AppBarLayout + ViewPager)
public class CoordinatorView extends FrameLayout {
@InjectView(R.id.tl_tab)
TabLayout tlTab;
@InjectView(R.id.vp_content)
MyViewPager vpContent;
@InjectView(R.id.appbar)
AppBarLayout appbar;
private View contentView;
//記錄是否處于展開狀态
public int mVerticalOffset = 0;
private String[] titles = {"tab1","tab2"};
private List<Fragment> fragments = new ArrayList<>();
private Context context;
private FragmentActivity fActivity;
public CoordinatorView(Context context) {
super(context);
this.context = context;
if (context instanceof FragmentActivity){
fActivity = (FragmentActivity) context;
}
init();
}
private void init(){
contentView = LayoutInflater.from(getContext()).inflate(R.layout.activity_cv,null);
contentView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
addView(contentView);
ButterKnife.inject(this,contentView);
appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
mVerticalOffset = verticalOffset;
}
});
}
public void initView(){
for (int i = 0; i < titles.length; i++) {
tlTab.addTab(tlTab.newTab().setText(titles[i]));
}
fragments = setFragmentList();
vpContent.setAdapter(new FragmentPagerAdapter(fActivity.getSupportFragmentManager()) {
@Override
public int getCount() {
return titles.length;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
if (null != titles) {
return titles[position];
}
return super.getPageTitle(position);
}
});
tlTab.setupWithViewPager(vpContent);
}
private List<Fragment> setFragmentList(){
List<Fragment> fragmentList = new ArrayList<>();
fragmentList.add(new ListFragment());
fragmentList.add(new ImageFragment());
return fragmentList;
}
public int getmVerticalOffset() {
return mVerticalOffset;
}
}
其實這裡的代碼很容易看到,就是CoordinatorLayout+ AppBarLayout + ViewPager的正常操作,不過加了一步很重要的方法:
在init()中
appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
mVerticalOffset = verticalOffset;
}
});
這裡是給AppBarLayout加了一個監聽事件addOnOffsetChangedListener,這個監聽是什麼意思呢?就是監聽折疊。
這個方法中的verticalOffset表示的是...我也不懂怎麼解釋好,我就告訴你,當AppBarLayout處于完全展開狀态,verticalOffset為0,而當你折疊的時候verticalOffset就變成負數,完全折疊時verticalOffset就變成負的AppBarLayout内折疊控件的高度。這個屬性和判斷什麼時候能下拉有關,一定要先了解。
5.判斷下拉
我們在PullRefreshCoordTest 的isReadyForPullStart方法中寫了一個算法來判斷什麼時候能下拉,現在我們在了解完上面的代碼之後來講講這個算法。
protected boolean isReadyForPullStart() {
// 或者判斷CoordinatorView是否全部展開
return coordinatorView.getScrollY() == 0 && coordinatorView.getmVerticalOffset() >= 0;
}
(1)coordinatorView.getScrollY()這個不用講了,很容易懂
(2)coordinatorView.getmVerticalOffset() >= 0;
什麼意思呢?coordinatorView.getmVerticalOffset() 能傳回我們第4步中講的verticalOffset,上面我們說過完全展開的時候是0,折疊之後變成負數。也就是說這句話的意思是完全展開時才能讓這個view重新整理。
其實也不是很難了解,多看兩次代碼就能看懂了。記住,要想讓CoordinatorLayout重新整理,隻有在它處于完全展開的時候才能重新整理。