图片中的Andr​​oid GridView的变化。怎么样? [英] Images in Android GridView changes. How?

查看:116
本文介绍了图片中的Andr​​oid GridView的变化。怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

我使用我的应用程序的标准gridview的来源。这是我面临的问题是,当我在GridView滚动图像时,图像动态改变每次,具有位置错误的图像。仅供参考下面是code I使用......


  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    //设置我们想要的缩略图ID列的一个阵列
    的String [] =投影{MediaStore.Images.Thumbnails._ID};
    //创建光标指向SD卡
    光标= managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            投影,//哪些列返回
            空,//返回所有行
            空值,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    //获取缩略图图片ID列索引
    参数:columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);    GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);
    gridView.setAdapter(新ImageAdapter(本));
    gridView.setOnItemClickListener(新OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司,视图V,
                INT位置,长的id){            //发送图片ID来FullScreenActivity
            意图I =新意图(getApplicationContext(),ImageSelection.class);
            //传递数组索引
            i.putExtra(imageIndex_from_Activity位置);
            startActivity(ⅰ);        }
    });

> ImageAdapter类

 私有类ImageAdapter延伸BaseAdapter {    私人上下文的背景下;    公共ImageAdapter(上下文localContext){
        上下文= localContext;
    }    公众诠释的getCount(){
        返回cursor.getCount();
    }
    公共对象的getItem(INT位置){
        返回的位置;
    }
    众长getItemId(INT位置){
        返回的位置;
    }
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        ImageView的picturesView;
        如果(convertView == NULL){
            picturesView =新ImageView的(上下文);
            //将光标移动到当前位置
            cursor.moveToPosition(位置);
            //获取被请求的列的当前值
            INT imageID = cursor.getInt(参数:columnIndex);
            //设置基于所提供的URI的图像的内容
            picturesView.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,+ imageID));
            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(5,5,5,5);
            picturesView.setLayoutParams(新GridView.LayoutParams(80,80));
        }
        其他{
            picturesView =(ImageView的)convertView;
        }
        返回picturesView;
    }
}


  

附加截图,1.Before滚动2.Scrolled下来,再向上滚动。



  

为什么顺序的变化?



解决方案

您只设置新的图片视图。

执行以下操作:

 如果(convertView == NULL){
    picturesView =新ImageView的(上下文);
    picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    picturesView.setPadding(5,5,5,5);
    picturesView.setLayoutParams(新GridView.LayoutParams(80,80));
}其他{
    picturesView =(ImageView的)convertView;
}
//将光标移动到当前位置
cursor.moveToPosition(位置);
//获取被请求的列的当前值
INT imageID = cursor.getInt(参数:columnIndex);
//设置基于所提供的URI的图像的内容
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,+ imageID));

I am using the standard gridview source for my app. The problem which i face is that, when i scroll the images in the gridview, the images change dynamically everytime, having wrong images at the positions. For reference below is the code i use......

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Set up an array of the Thumbnail Image ID column we want
    String[] projection = {MediaStore.Images.Thumbnails._ID};
    // Create the cursor pointing to the SDCard
    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    // Get the column index of the Thumbnails Image ID
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter(this));




    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), ImageSelection.class);
            // passing array index
            i.putExtra("imageIndex_from_Activity", position);
            startActivity(i);

        }
    });

> ImageAdapter class

 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;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            // 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(5, 5, 5, 5);
            picturesView.setLayoutParams(new GridView.LayoutParams(80, 80));
        }
        else {
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }
}

Attaching screenshots, 1.Before Scrolling 2.Scrolled down and scrolled up again.

Why does the order changes??

解决方案

You're only setting the image on new views.

Do the following:

if (convertView == null) {
    picturesView = new ImageView(context);
    picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    picturesView.setPadding(5, 5, 5, 5);
    picturesView.setLayoutParams(new GridView.LayoutParams(80, 80));    
} else {
    picturesView = (ImageView)convertView;
}
// 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));

这篇关于图片中的Andr​​oid GridView的变化。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆