ListView page display (reproduced + modified), listview page display

Source: Internet
Author: User

ListView page display (reproduced + modified), listview page display

Through practice, it is found that the "Previous Page" button and "next page" are still largely judged to be available. The problem is:

Original code:

Public void checkButton () {// the index value is smaller than or equal to 0, indicating that you cannot flip the page forward to the first page. // Set the button for turning the page forward to unavailable. If (index <= 0) {btnLeft. setEnabled (false);} // The length of the value minus the length of the first few pages. The rest is the length of this page. If the length of this page is smaller than View_Count, it indicates that this is the last page and there is no more. // Set the button for turning back to unavailable. Else if (data. length-index * VIEW_COUNT <= VIEW_COUNT) {btnRight. setEnabled (false) ;}// otherwise, set both buttons to available. Else {btnLeft. setEnabled (true); btnRight. setEnabled (true );}

The original code has a bug in two cases:

1. A problem occurs when the data length is <VIEW_COUNT

2. When the data length is <2 * VIEW_COUNT (only two pages can be displayed), the problem may also occur.

Code after modification:

Public void checkButton () {// when this page is the first page (index = 0), when the number of data entries is less than the number displayed on each page, if (index <= 0 & data. length <= VIEW_COUNT) {btnLeft. setEnabled (false); btnRight. setEnabled (false); // when this page is the first page (index = 0) and the number of data entries is greater than the number displayed on each page, the previous page button cannot be used, the next page button can use} else if (index <= 0 & data. length> VIEW_COUNT) {btnLeft. setEnabled (false); btnRight. setEnabled (true);} // The length of the value minus the length of the first few pages. The rest is the length of this page. If the length of this page is smaller than View_Count, it indicates that this is The last page is gone. // Set the button for turning back to unavailable. Else if (index> 0 & (data. length-index * VIEW_COUNT) <= VIEW_COUNT) {btnRight. setEnabled (false); btnLeft. setEnabled (true);} // otherwise, set both buttons to available. Else {btnLeft. setEnabled (true); btnRight. setEnabled (true );}

After the modification is complete, no problem will occur (in the project code to be released, there are two cases of data initialization. You can click the annotations and run them to know where the problem is)

Note the following when writing code into the project:

1. When detecting the availability of the button for the first time, place the checkButton () under the code that binds the adapter to the listview.

2. When two buttons are added under the Listview interface, it is easy for listView to overwrite the button. To solve this problem, the solution will be written in the blog below.

3. when there is a button to return to the previous page for reference in the project, in The onClick () event, put a sentence: index = 0; in this way, when you click this page again, the data is refreshed and the first page is displayed. Otherwise, the data will stay on the previous page!

I want to know how to upload a project ?? Why can't I find a location?


How to display listview by PAGE

Do you want to perform paging loading when the listview is pulled down, or trigger the start button to perform paging loading (if there is code on the open-source Chinese community, you can check it .)
 
In android l, how does one achieve re-paging display based on the original Listview?

I will not write the code to help you understand the working principle of listView.
You know, if you want it to work normally, the most important thing is to set the Adapter, right? So you can see that the main logic in the Adapter is related to the data source,
The Adapter will ask you the most important questions:
1. Total number of rows, 2. What is the layout of each row? 3. Where is the data array.

Once you understand the principle, you can return to your needs. Your array is obtained from the Internet, and there may be many, so you can split this large array into several small arrays, next, you can change the current array every time the next page is displayed. It will automatically help you refresh the array.

Related Article

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.