How to load data by page in listview in Android

Friends familiar with Android know that both microblog client and news client are inseparable from the list component. It can be said that the list component is the most important component in Android data display. Today, we will talk about the relevant contents of the data loaded by the list component listview. Generally speaking, when an application displays a large amount of data, it will not present all the available data to the user, because this is a great pressure for both the server and the client. Therefore, many applications use batch loading to obtain the data required by the user. For example, the microblog client may automatically load the data of the next page when the user slides to the bottom of the list, or a "load more" button may be placed at the bottom. After the user clicks, the data of the next page will be loaded. Let's demonstrate the process of using listview to obtain data with an example today. Create a new loadmore project. Let's take a look at the structure diagram and final effect diagram:

The left figure contains three layout files, an adapter and an activity. The right figure is the main interface after we run. Among them, main.xml is the layout file of the main interface, which contains a listview component. The code is as follows:

Here we refer to the Android built-in ID named list, because we will use listactivity later, and our mainactivity inherits from it. Then there is the list_ Item.xml, which is the layout file of a single list item in the listview. As can be seen from the rendering, only one textview component, list, is used here_ The code of item.xml is as follows:

We notice that there is a button at the bottom of the list in the right figure, which is different from other list items. What's the situation? In fact, this button is a view we added at the bottom of the listview. Listview component provides two practical functions, that is, you can add custom views at the top and bottom. We add a view at the bottom of the listview to load more data. This view corresponds to load_ More.xml layout file, the code is as follows:

Next, let's take a look

Our listviewadapter code is as follows:

This listviewadapter is our custom adapter, which inherits from baseadapter. Instantiating this adapter requires a context object to obtain the layouteinflator instance and a collection object to act as the data set of the adapter; In the getview method, we populate the list_ The item.xml layout file completes the data display of each item in the list; The addItem method is used to add new data to the dataset when loading data. Finally, let's take a look at mainactivity:

As shown in the code, when the oncreate method is called, we get the listview component and set its bottom view as loadmoreview, which contains a button. When clicked, the loadmore method call will be triggered. In addition, when the adapter is set for listview, we set a sliding event listener for it. Onscroll will be called when sliding the list, Onscrollstatechanged is called when the sliding state changes. Let's demonstrate the loading process:

As shown in the figure, after clicking the button, the loading action appears. After loading, as shown in the right figure, the new data is immediately after the original data. Then we slide to the bottom and the load button still works:

Finally, we test the sliding list to the bottom, then release it, and the console prints as follows:

@H_ 403_ 65@

We see that the code in the if statement in the onscrollstatechanged method is executed, so if we want to load automatically, we can put the loaded code here. That's all for today. Thank you.

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
分享
二维码
< <上一篇
下一篇>>