activity_main.xml

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:padding="10dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:gravity="center">
</GridView>

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
//BaseAdapter를 상속받는다
private Context mContext;
private int[] data;

public CustomAdapter(Context mContext, int[] data){
this.mContext = mContext;
this.data = data;
}

public int getCount(){
return data.length;
}//getCount method를 통해 data의 갯수를 얻어온다
public Object getItem(int position){
return data[position];
}
public long getItemId(int position){
return position;
}

public View getView(int position, View convertView, ViewGroup parent){
ImageView imageView;

if(convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(w: 150, h: 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(left: 5, top: 5, right: 5, bottom: 5);
}else{
imageView = (ImageView)convertView;
}
imageView.setImageResource(data[position]);
return imageView;
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private int[] imgData = {R.drawable.apple, R.drawable.apple,
R.drawable.apple, R.drawable.apple, R.drawable.apple, R.drawable.apple,
R.drawable.apple, R.drawable.apple, R.drawable.apple, R.drawable.apple,
R.drawable.apple, R.drawable.apple, R.drawable.apple, R.drawable.apple};
private CustomAdapter adapter;
private GridView gridView;

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

adapter = new CustomAdapter(mContext: this, imgData);

gridView = findViewById(R.id.gridView);
gridView.setAdapter(adapter);
}
}

--------------------

activity_main.xml

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:padding="10dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:gravity="center">
</GridView>

Grid_item.java

public class Grid_item {
String apple;
int resId;

public Grid_item(String apple, int resId){
this.apple = apple;
this.resId = resId;
}

public String getApple(){
return apple;
}
public void setApple(String apple){
this.apple = apple;
}
public int getResId(){
return resId;
}
public void setResId(int resId){
this.resId = resId;
}
}

item_style.xml

<ImageView
android:id="@+id/imgView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/apple"/>
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="apple"
android:textColor="#5505ff"
android:textStyle="bold"
android:layout_gravity="center"/>

GridItemView.java

public class GridItemView extends LinearLayout {
ImageView imageView;
TextView textView;

public GridItemView(Context context){
super(context);
init(context);
}
public void init(Context context){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.item_style, this, true);

imageView = findViewById(R.id.imgView);
textView = findViewById(R.id.txtView);
}
public void setImage(int resId){
imageView.setImageResource(resId);
}
public void setApple(String apple){
textView.setText(apple);
}
}

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
//BaseAdapter를 상속받는다
private Context mContext;
ArrayList<Grid_item> data = new ArrayList<Grid_item>();

public CustomAdapter(Context mContext){
this.mContext = mContext;
}

public void addItem(Grid_item grid_item){
data.add(grid_item);
}

public int getCount(){
return data.size();
}//getCount method를 통해 data의 갯수를 얻어온다
public Object getItem(int position){
return data.get(position);
}
public long getItemId(int position){
return position;
}

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

GridItemView view = new GridItemView(mContext);
Grid_item item = data.get(position);

view.setImage(item.getResId());
view.setApple(item.getApple());

return view;
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private CustomAdapter adapter;
private GridView gridView;

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

gridView = findViewById(R.id.gridView);

adapter = new CustomAdapter(this);
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));

gridView.setAdapter(adapter);
}
}

'android studio' 카테고리의 다른 글

toast message, style  (0) 2019.08.11
inflation  (0) 2019.08.11
Spinner  (0) 2019.08.10
AdapterView(ListView)  (0) 2019.08.10
view에 도형, 텍스트 그리기  (0) 2019.08.10

+ Recent posts