Pull down refresh and pull up more (implemented using SwipeRefreshLayout and ListView), swiperefreshlayout

Source: Internet
Author: User

Pull down refresh and pull up more (implemented using SwipeRefreshLayout and ListView), swiperefreshlayout

SwipeRefreshLayout is a pull-down refresh component updated by Google in the support v4 19.1 library, making it easier to refresh.


1: add the SwipeRefreshLayout and Listview components to the layout.

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout style = "@ style/BaseStyle. White" xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Pull-down refresh --> <android. support. v4.widget. SwipeRefreshLayout android: id = "@ + id/srl_refresh" style = "@ style/BaseStyle"> <! -- Pull up more --> <ListView android: id = "@ + id/lv_person_goods" style = "@ style/BaseStyle" android: gravity = "center" android: horizontalSpacing = "@ dimen/margin_standard" android: numColumns = "2" android: scrollbarStyle = "outsideOverlay" android: verticalSpacing = "@ dimen/margin_standard"/> </android. support. v4.widget. swipeRefreshLayout> <! -- View displayed when data is empty --> <TextView android: id = "@ + id/TV _no_data" style = "@ style/BaseStyle. fullWrap "android: layout_marginTop =" @ dimen/textview_width_small "android: gravity =" center_horizontal "android: text =" no data "android: textColor =" @ color/text_title_standard "android: textSize = "@ dimen/font_size_small" android: visibility = "gone"/> </RelativeLayout>



2: Use on the home page

/*** Created by pengkv on 2014/12/5. * My Baby list page, used to select to add to the auction site */public class MySaleGoodsListViewActivity extends VolleyActivity implements IInit, IResponseHandler, IPagination, SwipeRefreshLayout. onRefreshListener {private int auctionID; private GoodsInfoViewModel mViewModel; private TextView mEmptyTV; // blank view private ListView mListView; // session List private PersonGoodsListAdapter mAdapter; // adapter private Swip ERefreshLayout mSwipeRefreshLayout; // pull down and refresh the layout @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_me_auction_goods_list); init () ;}@ Override public void init () {ActionBarUtil. setup (this, "my shop baby"); auctionID = getIntent (). getIntExtra (EnumIntentKey. AUCTION_ID.toString (), 0); mListView = (ListView) findViewById (R. id. lv_p Erson_goods); mEmptyTV = (TextView) findViewById (R. id. TV _no_data); mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById (R. id. srl_refresh); // pull-down refresh component mSwipeRefreshLayout. setOnRefreshListener (this); // set the refresh listener mSwipeRefreshLayout. setColorSchemeResources (R. color. background_blue_standard, R. color. white, R. color. background_blue_standard, R. color. white); // set the color of the pull-down refresh component. mViewModel = new GoodsInfoViewModel (); FetchData (FIRST); // get favorite list Data} @ Override public void fetchData (int tag) {GetGoodsListForSellParam param = new GetGoodsListForSellParam (Data. getUserID (), 1, mViewModel. getPageIndex (), Data. PAGE_SIZE_MEDIUM); SquareApi. getGoodsListForSell (this, param, tag);}/*** update adapter */@ Override protected void onRestart () {super. onRestart (); mViewModel. reset (); fetchData (FIRST); // get favorite list data} @ Override public Void updateUI (Object response, final int tag) {if (response = null) return; if (tag = FIRST) {// get the mViewModel of favorite list data. inflate (response); // determines whether the list adapter mAdapter is empty for paging. if (mAdapter = null) {// when the adapter is empty, mSwipeRefreshLayout. setRefreshing (false); // the pull-down refresh component stops refreshing mAdapter = new PersonGoodsListAdapter (this, mViewModel. getList (); mListView. setAdapter (mAdapter); // ListView is bound to a favorite adapter/*** pull more */mListView. se TOnScrollListener (new AbsListView. onScrollListener () {@ Override public void onScrollStateChanged (AbsListView view, int scrollState) {switch (scrollState) {case SCROLL_STATE_IDLE: if (mListView. getLastVisiblePosition () = mViewModel. getList (). size ()-1) {if (! MViewModel. isComplete () {fetchNewData (FIRST) ;}} break ;}@ Override public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount ){}}); mListView. setEmptyView (mEmptyTV); // bind the image to the favorite list ListView} else {mAdapter. notifyDataSetChanged (); // adapter updates data} mListView. setOnItemClickListener (new AdapterView. onItemClickListener () {@ Override public void onItemClick (Ad ApterView <?> Parent, View view, int position, long id) {Intent I = new Intent (); if (getIntent (). getBooleanExtra (EnumIntentKey. IS_SELECT_LINK.toString (), false) {// select the sending link I. putExtra (EnumIntentKey. GOODS_ID.toString (), mViewModel. getList (). get (position ). getGoodsID (); I. putExtra (EnumIntentKey. GOODS_NAME.toString (), mViewModel. getList (). get (position ). getGoodsName (); setResult (RESULT_ OK, I);} else {// Add to session I. setClass (MySaleGoodsListViewActivity. this, AddAuctionGoodsActivity. class); I. putExtra (EnumIntentKey. AUCTION_GOODS_ID.toString (), mViewModel. getList (). get (position ). getGoodsID (); I. putExtra (EnumIntentKey. IS_DO.toString (), true); I. putExtra (EnumIntentKey. AUCTION_ID.toString (), auctionID); startActivity (I) ;}finish () ;}}}@ Override public void fetchNewData (int tag) {mViewModel. increasePageIndex (); fetchData (FIRST); // obtain the list of favorite items} @ Override public void onRefresh () {mViewModel. reset (); // reset the page index and completion status mAdapter = null; // clear the adapter fetchData (FIRST); // get the list of favorite items }}

Resolution:

(Pull-down refresh) The SwipeRefreshLayout. OnRefreshListener interface must be implemented on the home page.

 @Override    public void onRefresh() {    }
Update in Method


(Pull more) Pull more need to listen to setOnScrollListener () method

  mListView.setOnScrollListener(new AbsListView.OnScrollListener() {                    @Override                    public void onScrollStateChanged(AbsListView view, int scrollState) {                        switch (scrollState) {                            case SCROLL_STATE_IDLE:                                if (mListView.getLastVisiblePosition() == mViewModel.getList().size() - 1) {                                    if (!mViewModel.isComplete()) {                                        fetchNewData(FIRST);                                    }                                }                                break;                        }                    }                    @Override                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {                    }                });




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.