继承AppCompatActivity 与 Activity 差别
前者是后者子类
继承前者会有标题栏 后者没有
Adapter接口
public class MyGridViewAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
public MyGridViewAdapter(Context context){
this.mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
//数量有多少
return 10;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
static class ViewHolder{
public ImageView imageView;
public TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
convertView = mLayoutInflater.inflate(R.layout.layout_grid_item,null);
holder = new ViewHolder();
holder.imageView = convertView.findViewById(R.id.iv_grid);
holder.textView = convertView.findViewById(R.id.iv_title);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//赋值
holder.textView.setText("测试");
Glide.with(mContext).load("https://www.baidu.com/img/flexible/logo/pc/peak-result.png").into(holder.imageView);
return convertView;
}
}
设置单个样式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/iv_grid"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:background="#FF9900"
/>
<TextView
android:id="@+id/iv_title"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="hello"
android:textColor="#FF9900"
android:gravity="center"
android:layout_marginTop="10dp"
/>
<!--gravity对齐方式-->
</LinearLayout>
整体样式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gv"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
/>
<!--android:numColumns="3" 三列
android:horizontalSpacing="10dp" 水平间距
android:verticalSpacing="10dp" 竖直间距
-->
</LinearLayout>
主程序设置及点击事件
public class GrideViewActivity extends AppCompatActivity {
private GridView mgv;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
mgv =findViewById(R.id.gv);
mgv.setAdapter(new MyGridViewAdapter(GrideViewActivity.this));
mgv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(GrideViewActivity.this,"点击"+position,Toast.LENGTH_SHORT).show();
}
});
mgv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(GrideViewActivity.this,"长按"+position,Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
效果图