ListView skipps views / items

时间:2017-09-15 12:42:30

标签: android listview listviewitem

ListView正在跳过位于屏幕末尾的项目。我确信该项目存在于适配器中,因为分隔符在视图应该是较厚的位置。在向上和向下滚动一些距离时,此项目有时会变得可见,但其他项目会消失。

编辑:在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"/>

1 个答案:

答案 0 :(得分: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为null或空字符串时,列出View view = listItemView.findViewById(R.id.lesson_item_header_layout);设置为已消失的可见性。当视图被回收时,可见性将被忽略。稍后加载其他视图,以便整个列表视图项消失。只需在else语句中添加view.setVisibility(View.GONE);