Kết quả 1 đến 7 của 7
  1. #1
    Ngày tham gia
    Jan 2015
    Bài viết
    0

    Cần giải thích : getTag() và setTag() trong viewHolder > listview

    bác nào hiểu về viewholder giải thích mình với.
    mấy cái phương thức gettag và settag là để làm gì vậy.
    sao mình dùng viewholder, bắt click tại các item thì nó trả về position loạn hết cả lên vậy.

  2. #2
    Ngày tham gia
    Sep 2014
    Bài viết
    0
    bác nào giải thích cho e phát.

  3. #3
    Ngày tham gia
    Oct 2014
    Bài viết
    0
    theo ý hiểu của mình (cá nhân thôi nhóe, còn chính xác hoàn toàn ko thì ko rõ vì mình được hướng dẫn làm và cũng làm nhiều với nó nên nghĩ như thế) thì như này:
    - viewholder dùng để lưu giữ lại các thành phần của 1 view, ví dụ 1 view có các thành phần như textview, button....
    - mục đích của việc lưu giữ này là để thay đổi nội dung hiển thị của view mà tránh phải khởi tạo lại các thành phần. bạn để ý trong hàm getview sẽ thấy như thế này nhé:
    + nếu như convertview == null thì khi đó sẽ phải tạo mới view (inflate file layout), đồng thời tạo mới viewholder. sau đó sẽ settag cho cái convertview này là cái holder vừa tạo mới (settag)
    + nếu convertview != null thì chỉ việc lấy tag đó ra (gettag) mà ko cần phải findviewbyid nữa
    sau khi có cái viewholder rồi (tạo mới hoặc get) lúc đó ta sẽ set nội dung hiển thị cũng như sự kiện cho các thành phần đó

    hay nói một cách gần chính xác hơn đây chính là caching view
    theo một số giải thích trên mạng nhóe:
    "viewholder is a pattern used for avoiding the calling of "findviewbyid" every time the getview() method is called. imagine that you have a list with hundreds of rows, the "findviewbyid" will be called hundreds of times and it's not necessary, just bad for performance. so to increase the performance try to use the viewholder pattern as much as possible." -- http://myandroidsolutions.blogspot.com/2012/07/android-listview-tutorial.html

    "
    6.3. holder pattern
    the view holder pattern allows to avoid the findviewbyid() method call for a reused convertview.
    a viewholder class is a static inner class in your adapter which hold references to the relevant views in your layout. this reference is assigned to the view which represent the row layout as a tag via the settag() method.
    if we receive a convertview object, we can get the instance of the viewholder via the gettag() method and assign the new attributes to the views via the viewholder reference.
    while this sounds complex this is approx. 15 % faster then using the findviewbyid() method."
    --- http://www.vogella.com/articles/androidlistview/article.html

    "when the list is too big, containing lots of rows, then the calling of ‘findviewbyid()‘ everytime will impact slightly to your application performance, even though, it’s still working fine; however, it’s quite risky since it might be dead at some unexpected point.
    this is one of most common problem, it is! hence, from somewhere, the idea of viewholderpattern was created to improve this current situation.
    the viewholder object is static, once created, it will cache all things inside. therefore, we can avoid calling ‘findviewbyid()‘ on resource everytime." -- http://xjaphx.wordpress.com/2011/06/16/viewholder-pattern-caching-view-efficiently/

    hi vọng câu trả lời loằng ngoằng trên của mình giúp ích được phần nào cho bạn

  4. #4
    Ngày tham gia
    Jan 2015
    Bài viết
    0
    - theo long các bạn nên nghĩ đơn giản 1 tí. đơn giản nó là 1 chuỗi string. trong các class của các thư viện, hầu như các bạn sẽ luôn thấy 1 thuộc tính tag là string và 2 phương thức set() và get() nó.
    - và đây thường là mục đích cuối cùng của việc này:
    log.v(gettag(), "xem 1 giá trị nào đó");
    - giải thích thêm 1 tí là trong 1 đống source lớn, việc kiểm tra gía trị là khó, và thông thường người ta không biết bắn giá trị ra ở đâu để kiểm tra. hơn nữa trong 1 mảng đối tượng lại càng khó hơn, và cách phân biết duy nhất giữa các đối tượng trong mảng đối tượng chính là nó. khi tạo đối tượng thì đồng thời tạo luôn tag cho nó ví dụ như ("tagdoituonga1","tagdoituonga2", "tagdoituonga3"...) và giả xử như có đối tượng nào bị remove khỏi mảng và mảng tự sắp xếp lại chỉ số thì ta vẫn có thể nhận diện nó qua tag ... hết .

  5. #5
    Ngày tham gia
    Apr 2015
    Bài viết
    0
    mình cũng gặp vấn đề với viewholder. sau một hồi tìm hiểu, cảm thấy cũng thấy có vẻ hiểu hơn về vấn đề này nên muốn chia sẻ thêm cho mọi người.

    đầu tiên xem xét một custom adapter không có view holder



    Mã nguồn PHP:
    [color=#000000]
    [/color][color=#007700]public [/color][color=#0000bb]view getview[/color][color=#007700]([/color][color=#0000bb]int position[/color][color=#007700], [/color][color=#0000bb]view convertview[/color][color=#007700], [/color][color=#0000bb]viewgroup parent[/color][color=#007700]) { [/color][color=#0000bb]layoutinflater inflater [/color][color=#007700]= ([/color][color=#0000bb]layoutinflater[/color][color=#007700]) [/color][color=#0000bb]context [/color][color=#007700].[/color][color=#0000bb]getsystemservice[/color][color=#007700]([/color][color=#0000bb]context[/color][color=#007700].[/color][color=#0000bb]layout_inflater_service[/color][color=#007700]); [/color][color=#0000bb]view rowview [/color][color=#007700]= [/color][color=#0000bb]inflater[/color][color=#007700].[/color][color=#0000bb]inflate[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]layout[/color][color=#007700].[/color][color=#0000bb]rowlayout[/color][color=#007700], [/color][color=#0000bb]parent[/color][color=#007700], [/color][color=#0000bb]false[/color][color=#007700]); [/color][color=#0000bb]textview textview [/color][color=#007700]= ([/color][color=#0000bb]textview[/color][color=#007700]) [/color][color=#0000bb]rowview[/color][color=#007700].[/color][color=#0000bb]findviewbyid[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]id[/color][color=#007700].[/color][color=#0000bb]label[/color][color=#007700]); [/color][color=#0000bb]imageview imageview [/color][color=#007700]= ([/color][color=#0000bb]imageview[/color][color=#007700]) [/color][color=#0000bb]rowview[/color][color=#007700].[/color][color=#0000bb]findviewbyid[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]id[/color][color=#007700].[/color][color=#0000bb]icon[/color][color=#007700]); [/color][color=#0000bb]textview[/color][color=#007700].[/color][color=#0000bb]settext[/color][color=#007700]([/color][color=#0000bb]values[/color][color=#007700][[/color][color=#0000bb]position[/color][color=#007700]]); [/color][color=#0000bb]imageview[/color][color=#007700].[/color][color=#0000bb]setimageresource[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]drawable[/color][color=#007700].[/color][color=#0000bb]img[/color][color=#007700]); return [/color][color=#0000bb]rowview[/color][color=#007700]; }[/color] 
    ta có thể thấy mỗi khi getview được gọi thì các công việc phải thực hiện như sau:
    - inflate layout của list item - ở trên là rowview
    - findviewbyid() để lấy các view thành phần của rowview cần set.
    - set các view trên
    - return rowview


    bây giờ xem xét một custom adapter có view holder



    Mã nguồn PHP:
    [color=#000000]
    [/color][color=#007700]public [/color][color=#0000bb]view getview[/color][color=#007700]([/color][color=#0000bb]int position[/color][color=#007700], [/color][color=#0000bb]view convertview[/color][color=#007700], [/color][color=#0000bb]viewgroup parent[/color][color=#007700]) { [/color][color=#0000bb]view rowview [/color][color=#007700]= [/color][color=#0000bb]convertview[/color][color=#007700]; [/color][color=#ff8000]// reuse views [/color][color=#007700]if ([/color][color=#0000bb]rowview [/color][color=#007700]== [/color][color=#0000bb]null[/color][color=#007700]) { [/color][color=#0000bb]layoutinflater inflater [/color][color=#007700]= [/color][color=#0000bb]context[/color][color=#007700].[/color][color=#0000bb]getlayoutinflater[/color][color=#007700](); [/color][color=#0000bb]rowview [/color][color=#007700]= [/color][color=#0000bb]inflater[/color][color=#007700].[/color][color=#0000bb]inflate[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]layout[/color][color=#007700].[/color][color=#0000bb]rowlayout[/color][color=#007700], [/color][color=#0000bb]null[/color][color=#007700]); [/color][color=#ff8000]// configure view holder [/color][color=#0000bb]viewholder viewholder [/color][color=#007700]= new [/color][color=#0000bb]viewholder[/color][color=#007700](); [/color][color=#0000bb]viewholder[/color][color=#007700].[/color][color=#0000bb]text [/color][color=#007700]= ([/color][color=#0000bb]textview[/color][color=#007700]) [/color][color=#0000bb]rowview[/color][color=#007700].[/color][color=#0000bb]findviewbyid[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]id[/color][color=#007700].[/color][color=#0000bb]textview01[/color][color=#007700]); [/color][color=#0000bb]viewholder[/color][color=#007700].[/color][color=#0000bb]image [/color][color=#007700]= ([/color][color=#0000bb]imageview[/color][color=#007700]) [/color][color=#0000bb]rowview [/color][color=#007700].[/color][color=#0000bb]findviewbyid[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]id[/color][color=#007700].[/color][color=#0000bb]imageview01[/color][color=#007700]); [/color][color=#0000bb]rowview[/color][color=#007700].[/color][color=#0000bb]settag[/color][color=#007700]([/color][color=#0000bb]viewholder[/color][color=#007700]); } [/color][color=#ff8000]// fill data [/color][color=#0000bb]viewholder holder [/color][color=#007700]= ([/color][color=#0000bb]viewholder[/color][color=#007700]) [/color][color=#0000bb]rowview[/color][color=#007700].[/color][color=#0000bb]gettag[/color][color=#007700](); [/color][color=#0000bb]string s [/color][color=#007700]= [/color][color=#0000bb]names[/color][color=#007700][[/color][color=#0000bb]position[/color][color=#007700]]; [/color][color=#0000bb]holder[/color][color=#007700].[/color][color=#0000bb]text[/color][color=#007700].[/color][color=#0000bb]settext[/color][color=#007700]([/color][color=#0000bb]s[/color][color=#007700]); [/color][color=#0000bb]holder[/color][color=#007700].[/color][color=#0000bb]image[/color][color=#007700].[/color][color=#0000bb]setimageresource[/color][color=#007700]([/color][color=#0000bb]r[/color][color=#007700].[/color][color=#0000bb]drawable[/color][color=#007700].[/color][color=#0000bb]ok[/color][color=#007700]); return [/color][color=#0000bb]rowview[/color][color=#007700]; }[/color] 
    ta có thể thấy sự khác biệt giữa 2 trường hợp này như sau:
    - nếu convertview == null thì ta cần phải instance một object mới cho viewholder và thực hiện hai bước đầu tiên giống như trên, sau đó set tag cho viewholder. việc settag cho viewholder giống như bạn đánh một cái unique index cho view đó.
    - trong trường hợp convertview != null thì chỉ việc get tag cho viewholder. từ đó ta có thể lấy được cái view thành phần mà không cần phải gọi lại các hàm findviewbyid.
    - như gabk đã nói ở trên




    mục đích của việc lưu giữ này là để thay đổi nội dung hiển thị của view mà tránh phải khởi tạo lại các thành phần.
    ta có thể tránh được việc gọi liên tục hàm findviewbyid();

    thực ra thì findviewbyid() cũng không tốn quá nhiều thời gian nếu app bạn nhỏ, nhưng vấn đề sẽ nghiêm trọng hơn nếu row layout mà bạn sử dụng khá phức tạp và số lượng row là lớn.

  6. #6
    Ngày tham gia
    Jan 2015
    Bài viết
    0
    diễn đàn này tiasangmoi92 viết bài dễ hiểu nhất, gần gũi thân thiện hơn

  7. #7
    Ngày tham gia
    Mar 2015
    Bài viết
    0
    với những gì mình được biết:
    1. tag: chỉ đơn giản để lưu 1 object nào đó
    2. set + get tag: với sự kiện của 1 vài component trong 1 row item của 1 adapter (ví dụ của 1 button), có thể nó không nhận diện đúng position của item mà bạn đã click; giải pháp ở đây là bạn settag cho view = position, để lúc set event, bạn gettag và xử lý cho đúng (tất nhiên bạn có thể lưu giá trị khác)
    3. về viewholder, bạn gabk đã nói khá rõ

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •