网格视图GrideView

103 篇文章 1 订阅

继承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;
            }
        });
    }


}

效果图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值