Gallery和baseAdapter增量android

时间:2012-04-01 21:57:34

标签: android android-gallery android-adapter

所以,我喜欢很多人一直在努力创建一个有两行for android的画廊..

到目前为止,我的解决方案是让一个基本适配器调用布局,其中有两个表行,每个表中都有一个照片的位置,然后在我的基本适配器中放置一个图像,然后将光标移动到下一个位置并放置另一个图像在第二个位置..它到目前为止工作接受我们最终重复图像..所以我将image1和图像2放在第一遍,然后下一遍它将image2放在image3,..是否有一个设置我的基本适配器调用的增量的方法?

以下固定代码

public class GallAdapter extends BaseAdapter {
        public Cursor cursor;
        private int columnIndex;
        private Context context;
        int imageBackground;
        String url;
        Uri uri;
        int originalImageId;
        int imageID;
        int columnData;
        ViewGroup myp;
        ImageView d;
        ImageView d2;
        Boolean done = false;

        public GallAdapter(Context ctx, Cursor cur, int cIn ) {
            context = ctx;
            columnIndex = cIn;

            cursor = cur;

        }

        @Override
        public int getCount() {
            Toast.makeText(context, "old:"+cursor.getCount()+" 1/2:"+cursor.getCount()/2+" roundedup:"+Math.ceil((float)cursor.getCount()/2), Toast.LENGTH_SHORT).show();
            return (int) Math.ceil((float)cursor.getCount()/2);
        }

        @Override
        public Object getItem(int position) {

            return position;
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            myp = parent;
            View v;

            if(convertView == null){
                v = LayoutInflater.from(context).inflate(R.layout.galitem2, parent, false);
            }else{
                v = convertView;

            }


            ImageView photo = (ImageView) v.findViewById(R.id.imageView);
            ImageView border = (ImageView) v.findViewById(R.id.borderView);
            d = (ImageView) v.findViewById(R.id.delView);

            ImageView photo2 = (ImageView) v.findViewById(R.id.image2View);
            ImageView border2 = (ImageView) v.findViewById(R.id.border2View);
            d2 = (ImageView) v.findViewById(R.id.del2View);

                cursor.moveToPosition(position*2);

                // Get the current value for the requested column
                imageID = cursor.getInt(columnIndex);
                // obtain the image URI
                uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                url = uri.toString();
                // Set the content of the image based on the image URI
                originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
                Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
                                originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
                photo.setImageBitmap(b);

                photo.setScaleType(ImageView.ScaleType.FIT_CENTER); 
                d.setTag(uri);
                d.setOnClickListener(new OnClickListener(){


                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String path = getRealPathFromURI((Uri) v.getTag());

                        File file = new File(path);
                        file.delete();
                        getContentResolver().delete((Uri) v.getTag(), null, null);
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()+"/LC/images/")));
                        Intent galView = new Intent(getBaseContext(), GalleryView2.class);
                        galView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        getBaseContext().startActivity(galView);
                        finish();


                    }


                });
               if(position*2+1<=cursor.getCount()-1){
                  // Move cursor NEXT current position

                    cursor.moveToPosition(position*2+1);

                    // Get the current value for the requested column
                    imageID = cursor.getInt(columnIndex);
                    // obtain the image URI
                    uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                    url = uri.toString();
                    // Set the content of the image based on the image URI
                    originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
                    Bitmap b2 = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
                                    originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
                    photo2.setImageBitmap(b2);

                    photo2.setScaleType(ImageView.ScaleType.FIT_CENTER);    
                    d2.setTag(uri);
                    d2.setOnClickListener(new OnClickListener(){


                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            String path = getRealPathFromURI((Uri) v.getTag());

                            File file = new File(path);
                            file.delete();
                            getContentResolver().delete((Uri) v.getTag(), null, null);
                            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()+"/LC/images/")));
                            Intent galView = new Intent(getBaseContext(), GalleryView2.class);
                            galView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            getBaseContext().startActivity(galView);
                            finish();


                        }


                    });
                }else{
                    //border2.setVisibility(v.INVISIBLE);
                    d2.setVisibility(v.INVISIBLE);

               }

            return v;
        }

    }



}

1 个答案:

答案 0 :(得分:1)

等等,不是你想要的:

first photo -> cursor.moveToPosition(position * 2);
second photo -> cursor.moveToPosition(position * 2 + 1);

您只需在适配器getCount()中返回正确的尺寸即可。