diff --git a/app/build.gradle b/app/build.gradle index e817066..5c50f51 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/java/com/donkingliang/consecutivescrollerdemo/MyFragment.java b/app/src/main/java/com/donkingliang/consecutivescrollerdemo/MyFragment.java index 9ce64f5..6cfc426 100644 --- a/app/src/main/java/com/donkingliang/consecutivescrollerdemo/MyFragment.java +++ b/app/src/main/java/com/donkingliang/consecutivescrollerdemo/MyFragment.java @@ -14,6 +14,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.donkingliang.consecutivescrollerdemo.adapter.RecyclerViewAdapter; +import com.scwang.smartrefresh.layout.SmartRefreshLayout; /** * @Author donkingliang @@ -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); + } } diff --git a/app/src/main/java/com/donkingliang/consecutivescrollerdemo/ViewPagerActivity.java b/app/src/main/java/com/donkingliang/consecutivescrollerdemo/ViewPagerActivity.java index 6f90e3f..4ccd6e8 100644 --- a/app/src/main/java/com/donkingliang/consecutivescrollerdemo/ViewPagerActivity.java +++ b/app/src/main/java/com/donkingliang/consecutivescrollerdemo/ViewPagerActivity.java @@ -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) { @@ -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() { @@ -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 getTabs() { diff --git a/app/src/main/res/layout/activity_viewpager.xml b/app/src/main/res/layout/activity_viewpager.xml index b21bf3c..85152f2 100644 --- a/app/src/main/res/layout/activity_viewpager.xml +++ b/app/src/main/res/layout/activity_viewpager.xml @@ -1,35 +1,41 @@ - + android:layout_height="match_parent"> - + android:layout_height="match_parent" + android:scrollbars="vertical"> - + - + + + - \ No newline at end of file + + \ No newline at end of file