在android运行时更改项目点击时的gridview图像

ycl3bljg  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(287)

我正在应用程序中使用gridview。我希望当我点击这个项目时,它会替换另一个数组图片。实际上我在应用程序第一次运行时使用了两个数组,gridview将显示第一个数组图片,然后当我单击项目时,它将根据其位置被第二个数组图片替换。
我试过了,但没有解决。所以谁能帮帮我。。
gridviewcontent.java文件

public class GridViewContent extends BaseAdapter {
    private Context context;

    public int pictureArray[]={
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,        
    };
    public GridViewContent(Context c){
        context=c;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return (pictureArray.length);
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return pictureArray[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        ImageView myimage=new ImageView(context);
        myimage.setImageResource(pictureArray[position]);
        myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        myimage.setLayoutParams(new GridView.LayoutParams(70, 70));

        return myimage;
    }

}

主活动.java

public class MainActivity extends Activity {
    Context ctx;
    GridViewContent grid1 = new GridViewContent(ctx);
    // GridView grid;
    /*
     * static final int[] numbers = new int[] {
     * R.drawable.question,R.drawable.question
     * ,R.drawable.question,R.drawable.question
     * ,R.drawable.question,R.drawable.question, R.drawable.question,
     * 
     * };
     */
    public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
            R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final GridView grid = (GridView) findViewById(R.id.gv_memory);
        grid.setAdapter(new GridViewContent(this));
        // grid.setOnItemClickListener(itemClickListener);
        grid.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int id = v.getId();
                ImageView pictImage = new ImageView(ctx);
                int imgView = OriginalArray[id];
                pictImage.setImageResource(imgView);

            }
        });

    }
}

main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gv_memory"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" >

</GridView>

提前谢谢。。。。

4si2a6ki

4si2a6ki1#

您需要做一些小的更改,如下所示:

public class GridViewContent extends BaseAdapter {
    private Context context;

    public int pictureArray[]={
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,
            R.drawable.question,        
    };

 public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
            R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, };

    public GridViewContent(Context c){
        context=c;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return (pictureArray.length);
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return pictureArray[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        ImageView myimage=new ImageView(context);
        myimage.setImageResource(pictureArray[position]);
        myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        myimage.setLayoutParams(new GridView.LayoutParams(70, 70));

       myimage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            myimage.setImageResources(Original[arg2]);
        }
    });
        return myimage;
    }

}
cbjzeqam

cbjzeqam2#

为gridview实现onitemclicklistener。请参阅此网站http://www.mkyong.com/android/android-gridview-example/. 将您的更改逻辑写入其中并调用adapter.notifydatasetchanged()。http://stackoverflow.com/questions/6201884/how-can-i-change-image-on-gridview-runtime

相关问题