In Android applications, listview uses onscrolllistener to page load data

When the user reads the microblog from the network, if all the microblog unread by the user is loaded at once, it will take a long time, resulting in a bad user experience. At the same time, the content of one screen is not enough to display so much content. At this time, we need to use another function, that is, the paging of listview. Load data by paging, and users can load as much as they see.

Usually, this is also divided into two methods. One is to set a button, which can be loaded when the user clicks. The other is automatically loaded when the user slides to the bottom. Today I will share with you the implementation of this function.

First, write an XML file, moredata.xml, which defines the view at the bottom of the listview:

You can see a button and a progress bar. Because there is only one demonstration, it is easy to handle here. By setting the visibility of the control, the button is displayed when it is not loaded, and the progress bar is displayed when it is loaded.

Write an item.xml, which you should be familiar with. The view used to define each item of the listview.

Main.xml will not be posted, and the whole main interface will have a listview.

Directly look at the code of the activity to realize the paging effect.

Through the notes, it should be easy for everyone to understand. Here is a simple analysis. 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 listview. In this case, it is the view with button and ProgressBar. When the user clicks the button, call the loadmoredate method to bind more data for the listview, and notify the listview to refresh through the notifydatasetchanged method of the adapter to display the newly added data.

Here, the handler is used to asynchronously delay the operation for 2 seconds to simulate the loading process. At the same time, listview binds the onscrolllistener listener listener, and implements the onscroll and onscrollstatechanged methods. In the latter method, by judging that listview has stopped scrolling and the last visible item is equal to the item of adapter, we can know that the user has slid to the bottom and loaded automatically. This part of the code has been commented out in the code. You can try it yourself.

The code also adds a maxdatenum variable to record the maximum amount of data. That is to say, the data shared by the network or other places. After the onscroll method is used to judge that the user has loaded these data, remove the bottom view of listview and stop loading. At the same time, the loadmoredate method also performs corresponding operations on the maximum data volume to judge the loading quantity. (5 items are loaded by default, and the remaining items are loaded when less than 5 items are loaded).

Look at the renderings below:

@H_ 419_ 43@

Brief analysis on the sequence and times of onscrolllistener events in the whole event of onscrolllistener on Android, I mainly analyze its execution sequence: implement the listening interface for rolling events

I was always confused before. Later, after careful testing, I came to the conclusion in the code comments above. In addition, the scrolling of the listview picture list should be in scroll_ STATE_ Make the picture not displayed during flying, improve the scrolling performance and make the scrolling smoother, scroll_ STATE_ When idle, the picture visible on the current screen is displayed. It is basically not used for onscroll () interface method. Supplement: 1. When the finger only touches the screen without pulling, the onscroll method will only be triggered once, and other scrolling events will not be triggered; 2. If your finger stops after touching the screen and then slides, execute onscroll method first, and then scroll_ STATE_ TOUCH_ Scroll event; 3. If the finger directly slides after touching the screen, execute scroll for the first time_ STATE_ TOUCH_ Scroll event; 3. Trigger scroll_ STATE_ TOUCH_ After the scroll event, the onscroll event will continue to be triggered multiple times instead of directly triggering the scroll event_ STATE_ Fling event; 4. Scroll may not be triggered after scrolling_ STATE_ Fling event may be related to the distance of finger sliding; 5. Onscroll method will be called many times during scrolling; 6. Except onscroll triggered multiple times, other events will only be triggered once in the whole process.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>