0

我为我的 android 布局做了一个网格视图,但我不知道如何在网格视图上放置一个意图。有什么办法可以让我有意发起另一项活动?我已经尝试了几天,但我似乎无法弄清楚如何去做。

public class PregnancyStages extends Activity implements OnItemClickListener {

private GridView photoGrid;
private int mPhotoSize, mPhotoSpacing;
private ImageAdapter imageAdapter;

// Some items to add to the GRID
private static final String[] CONTENT = new String[] { "Pregnancy Stages", "Complications", "Diet And Fitness", "Myths And Facts",
        "FAQ's", "Helplines" };
private static final int[] ICONS = new int[] { R.drawable.baby1, R.drawable.baby2,
        R.drawable.baby3, R.drawable.baby4, R.drawable.baby5, R.drawable.baby6 };

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


    // get the photo size and spacing
    mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
    mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);

    // initialize image adapter
    imageAdapter = new ImageAdapter();

    photoGrid = (GridView) findViewById(R.id.albumGrid);

    // set image adapter to the GridView
    photoGrid.setAdapter(imageAdapter);

    // get the view tree observer of the grid and set the height and numcols dynamically
    photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (imageAdapter.getNumColumns() == 0) {
                final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                if (numColumns > 0) {
                    final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                    imageAdapter.setNumColumns(numColumns);
                    imageAdapter.setItemHeight(columnWidth);

                }
            }
        }
    });
}

// ///////// ImageAdapter class /////////////////
public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private int mItemHeight = 0;
    private int mNumColumns = 0;
    private RelativeLayout.LayoutParams mImageViewLayoutParams;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }

    public int getCount() {
        return CONTENT.length;
    }

    // set numcols
    public void setNumColumns(int numColumns) {
        mNumColumns = numColumns;
    }

    public int getNumColumns() {
        return mNumColumns;
    }

    // set photo item height
    public void setItemHeight(int height) {
        if (height == mItemHeight) {
            return;
        }
        mItemHeight = height;
        mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
        notifyDataSetChanged();
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(final int position, View view, ViewGroup parent) {

        if (view == null)
            view = mInflater.inflate(R.layout.photo_item, null);

        ImageView cover = (ImageView) view.findViewById(R.id.cover);
        TextView title = (TextView) view.findViewById(R.id.title);

        cover.setLayoutParams(mImageViewLayoutParams);

        // Check the height matches our calculated column width
        if (cover.getLayoutParams().height != mItemHeight) {
            cover.setLayoutParams(mImageViewLayoutParams);
        }

        cover.setImageResource(ICONS[position % ICONS.length]);
        title.setText(CONTENT[position % CONTENT.length]);

        return view;
    }
}

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    // TODO Auto-generated method stub

    Intent myIntent = null;
    if(position == 0){
        myIntent = new Intent(v.getContext(), PregnancyStagesGrid1.class);
    }
    if(position == 1){
        myIntent = new Intent(v.getContext(), PregnancyStagesGrid2.class);
    }
    startActivity(myIntent);


}}

有谁知道我可以把这个源代码放在哪里?

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

    gridview.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            // TODO Auto-generated method stub

            Intent myIntent = null;
            if(position == 0){
                myIntent = new Intent(v.getContext(), PregnancyStagesGrid1.class);
            }
            if(position == 1){
                myIntent = new Intent(v.getContext(), PregnancyStagesGrid2.class);
            }
            startActivity(myIntent);
        }
    });
}
4

1 回答 1

0

你忘了添加 setOnItemClickListener 监听器

photoGrid.setOnItemClickListener(this);
于 2013-09-18T17:20:25.580 回答