Skip to content

Commit

Permalink
添加使用SmartRefreshLayout刷新加载的示例
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Jun 30, 2020
1 parent ff547d8 commit 73c0b86
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 28 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation project(':consecutivescroller')

// 刷新框架
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import androidx.recyclerview.widget.RecyclerView;

import com.donkingliang.consecutivescrollerdemo.adapter.RecyclerViewAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

/**
* @Author donkingliang
Expand All @@ -40,4 +41,15 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
return view;

}

public void onLoadMore(final SmartRefreshLayout layout){
// 模拟加载5秒钟
layout.postDelayed(new Runnable() {
@Override
public void run() {
// 加载完成
layout.finishLoadMore();
}
},5000);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
package com.donkingliang.consecutivescrollerdemo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout;
import com.donkingliang.consecutivescroller.ConsecutiveViewPager;
import com.donkingliang.consecutivescrollerdemo.adapter.TabPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

public class ViewPagerActivity extends AppCompatActivity {

private ConsecutiveScrollerLayout scrollerLayout;
private ConsecutiveViewPager viewPager;
private TabLayout tabLayout;
private SmartRefreshLayout refreshLayout;

private TabPagerAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -28,9 +41,13 @@ protected void onCreate(Bundle savedInstanceState) {
"下面的例子中,通过自定义ViewPager,实现IConsecutiveScroller接口,ConsecutiveScrollerLayout能正确的处理ViewPager里" +
"的子布局。如果ViewPager的内容是可以垂直滑动的,请使用ConsecutiveScrollerLayout或者RecyclerView等可滑动布局作为它内容的根布局。\n" +
"下面的列子中使用ViewPager承载多个Fragment,Fragment的根布局为ConsecutiveScrollerLayout。");
scrollerLayout = findViewById(R.id.scrollerLayout);
viewPager = findViewById(R.id.viewPager);
tabLayout = findViewById(R.id.tabLayout);
viewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager(), getTabs(), getFragments()));
refreshLayout = findViewById(R.id.refreshLayout);

mAdapter = new TabPagerAdapter(getSupportFragmentManager(), getTabs(), getFragments());
viewPager.setAdapter(mAdapter);
tabLayout.setupWithViewPager(viewPager);

tabLayout.post(new Runnable() {
Expand All @@ -39,6 +56,50 @@ public void run() {
viewPager.setAdjustHeight(tabLayout.getHeight());
}
});

refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout r) {
// 把加载的动作传给当初的fragment
MyFragment fragment = (MyFragment) mAdapter.getItem(viewPager.getCurrentItem());
fragment.onLoadMore(refreshLayout);
}

@Override
public void onRefresh(@NonNull RefreshLayout r) {
refreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
refreshLayout.finishRefresh();
}
}, 5000);
}
});


refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener() {
@Override
public void onFooterMoving(RefreshFooter footer, boolean isDragging, float percent, int offset, int footerHeight, int maxDragHeight) {
// 上拉加载时,保证吸顶头部不被推出屏幕。
scrollerLayout.setStickyOffset(offset);
}
});

// 滑动到底部,自动触发上拉加载
scrollerLayout.setOnVerticalScrollChangeListener(new ConsecutiveScrollerLayout.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollY, int oldScrollY, int scrollState) {
if (scrollState == ConsecutiveScrollerLayout.SCROLL_STATE_IDLE && scrollerLayout.isScrollBottom()){
Log.e("eee","自动触发上拉加载");
refreshLayout.autoLoadMore();
}
}
});
}

// 提供给Fragment获取使用。
public SmartRefreshLayout getRefreshLayout() {
return refreshLayout;
}

private List<String> getTabs() {
Expand Down
60 changes: 33 additions & 27 deletions app/src/main/res/layout/activity_viewpager.xml
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns:android="http://schemas.android.com/apk/res/android"
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollerLayout"
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
android:layout_height="match_parent">

<TextView
android:id="@+id/text"
<com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout
android:id="@+id/scrollerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="18sp" />
android:layout_height="match_parent"
android:scrollbars="vertical">

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:layout_isSticky="true"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorPrimary"
app:tabIndicatorHeight="3dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorPrimary" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="18sp" />

<com.donkingliang.consecutivescroller.ConsecutiveViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:layout_isSticky="true"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorPrimary"
app:tabIndicatorHeight="3dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorPrimary" />

<com.donkingliang.consecutivescroller.ConsecutiveViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
</com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>

0 comments on commit 73c0b86

Please sign in to comment.