Use of GridView in Android Development (Interpretation of Google's official API)

Source: Internet
Author: User

The GridView displays the image in a two-dimensional movable Grid. You can use ListAdapter to automatically fill the Grid project with the entire layout. Next we will use the GridView based on the example in the official document, and click the position of the image displayed in the view.

First, create the following layout in the main. xml file.

 
 

In MainAcitivity, because you want to use the setAdapter adapter to load data, you must first create a class to inherit the BaseAdapter

public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    GridView gridview = (GridView) findViewById(R.id.gridview);    gridview.setAdapter(new ImageAdapter(this));    gridview.setOnItemClickListener(new OnItemClickListener() {        public void onItemClick(AdapterView
  parent, View v, int position, long id) {            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();        }    });}public class ImageAdapter extends BaseAdapter {    private Context mContext;    public ImageAdapter(Context c) {        mContext = c;    }    public int getCount() {        return mThumbIds.length;    }    public Object getItem(int position) {        return null;    }    public long getItemId(int position) {        return 0;    }    // create a new ImageView for each item referenced by the Adapter    public View getView(int position, View convertView, ViewGroup parent) {        ImageView imageView;        if (convertView == null) {  // if it's not recycled, initialize some attributes            imageView = new ImageView(mContext);            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);            imageView.setPadding(8, 8, 8, 8);        } else {            imageView = (ImageView) convertView;        }        imageView.setImageResource(mThumbIds[position]);        return imageView;    }    // references to our images    private Integer[] mThumbIds = {            R.drawable.sample_2, R.drawable.sample_3,            R.drawable.sample_4, R.drawable.sample_5,            R.drawable.sample_6, R.drawable.sample_7,            R.drawable.sample_0, R.drawable.sample_1,            R.drawable.sample_2, R.drawable.sample_3,            R.drawable.sample_4, R.drawable.sample_5,            R.drawable.sample_6, R.drawable.sample_7,            R.drawable.sample_0, R.drawable.sample_1,            R.drawable.sample_2, R.drawable.sample_3,            R.drawable.sample_4, R.drawable.sample_5,            R.drawable.sample_6, R.drawable.sample_7    };}

The above code has been added. setLayoutParams(ViewGroup.LayoutParams), Set the image size. setScaleType(ImageView.ScaleType)Set the image display format. setPadding(int, int, int, int)Set the image spacing. If the view passed to getView () is not null, use the locally recycled convertView object for initialization.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.