在Android中,GridView控件变得更大,而不仅仅是图片更大

时间:2013-10-30 07:26:06

标签: android android-gridview

我需要一些帮助。

在我的gridview中,gridview的项目中有一个imageView和TextView。我希望GridView图片更大,发现只是图片更大,但控件不会改变。所以我想让GridView控件更大。

我尝试实现图片效果,我希望GridView控件变得更大,而不仅仅是图片更大。请帮助我,非常感谢。

MainActivity.java

private GridView mGridview;

private List<PhotoLink> mProgramList;

private ImageView imageView;

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

    mProgramList = new ArrayList<PhotoLink>();
    for (PhotoLink mPhotoL : Data.broadcastLiving) {
        mProgramList.add(mPhotoL);
    }
    mGridview = (GridView) findViewById(R.id.gridview);
    mGridview.setAdapter(new ImageAdapter(this, mProgramList));
    mGridview.setOnItemClickListener(new GridViewOnItemClickListener());
    mGridview.setOnItemSelectedListener(new GridViewItemSelectedListener());
    mGridview.setOnFocusChangeListener(new GridViewOnFocusChangeListener(
            mGridview));
    }

    private class GridViewOnItemClickListener implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT)
                    .show();// 显示信息;
        }
    }

    private class GridViewItemSelectedListener implements
            OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            System.out.println("arg2........." + arg2);
            ImageAdapter imageAdapter = (ImageAdapter) arg0.getAdapter();
            System.out.println("arg1.getWidth()=====" + arg1.getWidth());
            System.out.println("arg1.getHeight()=====" + arg1.getHeight());
            imageAdapter.notifyDataSetChanged(arg2);
        }

        public void onNothingSelected(AdapterView<?> arg0) {

        }

    }

    private class GridViewOnFocusChangeListener implements
            OnFocusChangeListener {
        private ImageAdapter mImageAdapter;
        private GridView mGridView;

        public GridViewOnFocusChangeListener(GridView gridView) {
            mGridView = gridView;
            mImageAdapter = (ImageAdapter) mGridView.getAdapter();
        }

        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                mImageAdapter.notifyDataSetChanged(-1);
            } else {
                mImageAdapter.notifyDataSetChanged(mGridView
                        .getSelectedItemPosition());
            }
        }

    }

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private List<PhotoLink> mPhotoLink = null;
private int selected = -1;

public ImageAdapter(Context c, List<PhotoLink> photoLink) {
    this.mContext = c;
    this.mPhotoLink = photoLink;
}

@Override
public int getCount() {
    return mPhotoLink.size();
}

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

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

public void notifyDataSetChanged(int id) {
    selected = id;
    super.notifyDataSetChanged();
}

public class ViewHolder {
    ImageView screenShotImage;
    TextView tvProgramID;

    public ViewHolder(View view) {
        this.screenShotImage = (ImageView) view
                .findViewById(R.id.screen_shot);
        this.tvProgramID = (TextView) view.findViewById(R.id.tv_program_id);
    }
}

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

    View view = null;
    ViewHolder holder = null;
    view = View.inflate(mContext, R.layout.programrunning, null);
    holder = new ViewHolder(view);
    if (selected == position) {
        Animation testAnim = AnimationUtils.loadAnimation(mContext,
                R.anim.anim);
        holder.screenShotImage.startAnimation(testAnim);
        // holder.screenShotImage.setScaleType(ScaleType.CENTER_CROP);
    } else {
        holder.screenShotImage.setScaleType(ScaleType.CENTER_INSIDE);
    }
    holder.screenShotImage.setImageResource(mPhotoLink.get(position)
            .getPhotoID());
    return view;
}

Data.java

public class Data {

public static final PhotoLink[] broadcastLiving = {
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.ic_launcher, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world),
        new PhotoLink(R.drawable.scenery, R.string.hello_world) };

}

PhotoLink

class PhotoLink {
private int photoID;
private int programID;

public PhotoLink(int photoID, int programID) {
    this.setPhotoID(photoID);
    this.setprogramID(programID);
}

public int getPhotoID() {
    return photoID;
}

public void setPhotoID(int photoID) {
    this.photoID = photoID;
}

public int getprogramID() {
    return programID;
}

public void setprogramID(int programID) {
    this.programID = programID;
}

1 个答案:

答案 0 :(得分:0)

可能你将GridView设置为wrap-content。