0

I am trying to iterate all child views of a listview including those that are off screen (invisible until scroll) but it only returns those that are onscreen (visible).

Button btnAddToCart = (Button) findViewById(R.id.btnAddToCart);
    btnAddToCart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            View view;
            TextView tvProductBrand;
            for (int i = 0; i < lv.getCount(); i++){

                view = lv.getChildAt(i);
                if(view != null)
                {
                    tvProductBrand = (TextView) view.findViewById(R.id.tvProductBrand);
                    str_ItemBrand_from_lv = tvProductBrand.getText().toString();

                    Toast.makeText(getApplicationContext(), "lv_item: "+str_ItemBrand_from_lv, Toast.LENGTH_SHORT).show();
                }
            }
        }
    });

I know this view = lv.getChildAt(i); only returns visible views only, how can I return all child views?

My listview adapter is lv_custom_adapter (perhaps I need to use it instead but not sure how)?

user3560827
  • 632
  • 1
  • 5
  • 22

2 Answers2

0

It's Good idea Use RecyclerView

gradle dependency:

implementation "com.android.support:recyclerview-v7:28.0.0"

https://developer.android.com/guide/topics/ui/layout/recyclerview

put it in NestedScrollView set nestedScrollingEnabled=true

<NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:nestedScrollingEnabled="true"/>

You will get all children at once

Narek Hayrapetyan
  • 1,731
  • 15
  • 27
0

Thanks to this post for shedding the light: Get ListView children that are not in view

My view:

view = lv.getAdapter().getView(i, lv.getChildAt(i), lv);
user3560827
  • 632
  • 1
  • 5
  • 22