Android GridView サンプル
Activityレイアウト
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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="info.typea.kaigiutil.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <GridView android:id="@+id/grid_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="auto_fit" android:gravity="center"> </GridView> </LinearLayout> </android.support.constraint.ConstraintLayout>
Activity
package info.typea.kaigiutil; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.GridView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView gridView = (GridView)findViewById(R.id.grid_menu); gridView.setAdapter(new MenuItemAdapter(this)); } }
Adapter
package info.typea.kaigiutil; import android.content.Context; import android.media.Image; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; /** * Created by pppiroto on 2017/09/09. */ public class MenuItemAdapter extends BaseAdapter{ private Context context; private Integer[] menuItemThumbIds = { R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher }; public MenuItemAdapter(Context context) { this.context = context; } @Override public int getCount() { return menuItemThumbIds.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(this.context); imageView.setLayoutParams(new GridView.LayoutParams(85,85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(0,0,0,0); } else { imageView = (ImageView) convertView; } imageView.setImageResource(menuItemThumbIds[position]); return imageView; } }
開発環境や、わかりにくい概念的な部分が体系的に説明されておりわかりやすい良書
ネットで情報を無料で入手できるからこそ、体系的なことを理解していると効率が良いと思う