2012-04-01 82 views
0

所以,我喜欢很多一直缠斗到创建具有两排为Android画廊..画廊和baseAdapter增量的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; 
     } 

    } 



} 
+0

那么,你想要两行独立滚动,或者你只是创建一个每个项目有两行的图库(getview调用)? – dmon 2012-04-01 22:12:43

+0

每行getview调用2行.. – erik 2012-04-01 22:25:25

回答

1

等待,是不是你想要:

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

你只需要在您的适配器getCount()返回正确的大小。

+0

即时消息没有关注? – erik 2012-04-01 22:30:19

+0

我不知道你在说什么..我以为getCount返回了找到的图像总数..我感到困惑的是如何让getView()只能被称为每个其他图像.. – erik 2012-04-01 22:33:55

+0

(我是不是在讨论'cursor.getCount()')所以你想每次获取视图显示2件东西吗?然后,你的*适配器*需要声明它有一半的事情比它真正的有,这样,'getView()'只会被调用一半的时间。然后,在'getView()'中,你的任务是渲染与这个位置相对应的两个图像,这就是'pos * 2'和'pos * 2 + 1'的位置。 – dmon 2012-04-01 23:03:06