gpt4 book ai didi

android - GridView getChildAt 为可见子项返回 null

转载 作者:太空狗 更新时间:2023-10-29 14:42:04 28 4
gpt4 key购买 nike

我有一个从自定义相机 Activity 中拍摄的图像网格。在其他 Activity 中,我将图像加载到网格中。我有一种方法可以打开和关闭每个图像上的复选框。在尝试浏览所有图像时,我遇到了一些奇怪的行为:GridView.getCount() 返回 GridView 中正确数量的图像,但 GridView.getChildAt() 方法仅返回部分图像。因此,当我尝试这样做时,我得到了一个空异常。

GridView 适配器:

public class PhotoGalleryAdapter extends ArrayAdapter {

// Declare variables
private Activity adapterActivity;
private int layoutResourceId;
private ArrayList gridItemsArray;
private static LayoutInflater inflater = null;

public PhotoGalleryAdapter(Activity a, int layoutResourceId, ArrayList fGridItemArray) {
super(a, layoutResourceId, fGridItemArray);
this.layoutResourceId = layoutResourceId;
this.adapterActivity = a;
this.gridItemsArray = fGridItemArray;
}

public int getCount() {
return gridItemsArray.size();
}

public Object getItem(int position) {
return gridItemsArray.get(position);
}

public long getItemId(int position) {
return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
HolderViewRow holderRow = null;
View rowView = (View) convertView;

if (rowView == null){
LayoutInflater inflater = adapterActivity.getLayoutInflater();

rowView = inflater.inflate(layoutResourceId, parent, false);

holderRow = new HolderViewRow();

holderRow.imageFileBMP = (ImageView) rowView.findViewById(R.id.imageObject);

holderRow.checkBox = (CheckBox) rowView.findViewById(R.id.itemCheckBox);
holderRow.checkBoxImage = (ImageView) rowView.findViewById(R.id.itemImageCheckBox);

rowView.setTag(holderRow);
}
else
{
holderRow = (HolderViewRow) rowView.getTag();
}

PhotoGalleryItem item = (PhotoGalleryItem) gridItemsArray.get(position);

// Set the decoded bitmap iitemnto ImageView
holderRow.imageFileBMP.setImageBitmap(item.getImageBMP());
holderRow.imageFilePath = item.getImagePath();

return rowView;
}

static class HolderViewRow {
ImageView imageFileBMP;
String imageFilePath;
CheckBox checkBox;
ImageView checkBoxImage;
}
}


public class PhotoGalleryItem {
private Bitmap imageBMP;
private String imagePath;
private CheckBox checkBox;
private ImageView checkBoxImage;

public PhotoGalleryItem(Bitmap imageBMP, String imagePath) {
super();
this.imageBMP = imageBMP;
this.imagePath = imagePath;
}

public Bitmap getImageBMP() {
return imageBMP;
}

public void setImageBMP(Bitmap imageBMP) {
this.imageBMP = imageBMP;
}

public String getImagePath() {
return imagePath;
}

public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
}

网格 Activity :

        private void RenderGridAndSetImages() {
// Locate the GridView in gridView_PhotoDisplay.xml
gridView = (GridView) findViewById(R.id.gridView_PhotoDisplay);
// Pass String arrays to Adapter Class
imageAdapter = new PhotoGalleryAdapter(this, R.layout.gridview_photo_item, GridItemArrayList);
gridView.setAdapter(imageAdapter);
}

以及有问题的函数:

    public void SetVisibilityCheckBox_OnGrid (boolean checkBoxVisibility) {
// Handle item selection On GRIDVIEW ImageItem
final int size = gridView.getCount();
try {
ImageView imageCB;
int cbVisibility;

if (checkBoxVisibility)
cbVisibility = View.VISIBLE;
else
cbVisibility = View.INVISIBLE;

for(int i = 0; i < size; i++) {
View viewItem = (View) gridView.getChildAt(i);
if (viewItem != null) {
imageCB = (ImageView) viewItem.findViewById(R.id.itemImageCheckBox);
imageCB.setVisibility(cbVisibility);
}
}
}
catch (Exception ex) {
// Error occurred while SetSelectCheckBoxOnGrid
Log.e(PACKAGE_NAME, ACTIVITY_NAME + ".SetVisibilityCheckBox_OnGrid\n" + ex.getMessage());
}
}

GridView 布局:

<RelativeLayout
android:id="@+id/layout_Holder"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/activityBackground"
tools:context="com.mountain.saymera.PhotoGalleryActivity">

<!-- Image view: layout_GridView -->
<RelativeLayout
android:id="@+id/layout_PhotoGridDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:background="@color/activityBackground"
android:padding="1dp">

<GridView
android:id="@+id/gridView_PhotoDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:numColumns="auto_fit"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:focusable="true"
android:clickable="true"/>

<TextView
android:id="@+id/textView_NoImages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gallery_no_images"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:textColor="@color/MenuButtonTint"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>

GridView 项目布局:

<RelativeLayout
android:id="@+id/layout_Holder"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/single_photo_frame_on_grid">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:background="@color/Grid_ItemFrame"
android:padding="0.3dp">

<com.mountain.saymera.SquareImageView
android:id="@+id/imageObject"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
</LinearLayout>

<CheckBox
android:id="@+id/itemCheckBox"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:tint="@color/Grid_ItemCheckbox"/>

<ImageView
android:id="@+id/itemImageCheckBox"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:background="#00000000"
android:src="@mipmap/ic_crop_square_black_24dp"
android:tint="@color/MenuCheckboxTint"/>

</RelativeLayout>

需要一些帮助,谢谢

最佳答案

我在 LinearLayout 中以编程方式添加 ImageView 时遇到了同样的问题。不幸的是,目前这种方法似乎有问题,所以我不得不通过寻找替代方法来帮助自己。作为一种解决方法,我将我的 ImageView 的引用保存在一个列表中并使用它们。

关于android - GridView getChildAt 为可见子项返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46468885/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com