【问题标题】:[Android]adding checkbox to images in grid view[Android]在网格视图中为图像添加复选框
【发布时间】:2011-06-14 06:07:10
【问题描述】:

用于网格布局的 XML。

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myGrid"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:padding="2dip"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="148dp"
    android:stretchMode="spacingWidthUniform"
    android:gravity="center"
    />
</RelativeLayout>

imagenselect.xml 用于图像和复选框。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/GridItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="#000080">

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>
 <CheckBox 
     android:id="@+id/check1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" android:text="Android" />
</LinearLayout>

在gridview中添加图像并显示的类,

private class ImageAdapter extends BaseAdapter {

        private Context context;
        public ImageAdapter(Context localcontext){  
            context = localcontext;
        }
        public int getCount() { 
            return cursor.getCount();
        } 

        public Object getItem(int position) {
            return position;
        }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub\

    View MyView = convertView;
    ImageView picturesView;
    picturesView = new ImageView(context);

    if (convertView == null) {

     LayoutInflater li = getLayoutInflater();
     MyView =  li.inflate(R.layout.imagenselect, null);

    // Move cursor to current position
    cursor.moveToPosition(position);
    // Get the current value for the requested column
    int imageID = cursor.getInt(columnIndex);
    // Set the content of the image based on the provided URI
        picturesView.setImageURI(Uri.withAppendedPath(
    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));

    picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    picturesView.setPadding(8, 8, 8, 8);
    picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));           
        }
    else {
         picturesView = (ImageView) convertView;
    }
    return picturesView;
      }
    }
}

使用此代码,我只获得 gridview 中的图像。但我想放大视图并使用它,以便我能够在图像中添加一个复选框。 (对于每个图像一个复选框)。

由于函数“myView”和“picturesView”中有两个视图。如果我尝试将picturesView 类型转换为myView,那么我会崩溃。提前致谢!

按照你的建议进行更改,我遇到了崩溃。

        @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub\

        View myView = convertView;
        if (convertView == null) {
           LayoutInflater li = getLayoutInflater();
           myView =  li.inflate(R.layout.imagenselect, null);
            }
        ImageView picturesView;
        picturesView = new ImageView(context);
               picturesView = (ImageView) myView.findViewById( R.id.grid_item_image);

        // Move cursor to current position
        cursor.moveToPosition(position);
        // Get the current value for the requested column
        int imageID = cursor.getInt(columnIndex);
        // Set the content of the image based on the provided URI
        picturesView.setImageURI(Uri.withAppendedPath(
              MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));

        picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        picturesView.setPadding(8, 8, 8, 8);
        picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));               

        return myView;
    }

【问题讨论】:

  • 请发布崩溃的详细信息,最好是显示抛出异常的 logcat 输出。
  • 上面发布的代码不会崩溃,它只会在我将一个视图类型转换为另一个视图时崩溃。使用上面的代码我只得到图像,现在我尝试添加复选框

标签: android gridview view inflate


【解决方案1】:

您的代码实际上让我很困惑。您的代码总是返回一个ImageView 对象,而不是您从包含复选框的布局中扩展的MyView 视图。这可以解释为什么您的复选框没有出现。

我认为你需要一些类似的东西:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View myView = convertView
    if( myView == null )
    {
        LayoutInflater li = getLayoutInflater();
        myView = li.inflate(R.layout.imagenselect, null);
    }
    ImageView pictureView = (ImageView) myView.findViewById( R.id.grid_item_image );
    // initialise pictureView here.
    return myView;
}

【讨论】:

  • 好吧,如果您返回一个 ImageView 对象,您的复选框将不会出现在您的 ListView 项中。如果您从 imagenselect 布局返回您膨胀的视图,您将获得一个包含 ImageView 对象和 CheckBox 对象的 LinearLayout。在您的问题中,您说您无法弄清楚为什么您的 CheckBox 没有出现,但是您返回的视图不包含 CheckBox。在上面的答案中,我向您展示了您应该如何做才能返回一个视图,该视图由一个包含 ImageView 和 CheckBox 的 LinearLayout 组成。
  • 请提供崩溃的详细信息。
  • 谢谢马克!我得到了图片和复选框。但是,如果我有以下行,它就会崩溃。 PicturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
  • 为什么不通过添加android:layout_width="100dp"android:layout_height="100dp" 在您的布局XML 中设置它而不是通过编程方式进行设置?
  • 现在我在 imagenselect.xml 中添加了一个复选框,我可以看到该复选框。但是,如果我选择(复选标记)在网格视图中说 2 个项目。然后我向下滚动,我看到其他一些 2 图像也被选中。这很奇怪 。有什么想法吗?
猜你喜欢
  • 2014-10-29
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
相关资源
最近更新 更多