Original address: http://blog.csdn.net/liuhe688/article/details/6852523
be familiar with Android As you all know, no matter the micro-blog client or the news client, it can not be separated from the list component. It can be said that the list component is the most important component in Android data presentation. Today we will talk about the list component ListView loading data related content. Generally speaking, when an application presents a large amount of data, it will not present all available data to users, because this is not a small pressure for both the server and the client. Therefore, many applications use batch loading to obtain the data users need. For example, the microblog client may automatically load the next page of data when the user slides to the bottom of the list, or it may place a "Load More" button at the bottom to load the next page of data after the user clicks.
Let's show you how to use ListView to get data with examples today.
Create a new loadmore project. Let's take a look at the structure diagram and the final effect diagram.
The left figure contains three layout files, an Adapter and an Activity, and the right one is our main interface after running.
The main.xml is the layout file of the main interface, which contains a ListView component. The code is as follows:
Here we refer to Android's built-in id called list, because we're going to use ListActivity later, and our MainActivity inherits it.
Then there is list_item.xml, which is the layout file of a single list item in ListView. As you can see from the rendering, only one TextView component is used here. The list_item.xml code is as follows:
We noticed that there is a button at the bottom of the list in the right figure that is different from other list items. What is the situation? In fact, this button is a view we added at the bottom of ListView. The ListView component provides two very useful functions: you can add custom views at the top and bottom. Here we add a view at the bottom of ListView to load more data. This view corresponds to the load_more.xml layout file. The code is as follows:
Next, let's look at our Adapter, ListViewAdapter code as follows:
This ListView Adapter is our custom adapter, which inherits from BaseAdapter. To instantiate this adapter, we need a Context object to get a LayoutInflate instance and a collection object to act as the data set of the adapter. In the getView method, we fill in the list_item.xml layout file to complete the data display of each item in the list. The addItem method is used to add new data to the data set when loading data.
Finally, let's take a look at MainActivity:
As shown in the code, when the onCreate method is invoked, we get the listView component and set its bottom view to loadMoreView, which contains a button that triggers the loadMore method call when clicked. In addition, when the adapter is set up for the listView, we set a sliding event listener for it. When the list is sliding, onScroll is tuned. With this, onScrollStateChanged is invoked when the sliding state changes.
Let's demonstrate the loading process:
As shown in the figure, when the button is clicked, the loading action occurs. 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 Click on the bottom of the sliding list and release it. The console prints as follows:
We see the code executed in the if statement in the onScrollStateChanged method, so if we want to load automatically, we can put the load code here.
Today I'm going to start with this. Thank you.