android - ListView 获取所有子视图(在屏幕上和屏幕外)
问题描述
我正在尝试迭代列表视图的所有子视图,包括那些不在屏幕上的子视图(在滚动之前不可见),但它只返回那些在屏幕上的子视图(可见)。
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();
}
}
}
});
我知道view = lv.getChildAt(i);
这只返回可见视图,我怎样才能返回所有子视图?
我的 listview 适配器是lv_custom_adapter
(也许我需要使用它,但不确定如何)?
解决方案
使用RecyclerView是个好主意
梯度依赖:
implementation "com.android.support:recyclerview-v7:28.0.0"
https://developer.android.com/guide/topics/ui/layout/recyclerview
把它放在NestedScrollView
集合中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"/>
您将立即获得所有孩子
推荐阅读
- javascript - Firebase 添加的数据自动被删除
- java - 为什么我可以将 int 分配给 Short 作为返回类型,但不能在参数列表中?
- c - 我的代码在我的笔记本电脑上运行时有效,但是当我将其上传到hackerrank时,我在strtol处出错
- python - Keras:嵌入层的加权平均值
- android - PagerAdapter - 单击项目的按钮时出现错误的项目索引
- tensorflow - Tensorflow:如何反转model.predict()?
- python - 随机化两个列表(numpy in)并在python中保持顺序
- python-3.x - “to_csv”方法不提示下载窗口
- kotlin - 为什么这仍然抛出异常?
- php - 如果选择字段(带有 meta_key)具有使用 ajax/jquery 的特定值,则 css 的条件逻辑