Implementation of listview paging loading data function 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:

Where, main XML is the layout file of the main interface. It 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_ item. The XML code 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 learn about our adapter. The 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_ item. XML layout file to display the data 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:

That's all for today. Thank you.

Original link: http://blog.csdn.net/liuhe688/article/details/6852523 The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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