0
votes

ListView is skipping the items that lie at the end of the screen. I am sure that the item is present in the adapter because the separator is thicker where the view was supposed to be. On scrolling some distance up and down, this item sometimes becomes visible but others disappear.

Edit: Out of the 26 items in the arrayList, index 6, 13 and 20 are missing. The ListView is the only view which has been accessed by the activity.

Code of the Adapter

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;
    }
}

XML of the ListView

<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 of a list view item

<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
many things can cause this, visibilty gone or the scroll view is getting too big and covering the whole layout try to change tha parent layout to RelativeLayout and/or to remove everything except the listview to see if all items are really there or not - Yamen Nassif
Only the list view is visible and only 3-4 out of 26 views are missing. - Manik Sejwal
i am curious about your activity code which displays the views, there must be something causing this and/or try match_parent instead of fill_parent in the listview - Yamen Nassif
Full parent was just an experiment even match parent failed and caused this issue. - Manik Sejwal
Fill_parent was just an experiment when I was unable to fix this using match_parent. I have reverted to match_parent now. - Manik Sejwal

1 Answers

0
votes

Figured it out.

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);
            }
        });
    }

When item.mHeader is null or an empty string, list the visibility of View view = listItemView.findViewById(R.id.lesson_item_header_layout); is set to gone. When the view is being recycled, the visibility is left ignored. The other views are loaded later so the entire listview item disappears. just had to add view.setVisibility(View.GONE); in the else statement.