上拉重新整理下拉載入(實現程式碼)
阿新 • • 發佈:2019-01-01
一、導依賴
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
二、Xml佈局
<com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/refresh" android:layout_width="match_parent" android:layout_height="match_parent"> <com.scwang.smartrefresh.layout.header.ClassicsHeader android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v7.widget.RecyclerView android:id="@+id/mRecyclerView_two" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> <com.scwang.smartrefresh.layout.footer.ClassicsFooter android:layout_width="match_parent" android:layout_height="wrap_content"></com.scwang.smartrefresh.layout.footer.ClassicsFooter> </com.scwang.smartrefresh.layout.SmartRefreshLayout>
三、程式碼中
private RecyclerView mRecyclerView_two; private SmartRefreshLayout refresh; private List meiNvList = new ArrayList<>(); private MeiNvAdapter meiNvAdapter; private int a; @Override protected void initView(View inflate) { mPersenter.loginMeiNvPre("http://gank.io/api/"); mRecyclerView_two = (RecyclerView) inflate.findViewById(R.id.mRecyclerView_two); refresh = (SmartRefreshLayout) inflate.findViewById(R.id.refresh); mRecyclerView_two.setLayoutManager(new GridLayoutManager(getContext(),2)); mRecyclerView_two.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { if (newState > 0) { ((MainActivity) getActivity()).findViewById(R.id.tabbar).setVisibility(View.GONE); } else { ((MainActivity) getActivity()).findViewById(R.id.tabbar).setVisibility(View.VISIBLE); } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); } }); refresh.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { meiNvList.clear(); mPersenter.loginMeiNvPre("http://gank.io/api/data/福利/20/1"); refreshlayout.finishRefresh(); } }); refresh.setOnLoadmoreListener(new OnLoadmoreListener() { @Override public void onLoadmore(RefreshLayout refreshlayout) { a++; mPersenter.loginMeiNvPre("http://gank.io/api/data/福利/20/" + a); refreshlayout.finishLoadmore(); } }); }
這個只寫了下拉載入,上拉重新整理沒有寫,但是確實同理,你自己仔細看看程式碼就會懂了!!!