android - 以编程方式更改 GridView 中的 ImageView

标签 android android-layout android-activity gridview imageview

我已经设置了带有 TextView 叠加层的 ImageView 网格。我的ImageAdapter代码如下:

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.image, null);
        TextView textView = (TextView)grid.findViewById(R.id.mastery_text);

        imageView = (ImageView)grid.findViewById(R.id.mastery_image);
        grid.setLayoutParams(new GridView.LayoutParams(150, 150));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        grid.setPadding(8, 8, 8, 8);
        grid.setBackgroundResource(R.color.orange);

        imageView.setImageResource(mThumbIds[position]);            

    } else {
        grid = (View) convertView;
    }

    return grid;
}

我的 ImageAdapter 对应的 XML 布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_practitioner" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView 
        android:id="@+id/mastery_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
    />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="bottom"
        android:background="#bbffffff"
        android:focusable="false"
        android:focusableInTouchMode="false" >

        <TextView android:id="@+id/mastery_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:textColor="@color/black"
            android:gravity="bottom|center"
            android:textSize="12sp"
            android:textAllCaps="true"
            android:paddingBottom="0dp"
            android:text="3/3"
            />

    </LinearLayout>
</FrameLayout>

我的 GridView( Activity 类)的 XML 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="4"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center" />

</LinearLayout>

这是我主要 Activity 的 onCreate 方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_masteries);

    GridView gridview = (GridView) findViewById(R.id.gridview);

    ImageAdapter adapter = new ImageAdapter(this);
    gridview.setAdapter(adapter);

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Toast.makeText(Masteries.this, "" + position,
                    Toast.LENGTH_SHORT).show();
        }
    });
}

我想在用图像初始化网格后,更改我的 Activity 类中的一个 ImageView,给定它在网格上的位置。我该怎么做?

不是请求更改图像以响应 onItemClick。 提前致谢!

编辑: 我正在考虑在我的适配器中创建一个 changeImage(int position, int imageId) 方法并从我的 Activity 类中调用它。这是正确的做法吗?

最佳答案

在你的适配器中:

public void updateImage(int position, int resourceId)
{
    mThumbIds[position] = resourceId;
    notifyDataSetChanged();
}

在您的 Activity 中:

mAdapter.updateImage(<position>, <image_resource_id>);

注意事项

  1. 您必须使适配器成为您的 Activity 的成员
  2. 主要思想是您修改支持数据并通知 GridView 这已经改变并且是时候重绘了
  3. 您的getView() 方法实现需要大量改进。一旦系统开始回收 View ,它会导致很多错误(convertView 参数进来!= null 的位置与上次使用的位置不同)

这是您的 getView() 应该是什么样子的草图:

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder viewHolder;
    LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.image, parent, false);
        // next three lines would not be necessary if:
        //  a) it is the same for every item;
        //  b) you inflate properly (using the parent);
        //  c) you specify this in the item's xml (R.layout.image)
        convertView.setLayoutParams(new GridView.LayoutParams(150, 150));
        convertView.setPadding(8, 8, 8, 8);
        convertView.setBackgroundResource(android.R.color.holo_red_light);

        viewHolder = new ViewHolder();
        viewHolder.mTextView = (TextView)convertView.findViewById(R.id.mastery_text);
        viewHolder.mImageView = (ImageView)convertView.findViewById(R.id.mastery_image);
        // this could also be set in xml perhaps
        viewHolder.mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder)convertView.getTag();
    }

    // update the values every time we are being asked to update the item,
    // because the item might have been reused from a different position
    viewHolder.mImageView.setImageResource(mThumbIds[position]);
    //viewHolder.mTextView.setText("myText");

    return convertView;
}


public static class ViewHolder
{
    TextView mTextView;
    ImageView mImageView;
}

关于android - 以编程方式更改 GridView 中的 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32011677/

相关文章:

java - 为 View 禁用 onClick 声音

java - 从位图android的边缘创建路径

android - 将数据从 Activity 传回已创建的 fragment

android - 获取另一个 Activity 的值(value)放在另一个 Activity 的 ListView 的编辑文本中

java - 如何从 Activity 外部移动到另一个 Activity android

android - 始终显示 ListView 的滚动条

android - 如何在Android中前三次扫描图像后制作免费或付费应用程序

android - 如何在 appwidgetprovider 中找到 View ID?

android - 请求许可时应用崩溃

android - 当手动将进度设置为 MotionLayout 时,它会清除所有约束