TAILIEUCHUNG - Lập trình Androi part 11

Các getView () thực hiện được hiển thị trong các công trình phần trước, nhưng nó không hiệu quả. Mỗi lần các cuộn người dùng, chúng ta phải tạo ra một loạt các đối tượng mới Xem để chứa các hàng vừa được hiển thị. Điều này là xấu. | CHAPTER 8 Getting Fancy with Lists Better. Stronger. Faster. The getView implementation shown in the preceding section works but it s inefficient. Every time the user scrolls we must create a bunch of new View objects to accommodate the newly shown rows. This is bad. It might be bad for the immediate user experience if the list appears to be sluggish. More likely though it will be bad due to battery usage every bit of CPU that is used eats up the battery. This is compounded by the extra work the garbage collector needs to do to get rid of all those extra objects we create. So the less efficient our code the more quickly the phone s battery will be drained and the less happy the user will be. And we want happy users right So let s take a look at a few tricks to make your fancy ListView widgets more efficient. Using convertView The getView method receives as one of its parameters a View named by convention convertView. Sometimes convertView will be null. In those cases you need to create a new row View from scratch . via inflation just as in the previous example. However if convertView is not null then it is actually one of your previously created View objects. This will happen primarily when the user scrolls the ListView. As new rows appear Android will attempt to recycle the views of the rows that scrolled off the other end of the list to save you from needing to rebuild them from scratch. Assuming that each of your rows has the same basic structure you can use findViewById to get at the individual widgets that make up your row and change their contents and then return convertView from getView rather than create a whole new row. For example here is the getView implementation from the previous example now optimized via convertView from the FancyLists Recycling project public class RecyclingDemo extends ListActivity TextView selection String items lorem ipsum dolor sit amet consectetuer adipiscing elit morbi vel ligula vitae arcu aliquet mollis etiam vel erat .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.