In Android applications, ListView uses Onscrolllistener paging to load data _android

Source: Internet
Author: User

When users read meager from the network, if all of a sudden the user is not read the meager this will take a long time, resulting in a bad user experience, while a screen of content is not enough to show so much content. At this point, we need to use another function, that is ListView page. Load the data through the paging, the user will see how much to load.

Usually this is divided into two ways, one is to set a button, the user clicks that load. The other is to load automatically when the user slides to the bottom. Today I will share with you the realization of this function.

First, write an XML file, Moredata.xml, which defines the view placed at the bottom of the ListView:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" match_parent "
  android:o" rientation= "vertical" >
 <button  
   android:id= "@+id/bt_load" android:layout_width= "Fill_parent"  
   android:layout_height= "wrap_content" 
   android:text= "load more Data"/> 
 <progressbar
   android:id= "@+id/pg"
   android:layout_width= "wrap_content"
   android:layout_height= "Wrap_content"
   android: Layout_gravity= "Center_horizontal"
   android:visibility= "Gone"
   />
</LinearLayout>

You can see a button and a progress bar. Because only do a demo, here simple processing, by setting the control's visibility, the display button does not load, displays the progress bar when loading.

Write a item.xml, everyone should be very familiar with. The view used to define each item of the ListView.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" match_parent "
  android:o" rientation= "vertical" >
  
  <textview
    android:id= "@+id/tv_title"
    android:textsize= "20SP"
    Android:layout_width= "Wrap_content"
    android:layout_height= "wrap_content"
    android:layout_margintop= " 5DP "
    />
  <textview
    android:textsize=" 12SP "
    android:id=" @+id/tv_content "
    android: Layout_width= "Wrap_content"
    android:layout_height= "wrap_content"
    android:layout_margintop= "5DP"
    />

</LinearLayout>

Main.xml will not be pasted, the whole main interface is a ListView.

Take a look at the activity code first, and implement the paging effect inside.

Package com.notice.moredate;
Import java.util.ArrayList;

Import Java.util.HashMap;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.AbsListView;
Import Android.widget.AbsListView.OnScrollListener;
Import Android.widget.Button;
Import Android.widget.ListView;
Import Android.widget.ProgressBar;
Import Android.widget.SimpleAdapter;

Import Android.widget.Toast; public class Moredatelistactivity extends activity implements Onscrolllistener {//ListView private Simple
  Adapter Msimpleadapter;
  Private ListView LV;
  Private Button BT;
  Private ProgressBar PG;
  Private arraylist 

It should be easy to understand by annotation. Here's how to do the simple parsing. The first thing to note is that the Addfootview method must be before the Setadapter method, otherwise it will be invalid. The Addfootview method adds a view to the bottom of the ListView, which in this case is the button plus ProgressBar view. When the user clicks on the button, call the Loadmoredate method, bind more data for ListView, notify ListView Refresh by Adapter Notifydatasetchanged method, display the data just added.

This is done by handler asynchronous delay of 2 seconds to simulate the loading process. At the same time, ListView binds the Onscrolllistener listener and implements the Onscroll and Onscrollstatechanged methods. In the latter method, we can see that the ListView has stopped scrolling and the last visible entry is equal to the adapter entry, and that the user has slipped to the bottom and is automatically loaded, the code will be commented out, you can try.

A maxdatenum variable is added to the code to record the maximum number of data. That means a total of data in the network or elsewhere. After the user has finished loading the data, the Onscroll method is used to remove the ListView bottom view and not allow the load to continue. At the same time, in the Loadmoredate method, the maximum amount of data is also manipulated to determine the load quantity. (The default load 5, less than 5 to load the remaining).

Look at the effect chart:

A brief analysis of sequence times of Onscrolllistener events
in the Android onscrolllistener whole event I'm mainly analyzing his execution sequence:
Implement a listener interface for scrolling events

 new Abslistview.onscrolllistener () {@Override public void onscrollstatechanged (Abs ListView abslistview, int scrollstate) {switch (scrollstate) {case AbsListView.OnScrollListener.SCROLL_ST 
      Ate_touch_scroll://finger touch screen pull ready to roll, only trigger once order: 2 break; 
      Case AbsListView.OnScrollListener.SCROLL_STATE_FLING://continuous scrolling start, triggering only once order: 4 break; 
      Case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://The entire scrolling event ends, triggering only once in order: 6 break; 
    Default:break;             @Override public void Onscroll (Abslistview abslistview, int i, int i1, int i2) {//always scrolling, triggering multiple times Order: 1, 3, 5}} 

has been very confused before, and then carefully tested after the conclusion of the code comments above.
In addition to the ListView Picture List scrolling, should be scroll_state_fling when the picture does not show, improve scrolling performance so that scrolling smoother, scroll_state_idle display the current screen visible picture, for Onscroll () Interface method basically doesn't need him.
Add:
1. When the finger touches only the screen does not pull will trigger only once Onscroll method, does not trigger other scrolling events;
2. If the fingers touch the screen and then stop and slide then the Onscroll method is executed first and then the Scroll_state_ Touch_scroll event;
3. If the finger touches the screen and slides directly, the Scroll_state_touch_scroll event is executed for the first time;
3. Trigger Scroll_state_touch_ The SCROLL event also continues to trigger the Onscroll event more than once, rather than directly triggering the Scroll_state_fling event;
4. Scrolling does not necessarily trigger the Scroll_state_fling event, which may be related to the distance of the finger sliding;
5. The Onscroll method is called multiple times during scrolling, and
6. Except that the onscroll triggers multiple times, the other events are triggered only once throughout the process.

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.