What is GridView ?
GridView layout is a use to show data in grid layout.GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.In this layout is show row and column item in gridview.Gridview is use to BaseAdapter.GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. GridView is mainly useful when we want show data in grid layout like displaying images or icons.This layout can be used to build applications like image viewer, audio or video players in order to show elements in grid view.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:columnWidth="90dp" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:gravity="center" android:stretchMode="columnWidth" > </GridView> </RelativeLayout>
What is BaseAdapter ?
Base Adapter. Adapter is a bridge between UI and data source, It helps us to fill data in the UI components.Common base class of common implementation for an Adapter that can be used in both ListView (by implementing the specialized ListAdapter interface) and Spinner (by implementing the specialized SpinnerAdapter interface).
MyAdapter.java
package com.example.bhaumik.gridviewdemo; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class MyAdapter extends BaseAdapter{ Context mContext; int img[] = {R.drawable.img_1,R.drawable.img_2,R.drawable.img_3,R.drawable.img_4,R.drawable.img_5,R.drawable.img_6,R.drawable.img_7}; public MyAdapter(Context mContext) { this.mContext = mContext; } @Override public int getCount() { return img.length; } @Override public Object getItem(int i) { return img[i]; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageResource(img[position]); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); return imageView; } }
MainActivity.java
package com.example.bhaumik.gridviewdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.GridView; public class MainActivity extends AppCompatActivity { GridView gridView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = findViewById(R.id.grid_view); gridView.setAdapter(new MyAdapter(MainActivity.this)); } }
0 comments:
Post a Comment