2017-09-15 86 views
0

ListView正在跳過位於屏幕末尾的項目。我確定該項目存在於適配器中,因爲分隔符在視圖應該是較厚的地方。在上下滾動一段距離時,此項目有時會變得可見,但其他項目消失。ListView skipps views/items

編輯:在arrayList中的26個項目中,索引6,13和20缺失。 ListView是活動訪問的唯一視圖。

適配器的代碼

private class LessonAdapter extends ArrayAdapter<Item> { 

    LessonAdapter(Activity context, ArrayList<Item> list) { 
     super(context, 0, list); 
     if (BuildConfig.DEBUG) Log.v(LOG_TAG, "LessonAdapter() entered"); 
    } 

    @NonNull 
    @Override 
    public View getView(int position, View listItemView, @NonNull ViewGroup parent) { 
     if (BuildConfig.DEBUG) Log.v(LOG_TAG, "getView() entered"); 
     if (listItemView == null) 
      listItemView = LayoutInflater.from(getContext()).inflate(
        R.layout.lesson_list_item, null, true); 

     final Item item = getItem(position); 
     final TextView textView1 = listItemView.findViewById(R.id.lesson_item_body); 
     final View progressBar = listItemView.findViewById(R.id.lesson_list_progress_bar); 

     if (position > 0) { 
      textView1.setVisibility(View.GONE); 
      textView1.setOnLongClickListener(new View.OnLongClickListener() { 
       @Override 
       public boolean onLongClick(View view) { 
        textView1.setVisibility(View.GONE); 
        return false; 
       } 
      }); 
     } 

     Timber.v("mHeader = " + item.mHeader); 
     Timber.i("height = " + listItemView.getMeasuredHeightAndState()); 

     TextView textView = listItemView.findViewById(R.id.lesson_item_header_text); 
     View view = listItemView.findViewById(R.id.lesson_item_header_layout); 
     if (item.mHeader == null || item.mHeader.equals("")) 
      view.setVisibility(View.GONE); 
     else { 
      textView.setText(Html.fromHtml(item.mHeader)); 
      view.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        if (textView1.getVisibility() == View.GONE) { 
         progressBar.setVisibility(View.VISIBLE); 
         textView1.post(new Runnable() { 
          public void run() { 
           Timber.v("mText = " + item.mText); 
           textView1.setText(Html.fromHtml(item.mText)); 
           progressBar.setVisibility(View.GONE); 
           textView1.setVisibility(View.VISIBLE); 
          } 
         }); 
        } else textView1.setVisibility(View.GONE); 
       } 
      }); 
     } 

     return listItemView; 
    } 
} 

ListView中的XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_margin="16dp" 
android:orientation="vertical"> 

<ScrollView 
    android:id="@+id/lesson_scroll" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:visibility="gone"> 

    <TextView 
     android:id="@+id/lesson" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> 

</ScrollView> 

<WebView 
    android:id="@+id/web_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:visibility="gone" /> 

<ListView 
    android:id="@+id/lesson_list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollingCache="true" 
    android:visibility="gone" /> 

XML的列表視圖項

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:id="@+id/lesson_item"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:id="@+id/lesson_item_header_layout"> 

<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:minHeight="50dp" 
    android:layout_weight="1" 
    android:id="@+id/lesson_item_header_text" 
    android:layout_marginTop="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:textAppearance="?android:textAppearanceMedium" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_action_drop_down" 
     android:padding="8dp" 
     android:layout_gravity="center_vertical" 
     android:contentDescription="@string/drop_down" /> 

</LinearLayout> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/lesson_item_body" 
    android:paddingLeft="4dp" 
    android:paddingRight="4dp" 
    android:textAppearance="?android:textAppearanceMedium" 
    /> 

<ProgressBar 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:id="@+id/lesson_list_progress_bar" 
    android:visibility="gone"/> 

的3210
+2

很多事情都會導致這種情況,可見性消失或滾動視圖變得太大並且覆蓋整個佈局 嘗試將父級佈局更改爲RelativeLayout和/或刪除除listview之外的所有內容以查看是否所有項目都是真的存在或不是 –

+0

只有列表視圖可見,並且只有3-4個視圖缺失。 –

+0

我很好奇你的活動代碼顯示的意見,必須有東西造成這種情況和/或嘗試match_parent,而不是在列表視圖中的fill_parent –

回答

0

找出來了。

if (item.mHeader == null || item.mHeader.equals("")) 
     view.setVisibility(View.GONE); 
    else { 
     textView.setText(Html.fromHtml(item.mHeader)); 
     view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (textView1.getVisibility() == View.GONE) { 
        progressBar.setVisibility(View.VISIBLE); 
        textView1.post(new Runnable() { 
         public void run() { 
          Timber.v("mText = " + item.mText); 
          textView1.setText(Html.fromHtml(item.mText)); 
          progressBar.setVisibility(View.GONE); 
          textView1.setVisibility(View.VISIBLE); 
         } 
        }); 
       } else textView1.setVisibility(View.GONE); 
      } 
     }); 
    } 

當item.mHeader爲空或空字符串,列出View view = listItemView.findViewById(R.id.lesson_item_header_layout);的可見性設置爲不見了。當視圖被回收時,可見性將被忽略。其他視圖將在稍後加載,以便整個listview項目消失。只需在else語句中添加view.setVisibility(View.GONE);即可。