2013-08-27 33 views
17

Đối với một ứng dụng tôi đang làm việc trên, tôi đang cố gắng để có được những đứa trẻ của một ListView. Để thực hiện việc này, tôi có đoạn mã sau:Lấy ListView trẻ em không có trong xem

View listItem = mListView.getChildAt(i); 

Tuy nhiên, điều này chỉ phù hợp với trẻ em đang xem. Tôi cũng cần phải tiếp cận với những đứa trẻ không có trong tầm nhìn. Làm thế nào tôi sẽ làm điều này?

EDIT:

So sánh các phương pháp đề nghị một trong những tôi đã được sử dụng tôi thấy như sau:

RelativeLayout listItem1 = (RelativeLayout) mListView.getAdapter().getView(i, null, mListView); 
RelativeLayout listItem2 = (RelativeLayout) mListView.getChildAt(i); 
Log.d("Checks", "listItem1: " + listItem1); 
Log.d("Checks", "listItem2: " + listItem2); 

08-27 14:16:56.628: D/Checks(15025): listItem1: [email protected] 
08-27 14:16:56.628: D/Checks(15025): listItem2: [email protected] 
08-27 14:16:56.628: D/Checks(15025): listItem1: [email protected] 
08-27 14:16:56.628: D/Checks(15025): listItem2: [email protected] 
08-27 14:16:56.638: D/Checks(15025): listItem1: [email protected] 
08-27 14:16:56.638: D/Checks(15025): listItem2: [email protected] 
08-27 14:16:56.638: D/Checks(15025): listItem1: [email protected] 
08-27 14:16:56.638: D/Checks(15025): listItem2: [email protected] 
08-27 14:16:56.648: D/Checks(15025): listItem1: [email protected] 
08-27 14:16:56.648: D/Checks(15025): listItem2: [email protected] 
08-27 14:16:56.648: D/Checks(15025): listItem1: [email protected] 
08-27 14:16:56.648: D/Checks(15025): listItem2: [email protected] 

điểm listItem2 đến Xem thực tế trong ListView, đó là những gì tôi muốn ngoại trừ việc nó chỉ hoạt động đối với các Chế độ xem có trong tầm nhìn. Bạn có thể thấy từ tệp nhật ký listItem1 và listItem2 không tương ứng.

EDIT 2:

tôi đã đưa ra như sau:

for (int i = 0; i < mListView.getCount(); i++) { 
    RelativeLayout listItem = (RelativeLayout) mListView.getAdapter().getView(i, mListView.getChildAt(i), mListView); 
} 

này trả Views một cách chính xác cho tất cả các mục có thể nhìn thấy trong danh sách. Tuy nhiên, nó trả về một cái nhìn khác nhau cho những cái mà không có trong tầm nhìn.

EDIT 3:

Nhờ nfirex Tôi có một câu trả lời làm việc. Điều này sẽ trở lại xem ngay cả khi họ không trực tiếp trong tầm nhìn:

public View getViewByPosition(int position, ListView listView) { 
    final int firstListItemPosition = listView.getFirstVisiblePosition(); 
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1; 

    if (position < firstListItemPosition || position > lastListItemPosition) { 
     return listView.getAdapter().getView(position, listView.getChildAt(position), listView); 
    } else { 
     final int childIndex = position - firstListItemPosition; 
     return listView.getChildAt(childIndex); 
    } 
} 

Trả lời

33

Bạn cần phương pháp Adapter.getView():

final View view = mListView.getAdapter().getView(position, null, mListView); 

UPDATE:

Bạn cần phải tạo phương pháp của bạn. Một cái gì đó như thế này:

public View getViewByPosition(int position, ListView listView) { 
    final int firstListItemPosition = listView.getFirstVisiblePosition(); 
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1; 

    if (position < firstListItemPosition || position > lastListItemPosition) { 
     return listView.getAdapter().getView(position, null, listView); 
    } else { 
     final int childIndex = position - firstListItemPosition; 
     return listView.getChildAt(childIndex); 
    } 
} 
+2

Điều này thực sự trả về Chế độ xem. Tuy nhiên, tôi nghĩ nó trả về một cái nhìn mới thay cho cái nhìn thực tế mà tôi muốn. – Zero

+0

@Zero Chế độ xem nào bạn cần truy xuất? Phương thức ListView.getChildAt (i) sẽ trả về con của nó (nó là phương thức của ViewGroup), được lấy từ bộ điều hợp. Adapter.getView (pos, oldView, parent) sẽ trả về dạng xem mới (nếu oldView == null) hoặc oldView với dữ liệu chứa trong Adapter.getItem (pos) theo vị trí của phần tử trong danh sách dữ liệu của bạn. Bạn muốn nhận các lượt xem cũng giữ trong ListView theo chỉ mục? – nfirex

+1

Tôi đang cố gắng để có được View mà tôi cũng nhận được với getChildAt (i), nhưng đối với Views không có trong tầm nhìn (nếu tôi có 8 mục trên màn hình và tôi yêu cầu getChildAt (10), sẽ ở đó nếu tôi cuộn xuống, tôi nhận được NullPointerException) – Zero

2

Bạn có thể sử dụng mã này

private static void initRecyclerBin(AbsListView view) { 
     try { 
      Field localField = AbsListView.class.getDeclaredField("mRecycler"); 
      localField.setAccessible(true); 
      Object recyclerBin = localField.get(view); 
      Class<?> clazz = Class.forName("android.widget.AbsListView$RecycleBin"); 
      Field scrapField = clazz.getDeclaredField("mScrapViews"); 
      scrapField.setAccessible(true); 
      ArrayList<View>[] scrapViews; 
      scrapViews = (ArrayList<View>[]) scrapField.get(recyclerBin); 
      if (null != scrapViews) { 
       int length = scrapViews.length; 
       for (int i = 0, count = 0; i < length; i++) { 
        if (null != scrapViews[i] && !scrapViews[i].isEmpty()) { 
         for (int j = 0; j < scrapViews[i].size(); j++) { 
          scrapViews[i].get(j);//this is the scrapViews 
         } 
        } 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

và ListView getChildViews().

Các vấn đề liên quan