Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize pull-down refresh and pull-up load in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to achieve Android pull-down refresh and pull-up loading, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Technical essentials

ListView.setSelection (int postion); navigate to a specific entry of the listview.

ListView.addHeaderView (view view); add view to the listview header

ListView.addFooterView (view); add view to the bottom of the listview

ListView.setOnScrollListener (this); add a scrolling event to the listview to listen for the scrolling position.

If you scroll to the top, the header view is displayed to refresh the data.

If you scroll to the bottom of the view display, achieve data loading.

2. Examples

Package com.example.test

Import java.util.ArrayList

Import java.util.HashMap

Import android.app.Activity

Import android.os.Bundle

Import android.os.Handler

Import android.util.Log

Import android.view.View

Import android.widget.AbsListView

Import android.widget.AbsListView.OnScrollListener

Import android.widget.ListView

Import android.widget.SimpleAdapter

Import android.widget.TextView

Import android.widget.Toast

Public class MainActivity1 extends Activity implements OnScrollListener {

Private static final String TAG = "MainActivity"

Private ListView listView

Private View moreView,moreViewheader; / / load more pages

Private SimpleAdapter adapter

Private ArrayList listData

Private int lastItem

Private int count

Private int firstVisibleItem

@ Override

Public void onCreate (Bundle savedInstanceState) {

Super.onCreate (savedInstanceState)

SetContentView (R.layout.activity_main1)

ListView = (ListView) findViewById (R.id.listView)

MoreView = getLayoutInflater () .inflate (R.layout.load1, null)

MoreViewheader = getLayoutInflater () .inflate (R.layout.load, null)

ListData = new ArrayList ()

ListView.setDivider (null)

PrepareData (); / / prepare data

Count = listData.size ()

Adapter = new SimpleAdapter (this, listData, R.layout.item

New String [] {"itemText"}, new int [] {R.id.itemText})

ListView.addHeaderView (moreViewheader)

ListView.addFooterView (moreView); / / add the bottom view. Be sure to add it before setAdapter, otherwise an error will be reported.

ListView.setAdapter (adapter); / / set adapter

ListView.setSelection (1)

ListView.setOnScrollListener (this); / / sets the scrolling event for listview

}

Private void prepareData () {/ / prepare data

For (int I = 0; I < 10; iTunes +) {

HashMap map = new HashMap ()

Map.put ("itemText", "test data" + I)

ListData.add (map)

}

}

Private void loadMoreData () {/ / load more data

Count = adapter.getCount ()

For (int I = count; I < count + 5; iTunes +) {

HashMap map = new HashMap ()

Map.put ("itemText", "test data more---" + I)

ListData.add (map)

}

Count = listData.size ()

}

@ Override

Public void onScroll (AbsListView view, int firstVisibleItem

Int visibleItemCount, int totalItemCount) {

Log.i (TAG, "firstVisibleItem=" + firstVisibleItem

+ ", visibleItemCount=" + visibleItemCount + ", totalItemCount"

+ totalItemCount)

This.firstVisibleItem=firstVisibleItem

LastItem = this.firstVisibleItem + visibleItemCount-2; / / minus 1 because there is an addFooterView on it

}

@ Override

Public void onScrollStateChanged (AbsListView view, int scrollState) {

Log.i (TAG, "scrollState=" + scrollState)

/ / update when the number of the last item is equal to the total number of data.

System.out.println (lastItem + "= =" + count)

If (lastItem = = count & & scrollState = = this.SCROLL_STATE_IDLE) {

Log.i (TAG, "pull to the bottom")

MoreView.setVisibility (View.VISIBLE)

MHandler.sendEmptyMessage (0)

}

If (this.firstVisibleItem==0) {

((TextView) moreViewheader.findViewById (R.id.button1)) .setText ("refreshing...")

MoreViewheader.setVisibility (View.VISIBLE)

MHandler.sendEmptyMessage (1)

}

}

/ / declare Handler

Private Handler mHandler = new Handler () {

Public void handleMessage (android.os.Message msg) {

Switch (msg.what) {

Case 0:

Try {

Thread.sleep (3000)

} catch (InterruptedException e) {

E.printStackTrace ()

}

LoadMoreData (); / / load more data, here you can use asynchronous loading

Adapter.notifyDataSetChanged ()

MoreView.setVisibility (View.GONE)

Log.i (TAG, "load more data")

ListView.setSelection (lastItem)

Break

Case 1:

Try {

Thread.sleep (3000)

} catch (InterruptedException e) {

E.printStackTrace ()

}

Adapter.notifyDataSetChanged ()

MoreViewheader.setVisibility (View.GONE)

ListView.setSelection (1)

Break

Default:

Break

}

}

}

}

2. Search in github using the open source framework pulltorefresh.

The above is all the contents of the article "how to pull down, refresh and load Android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report