Data is automatically loaded when listview is rolled to the bottom, while listview is rolling.
In android, listvieww loads data, and some load data in batches. For example, if 20 messages are loaded at a time, 5 times are required to load 100 entries. If 100 entries are loaded at a time, the loading of listview becomes slow, if there are images, the traffic will be wasted for a while. Second, there will be misplacement in the images in the item. Currently, there are many apps loading data in listview.
I. Batch loading and sliding to the bottom is automatically updated
2. slide to the bottom and manually click to load more
3. Pull down and refresh + load more at the bottom
Let's talk about the automatic update of listview sliding to the bottom today
Analysis: listens to the sliding event of the listview, determines whether the listview slides to the bottom, and then loads data.
The Code is as follows:
Public class MainActivity extends Activity {private ListView; private List <String> datas; private LayoutInflater inflater; private MyAdapter adapter; private List <String> contents; private int count = 0; private View footView; private Handler handler = new Handler (); int lastItem; private RelativeLayout loading; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initData (); inflater = LayoutInflater. from (this); listview = (ListView) findViewById (R. id. listview); adapter = new MyAdapter (); footView = inflater. inflate (R. layout. footer, null); loading = (RelativeLayout) footView. findViewById (R. id. loading); // Add the view to the bottom of the listview by adding addFooterView () to the listview. setAdapter (adapter); the Code is preceded by listview. addFooterView (footView); listview. setAdapter (adapter); // Add a listview scroll listener listview. setOnScrollListener (new OnScrollListener () {// The view object of AbsListView view is listview @ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {if (scrollState = OnScrollListener. SCROLL_STATE_IDLE) {if (view. getLastVisiblePosition () = view. getCount ()-1) {loadData () ;}}@ Overridepublic void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {lastItem = firstVisibleItem + visibleItemCount-1 ;}}) ;}protected void loadData () {loading. setVisibility (View. VISIBLE); handler. postDelayed (new Runnable () {@ Overridepublic void run () {load (); loading. setVisibility (View. GONE); adapter. notifyDataSetChanged () ;}}, 5000) ;} protected void load () {int count = adapter. getCount () + 1; for (int I = count; I <count + 20; I ++) {contents. add ("load data:" + I) ;}} private void initData () {contents = new ArrayList <String> (); for (int I = 1; I <20; I ++) {contents. add ("load data:" + I) ;}} class MyAdapter extends BaseAdapter {@ Overridepublic int getCount () {return contents. size () ;}@ Overridepublic Object getItem (int position) {return contents. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder; if (convertView = null) {convertView = inflater. inflate (R. layout. item, parent, false); holder = new ViewHolder (); holder. tvContent = (TextView) convertView. findViewById (R. id. tvContent); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} holder. tvContent. setText (contents. get (position); return convertView;} class ViewHolder {TextView tvContent ;}}}
In the android program, when the ListView asynchronously loads data, if the display is loaded every time, the new data is triggered.
After reading this, it should be okay. Why don't you execute the // statement? I wrote
OnScrollStateChanged has another layer of view. getLastVisiblePosition () = view. getCount ()-1 to judge whether it works.
How to Make listview scroll to the bottom
// The adapter is the Adapter bound to the ListView. If it is not convenient to use directly, you can also obtain it through the getAdapter () method of the ListView, provided that you have already bound the adapter.
// The parameters in it are very familiar. In fact, the main function of this method is to select the specified column of the listview. If it is selected, the item must be visible and naturally scroll.