1. 程式人生 > >通過getChildAt方法取得AdapterView中第n個Item(重新編輯)

通過getChildAt方法取得AdapterView中第n個Item(重新編輯)

以前對於AdapterView(ListView和GridView的父類)以及這個類的getChildAt()方法理解十分粗淺,以為是指傳入一個絕對position(就是這個item是AdapterView第幾個元素)來直接獲得到該item,當然,android SDK中對於該方法也沒有很具體的說明。這個理解是錯誤的。以簡單的listview為例先說下listview對於item的複用,一個元素很多的listview,如果頁面的可視區域內最多可以看到6個元素,不妨假如它們的index是0-5,那麼在記憶體中只有6個item物件。當第7個item(即index=6)進入到可視區域的時候,那麼其實是在複用index=0的item。由於在滑動時getView這個更新控制元件的方法執行非常頻繁,所以肉眼是絲毫看不出這種複用的。
那麼,接著上面的例子來說,任何情況下,getChildAt(int position)返回的item都是指的可視區域內的第position個元素。下面是一種巧妙和嚴謹的方法可以在任何時候(指的是listView滑動在任意狀態時)根據position取得可視區域內的item,如果該item不在可視區域,則返回null。

private static class ChildViewInfo {

        private Integer firstVisiblePosition = null;
        private Integer childCount = null;

        private Integer headerViewsCount = null;

        private Integer firstVisiblePositionWithOutHeaderViews = null;
        private Integer childCountWithOutHeaderViews = null
; private Integer visibleHeaderViewsCount = null; public ChildViewInfo(AdapterView<Adapter> adapterView) { Adapter adapter = adapterView.getAdapter(); firstVisiblePosition = adapterView.getFirstVisiblePosition(); childCount = adapterView.getChildCount(); if
(null != adapter && adapter instanceof HeaderViewListAdapter) { headerViewsCount = ((HeaderViewListAdapter) adapter).getHeadersCount(); firstVisiblePositionWithOutHeaderViews = firstVisiblePosition - headerViewsCount; if (firstVisiblePositionWithOutHeaderViews < 0) { firstVisiblePositionWithOutHeaderViews = 0; } visibleHeaderViewsCount = headerViewsCount - firstVisiblePosition; if (visibleHeaderViewsCount > 0) { visibleHeaderViewsCount = Math.min(childCount, visibleHeaderViewsCount); childCountWithOutHeaderViews = childCount - visibleHeaderViewsCount; } else { visibleHeaderViewsCount = 0; childCountWithOutHeaderViews = childCount; } } } public View getItemView(int position, boolean withOutHeaderViews) { boolean b = withOutHeaderViews && adapterView.getAdapter() instanceof HeaderViewListAdapter; ChildViewInfo childViewInfo = new ChildViewInfo(adapterView); int firstVisiblePosition = b ? childViewInfo.firstVisiblePositionWithOutHeaderViews : childViewInfo.firstVisiblePosition; int childCount = b ? childViewInfo.childCountWithOutHeaderViews : childViewInfo.childCount; Integer index = getItemViewIndex(firstVisiblePosition, childCount, position); if (null != index) { index = b ? index + childViewInfo.visibleHeaderViewsCount : index; return adapterView.getChildAt(index); } return null; } public static Integer getItemViewIndex(int firstVisiblePosition, int childCount, int position) { int index = position - firstVisiblePosition; int count = childCount; if (index >= 0 && index < count) { return index; } return null; } }

相關推薦

通過getChildAt方法取得AdapterViewnItem(重新編輯

以前對於AdapterView(ListView和GridView的父類)以及這個類的getChildAt()方法理解十分粗淺,以為是指傳入一個絕對position(就是這個item是AdapterView第幾個元素)來直接獲得到該item,當然,android

LintCode—刪除連結串列倒數n節點(174

資料結構—線性結構—連結串列:(刪除連結串列中倒數第n個元素)一、題目:給定一個連結串列,刪除連結串列中倒數第n個節點,返回連結串列的頭節點。(連結串列中的節點個數大於n)樣例:給出連結串列1->

刪除連結串列的倒數N節點(leetcode

給定一個連結串列,刪除連結串列的倒數第 n 個節點,並且返回連結串列的頭結點。 示例: 給定一個連結串列: 1->2->3->4->5, 和 n = 2. 當刪除了倒數第二個節點後,連結串列變為 1->2->3->5. 說

leetcode 19. 刪除連結串列的倒數N節點 (python 進階問題

給定一個連結串列,刪除連結串列的倒數第 n 個節點,並且返回連結串列的頭結點。示例:給定一個連結串列: 1->2->3->4->5, 和 n = 2. 當刪除了倒數第二個節點後,連結串列變為 1->2->3->5. 說明:給定的 n

初級-連結串列-刪除連結串列的倒數N節點(JavaScript

給定一個連結串列,刪除連結串列的倒數第 n 個節點,並且返回連結串列的頭結點。 示例: 給定一個連結串列: 1->2->3->4->5, 和 n = 2. 當刪除了倒數第二個節點後,連結串列變為 1->2->3->5. 說明

C:numberNMax 陣列n大數的下標,方法比較笨,應用二級指標

//求陣列中第n大數的下標 int * numberNMax(int * array, int size, int n) { if (NULL==array) { printf("Pointer is NULL\n"); exit(EXIT_FAILURE);

擷取字串,改變N字元的顏色;自定義方法,oncreat呼叫

用到過很多次,每次在用的時候,思路就會斷線,以此特意寫出來,幫助自己記憶; 修改TextView 中部分文字的顏色 textView = (TextView) findViewById(R.id.textview);  SpannableStringBuilder bui

Java原始碼-N數字k最大值的求解方法

翻開《資料結構》正文第一頁,作者問了個問題:N個數字中的第k個最大值怎麼計算? 作者認為,對於學程式設計兩年的人而言,這個問題應該可以拿下。 事實上,這是我剛學習程式設計,考計算機等級考試3、4級時的基本問題,當時是在別人寫好的C語言程式碼段裡,加上一個巢狀的for迴圈,

編寫程式,要求通過一次遍歷找到單鏈表倒數 n 節點

這是我們實習的第一道題目 1問題描述:要求通過一次遍歷找到單鏈表中倒數第 n 個節點(ps: 不允許使用 雙向連結串列;不允許 修改原始單鏈表;可使用額外的輔助空間,但是輔助空間的數目必須最小,不能和

刪除鏈表的倒數N節點(三種方法實現

from ++ n+1 while end != bsp -- 結點 刪除鏈表的倒數第N個節點 給定一個鏈表,刪除鏈表的倒數第 n 個節點,並且返回鏈表的頭結點。 示例: 給定一個鏈表: 1->2->3->4->5, 和 n = 2. 當刪

C List include list 動態陣列以及取其中N元素的方法

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

n斐波那契數(分別用遞迴和非遞迴兩種方法求解

斐波那契數列指的是這樣一個數列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55……這個數列從第3項開始,每一項都等於前兩項之和。 這裡分別用遞迴和非遞迴的方法實現: 遞迴 #define _CRT_SECURE_NO_WARNINGS 1 #include&l

查詢斐波納契數列 N 個數

題目 查詢斐波納契數列中第 N 個數。 所謂的斐波納契數列是指: 前2個數是 0 和 1 。 第 i 個數是第 i-1 個數和第i-2 個數的和。 斐波納契數列的前10個數字是: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 … 解法 pub

LeetCode刪除連結串列倒數n結點

  Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4-

分治演算法求n元素序列k大的元素

     首先,我們應該設定產生隨機數的序列儲存在陣列中,然後我們應該最容易想到的是排序對吧,做一個降序排序,就很容易找到第k個大的元素。但是用排序演算法的話,時間複雜度最快的也是快速排序O(logn),如果我們使用分治演算法求得話,會得到一個線性的時間複雜度O(n)。分治演

通過find()方法獲取table選中trinput值

點選選中行中其中一個input框,獲取本行中另外一個input欄位值: <input name='' onclick='check(this)'>  function check( o){ //獲取上上級tr 的demo var tr = o.parentNod

刪除連結串列的倒數n節點

給定一個連結串列,刪除連結串列的倒數第n個節點,並且返回連結串列的頭結點。 示例: 給定一個連結串列: 1->2->3->4->5, 和 n = 2. 當刪除了倒數第二個節點後

連結串列-LeetCode19刪除連結串列N節點

給定一個連結串列,刪除連結串列的倒數第 n 個節點,並且返回連結串列的頭結點。 示例: 給定一個連結串列: 1->2->3->4->5, 和 n = 2. 當刪除了倒數第二個節點後,連結串列變為 1->2->3->5. 說明: 給定的&

java:判斷二進位制資料n位是否為1

可以使用位運算來判斷。 &是位的與運算子,是指二進位制數按位“與”的操作, 邏輯與就是兩者都為真的時候才為真,其他真假,假真,假假的運算結果都是假。二進位制寫法如下1&1=1,1&0=0,0&1=0,0&0=0 如果想判斷十進

005_010 Python 選擇序列最小的n元素

程式碼如下: #encoding=utf-8 print '中國' #選擇序列中最小的第n個元素 #如果序列很長 ,而且洗牌很充分 排序的複雜度為logn 下面為O(n)的演算法 最小元素師第0個 獲取第n個最小的 import random def seln(data,n)