在View分页器的网格视图中显示图像

时间:2012-03-15 14:56:41

标签: android image gridview android-viewpager

我正在使用ViewPager addon api的图片库。我正在从SD卡中的特定文件夹加载图像。我的目标是在ViewPager中每个屏幕只显示9个图像,这是我无法实现的。以下代码是主要活动。

public class AndroidSwipeGalleryActivity extends Activity {

private int size;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.image_gallery);

    File file = new File(Environment.getExternalStorageDirectory()+"/xxxxxxx/images");

    if (file.exists()) {

        size = file.listFiles().length;

        System.out.println("=====File exists====Length is====="+size);

        double quo = (double)size/9;

        System.out.println("====Dividing by 9====" + quo);

        size = (int) Math.ceil(quo);

        System.out.println("===Math===== "+size);

    } else {

        System.out.println("======File does not exists====");

    }

    MyPagerAdapter adapter = new MyPagerAdapter(this);
    adapter.setScrCount(size);
    ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);

}

}

寻呼机适配器和图像适配器位于以下类别中:

public class MyPagerAdapter extends PagerAdapter {

private TextView tv;

private GridView gv;

private int scrCount;

private int count;

public int imageCount;

private Cursor cursor;

private int columnIndex;

private Activity act;

public MyPagerAdapter(Activity act) {

    this.act = act;

}

public int getCount() {

    return getScrCount();

}

public Object instantiateItem(View collection, int position) {

    // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // Create the cursor pointing to the SDCard

    String uri = MediaStore.Images.Media.DATA;

    String condition = uri + " like '%/beverlyhills/images%'";

    cursor = act.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, condition, null, null);

    count = cursor.getCount();

    System.out.println("Cursor count::::"+count);

    // Get the column index of the Thumbnails Image ID

    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;

    for (int i = 0; i < getScrCount() ; i++) {

            if (count > 9) {

                if (position == i) {

                int num = 0;

                num = count - 9;

                count = num;

                imageCount = 9;

                }

            } else {

                imageCount = count;

            }
    }

    resId = R.layout.number_one;

    View view = inflater.inflate(resId, null);

    ((ViewPager) collection).addView(view, 0);

    tv = (TextView) collection.findViewById(R.id.swipeTitleTextView);

    tv.setText("Swipe Gallery");

    gv = (GridView) collection.findViewById(R.id.galleryGridView);

    ImageAdapter imageAdapter = new ImageAdapter(collection.getContext(), imageCount);

    gv.setAdapter(imageAdapter);

     // Set up a click listener
    gv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {
            // Get the data location of the image
            String[] projection = {MediaStore.Images.Media.DATA};
            cursor = act.managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection, // Which columns to return
                    null,       // Return all rows
                    null,
                    null);
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToPosition(position);
            // Get image filename
            String imagePath = cursor.getString(columnIndex);
            // Use this path to do further processing, i.e. full screen display

            System.out.println("=====Image Path:::::"+imagePath);
        }
    });

    return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);

}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);

}

@Override
public Parcelable saveState() {
    return null;
}

@Override
public void finishUpdate(View arg0) {
    // TODO Auto-generated method stub

}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
    // TODO Auto-generated method stub

}

@Override
public void startUpdate(View arg0) {
    // TODO Auto-generated method stub

}

public int getScrCount() {
    return scrCount;
}

public void setScrCount(int scrCount) {
    this.scrCount = scrCount;
}


private class ImageAdapter extends BaseAdapter {

    private int count;

    public ImageAdapter(Context ctx, int count) {

        this.count = count;

    }

    @Override
    public int getCount() {

        return count;

    }

    @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) {
          ImageView picturesView;
          if (convertView == null) {
              picturesView = new ImageView(act);
              // 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(8, 8, 8, 8);
              picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
          }
          else {
              picturesView = (ImageView)convertView;
          }
          return picturesView;
    }

}

}

问题是我可以加载图像并在屏幕上显示它们但不能在每个屏幕上准确显示9个图像。请仔细阅读代码,这并不复杂。

1 个答案:

答案 0 :(得分:1)

我创建了视图寻呼机的另一个演示示例来管理页面并加载图像。按照明智的步骤,它完美的工作..

它是一个寻呼机适配器和mDataHolder(图像列表)和 设置gridview android:numColumns="3"

的属性
    private int size;
    private int start;
    private int end;
    private int MATRIX = 9;


public int getCount() {
        int size = 0;
        if (mDataHolder != null)
            modular = mDataHolder.get_Listholder().size() % MATRIX;
        if (modular != 0)
            size++;

        return ((mDataHolder.get_Listholder().size()) / MATRIX) + size;
    }



    @Override
    public Object instantiateItem(ViewGroup container, final int pageIndex) {

        mRelativeLayoutInflater = (RelativeLayout) mMasterFragmentActivity.getLayoutInflater().inflate(R.layout.member_pager_adapter, container, false);
        mGridView = (GridView) mRelativeLayoutInflater.findViewById(R.id.member_gridView_list);

Calculation of pages&LT;

        size = MATRIX;
        start = pageIndex * MATRIX;
        end = 0;
        int temp = 0;

        if ((start + MATRIX) < mDataHolder.get_Listholder().size()) {
            end = start + MATRIX;
        } else {
            temp = mDataHolder.get_Listholder().size() - start;
            end = start + temp;
            size = temp;
        }
  

...然后将变量传递给gridview适配器以在寻呼机中设置其他内容。

        mAdapterMember = new AdapterMember(mContext, start, end, size);
        mGridView.setAdapter(mAdapterMember);

        ((ViewPager) container).addView(mRelativeLayoutInflater);
        return mRelativeLayoutInflater;
    }

管理页面下面的变量也使用Gridview适配器来设置位置。 当设置的页面内容位置如(start + position)

private class AdapterMember extends BaseAdapter {

    private ViewHolder mViewHolder;
    private Context mContext;
    private int start;
    private int end;
    private int size;

    private AdapterMember(Context mContext, int start, int end, int size) {
        this.mContext = mContext;
        this.start = start;
        this.end = end;
        this.size = size;
    }

    @Override
    public int getCount() {
        if (size > 0)
            return size;
        else 
            return 0;
    }
    @Override
    public Object getItem(int position) {
        if (mMemberListVO.getMember().size() > 0)
            return mMemberListVO.getMember().get(position);
        else 
            return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.row_member_list, null);
            mViewHolder = new ViewHolder();
            mViewHolder.mImageViewMemberPhoto = (NetworkImageView) convertView .findViewById(R.id.row_member_list_imageView_person);
            mViewHolder.mTextViewMemberName = (TextView) convertView .findViewById(R.id.row_member_list_textview_person_name);
            mViewHolder.mTextViewMemberDesignation = (TextView) convertView .findViewById(R.id.row_member_list_textview_person_designation);
            mViewHolder.mTextViewMemberAddress = (TextView) convertView .findViewById(R.id.row_member_list_textview_person_address);
            mViewHolder.mTextViewMemberNotification = (TextView) convertView .findViewById(R.id.row_member_list_imageview_msg_notification);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        } 


         mViewHolder.mTextViewMemberName.setText(mDataHolder.get_Listholder().get(start + position) .get(DBHelper.mFieldMember_First_Name)
                + getString(R.string.double_quate_with_space)
                + mDataHolder.get_Listholder(). get(start + position).get(DBHelper.mFieldMember_Last_Name));

        mViewHolder.mTextViewMemberDesignation.setText(mDataHolder.get_Listholder().get(start + position)
                .get(DBHelper.mFieldMember_Occupation));

        mViewHolder.mTextViewMemberAddress.setText(mDataHolder.get_Listholder().get(start + position) .get(DBHelper.mFieldMember_Block_No)
                + "," + getString(R.string.double_quate_with_space)
                + mDataHolder.get_Listholder().get(start + position).get(DBHelper.mFieldMember_House_No) + ","
                + getString(R.string.double_quate_with_space)
                + mDataHolder.get_Listholder().get(start + position).get(DBHelper.mFieldMember_Home_Address) + ".");

        return convertView;
    }

    private class ViewHolder {
        private NetworkImageView mImageViewMemberPhoto;
        private TextView mTextViewMemberName, mTextViewMemberDesignation, mTextViewMemberAddress,
                mTextViewMemberNotification;
    }
}

完美地满足您的要求...... 问题是解决加载图像并在屏幕上显示它的显示器每个屏幕恰好9个图像。