[java] 사용자 정의 listview 어댑터 getView 메소드가 여러 번 호출되며 일관된 순서로 호출되지 않습니다.

사용자 정의 목록 어댑터가 있습니다.

class ResultsListAdapter extends ArrayAdapter<RecordItem> {

재정의 된 ‘getView’메서드에서 위치를 확인하고 위치가 convertView인지 여부를 확인하기 위해 인쇄합니다.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("getView " + position + " " + convertView);

이것의 출력 (목록이 처음 표시 될 때 아직 사용자 입력이 없음)

04-11 16:24:05.860: INFO/System.out(681): getView 0 null  
04-11 16:24:29.020: INFO/System.out(681): getView 1 android.widget.RelativeLayout@43d415d8  
04-11 16:25:48.070: INFO/System.out(681): getView 2 android.widget.RelativeLayout@43d415d8  
04-11 16:25:49.110: INFO/System.out(681): getView 3 android.widget.RelativeLayout@43d415d8  
04-11 16:25:49.710: INFO/System.out(681): getView 0 android.widget.RelativeLayout@43d415d8  
04-11 16:25:50.251: INFO/System.out(681): getView 1 null  
04-11 16:26:01.300: INFO/System.out(681): getView 2 null  
04-11 16:26:02.020: INFO/System.out(681): getView 3 null  
04-11 16:28:28.091: INFO/System.out(681): getView 0 null  
04-11 16:37:46.180: INFO/System.out(681): getView 1 android.widget.RelativeLayout@43cff8f0  
04-11 16:37:47.091: INFO/System.out(681): getView 2 android.widget.RelativeLayout@43cff8f0  
04-11 16:37:47.730: INFO/System.out(681): getView 3 android.widget.RelativeLayout@43cff8f0  

AFAIK, 명시 적으로 찾을 수는 없지만 getView ()는 보이는 행에 대해서만 호출됩니다. 내 앱은 4 개의 보이는 행으로 시작하기 때문에 적어도 0-3에서 순환하는 위치 번호가 의미가 있습니다. 그러나 나머지는 엉망입니다.

  • 왜 각 행마다 getview가 세 번 호출됩니까?
  • 아직 스크롤하지 않았을 때 이러한 convertView는 어디에서 오는가?

나는 약간의 재교육을 받았으며 좋은 대답을 얻지 못한 채 사람들 이이 문제를 레이아웃 문제와 관련시키고 있음을 알았습니다. 따라서 목록이 포함 된 레이아웃은 다음과 같습니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    android:orientation="vertical" >

    <TextView android:id="@+id/pageDetails"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />

    <ListView android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:drawSelectorOnTop="false" />

</LinearLayout>

각 개별 행의 레이아웃 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="108dp"    
android:padding="4dp">

<ImageView
    android:id="@+id/thumb"        
    android:layout_width="120dp"
    android:layout_height="fill_parent"        
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp"        
    android:src="@drawable/loading" />

<TextView  
    android:id="@+id/price"
    android:layout_width="wrap_content"
    android:layout_height="18dp"         
    android:layout_toRightOf="@id/thumb"
    android:layout_alignParentBottom="true"       
    android:singleLine="true" />

<TextView  
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="18dp"         
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true" 
    android:paddingRight="4dp"       
    android:singleLine="true" />

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="17dp" 
    android:layout_toRightOf="@id/thumb"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:paddingRight="4dp"   
    android:layout_alignWithParentIfMissing="true"
    android:gravity="center" />

</RelativeLayout>

시간 내 주셔서 감사합니다



답변

이것은 문제가 아니며, getView()몇 번이나 호출 될지 에 대한 보장은 없습니다 . 당신의 특별한 경우에 당신은 ListView그것을 제공함으로써 가능한 최악의 일 을하고 있습니다 height=wrap_content. 이렇게 ListView하면 레이아웃 타임에 어댑터에서 몇 개의 자식을 측정하여 크기가 얼마나 큰지 알 수 있습니다. 이것은 스크롤하기 전에 전달 된 것을 제공 ListView하는 convertViewsgetView()입니다.


답변

목록보기 match_parentlayout_height속성을 사용해보십시오 . 그것은 방지 할 수 getView()너무 자주 호출 할 수 있습니다.


답변

layout_width와 layout_height를 match_parent로 변경했을 때이 문제를 제거했습니다 (layout_height 만 변경해도 도움이되지 않음).


유용한 참고 사항 중첩 된 항목이 있는지 확인하십시오. “가장 높은”것을 match_parent 로 변경해야합니다 . 그것이 누군가를 돕기를 바랍니다.


답변

“왜”질문에 대답 할 수는 없지만 ” ListView items repeating “문제 (스크린 높이보다 더 높은 항목이있는 경우) 에 대한 문제를 해결할 수있는 솔루션 이 있습니다.

위의 많은 사람들이 언급했듯이 ListVew 태그 의 android : layout_height 속성을 fill_parent로 유지하십시오 .

getView () 함수에 대한 해결책은 ViewHolder 라는 정적 클래스 를 사용하는 입니다. 예를 확인하십시오 . ur 배열 또는 ArrayCollection의 모든 항목을 추가하는 작업을 성공적으로 수행합니다.

이것이 친구를 돕기를 바랍니다!

안부 인사, Siddhant


답변

질문 : 어댑터가 getView ()를 여러 번 호출하는 이유는 무엇입니까? 답변 : 스크롤 할 때 Listview가 렌더링 될 때 어댑터가 getView ()를 호출하여보기를 가져와야 할 다음보기가있는보기입니다.

Ques : listview 너비와 높이가 fill_parent로 설정되어 있으면 왜 호출이 적습니까? Ans : 팽창기가리스트의 화면 영역에 대해 고정 된 크기를 가지기 때문에 뷰를 화면에 렌더링하기 위해 한 번 계산됩니다.

쿼리가 해결되기를 바랍니다.


답변

AutoCompleteTextView의 드롭 다운과 동일한 문제가 발생했습니다. 여기에 도착할 때까지 이틀 동안 문제와 싸우고 있었고 해결책을 보여주었습니다.

dropDownHeight = “match_parent”를 쓰면 문제가 해결 된 것입니다. 이제 문제는 UI와 관련이 있습니다 (하나의 항목이있는 경우 드롭 다운이 너무 큼). 그러나 여러 호출 문제 (보다 중요)가 수정되었습니다.

감사합니다!!


답변

“왜 각 행마다 getview가 세 번 호출됩니까?” getView는 listview를 스크롤 할 때 호출되고 더 잘 말하면 목록보기의 위치가 변경되면 호출됩니다!