Implementation of the Advanced Android UI page load of the ListView

Source: Internet
Author: User



Share the dropdown refresh, which is a very good way to operate the user experience. Sina's meager use is typical of this approach.

There is also a problem, when the user from the network to read the meager time, if all of a sudden load the user unread this will take a long time, resulting in a bad user experience, while the content of a screen is not enough to display so much content. At this point, we need to use another function, that is the page of the ListView. By paging through the data load, the user can see how much to load.

Usually this is also divided into two ways, one is to set a button, the user click to load. The other is automatically loaded when the user slides to the bottom. Today I will share with you the implementation of this feature.

First, write an XML file, Moredata.xml, that defines the view that is 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 that it is a button and a progress bar. Because only a demo is done here, it is easy to handle it by setting the control's visibility, displaying the button when it is not loaded, and displaying the progress bar when loaded.

Write a item.xml, everyone should be very familiar with it. 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 is not posted, the entire main interface is a ListView.

Look directly at the activity code to achieve 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 A    Dapter private Simpleadapter Msimpleadapter;    Private ListView LV;    Private Button BT;    Private ProgressBar PG;    Private arraylist
Comments, it should be easy for everyone to understand. Here's a simple explanation. 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, in this case the button plus ProgressBar view. When the user taps the button, calls the Loadmoredate method, binds more data to the ListView, notifies the ListView refresh by Adapter's Notifydatasetchanged method, and displays the data just added.

This handler asynchronously delays 2 second operation, mimicking the loading process. The ListView also binds the Onscrolllistener listener, and implements the Onscroll and Onscrollstatechanged methods. In the latter method, we know that the ListView has stopped scrolling and the last visible entry equals the adapter entry, knowing that the user has slipped to the bottom and loaded automatically, and that the code has commented out this part of the code, and you can try it yourself.

The code also adds a Maxdatenum variable to record the maximum number of data. That is to say, network or other places altogether data. After the user has finished loading the data, the Onscroll method is used to remove the bottom view of the ListView 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 number of loads. (Load the remaining 5 by default, less than 5).

Look under:

It's so much to write about, but in general it's very simple, but it's a really useful effect.



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.