简单的GridView用法

本文介绍了一个使用Java实现的简单GridView应用案例,通过自定义适配器展示了一组字符串数据。该示例展示了如何初始化数据源、设置GridView、创建自定义适配器以及填充视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//Mainactivity

public class MainActivity extends AppCompatActivity {
    ArrayList<String> g1arr = new ArrayList<>();
    private GridView gv1;
    private MainActivity.gv1spq gv1spq;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//获取ID
        gv1 = (GridView) findViewById(R.id.gv1);
        g1shuju();
//连接适配器
        gv1spq = new gv1spq();
        gv1.setAdapter(gv1spq);

    }

//适配器
    class gv1spq extends BaseAdapter {

        @Override
        public int getCount() {
            return g1arr.size();
        }

        @Override
        public Object getItem(int i) {
            return g1arr.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        class chen{
            TextView name;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            String s = g1arr.get(i);
            chen chen = new chen();
            if(view==null){
                view = View.inflate(MainActivity.this, R.layout.gv1buju, null);
                chen.name=view.findViewById(R.id.te);
                view.setTag(chen);
            }else{
                chen = (chen) view.getTag();
            }
            chen.name.setText(s);
            return view;
        }
    }
//展示的数据
    public void g1shuju(){
       g1arr.add("速度");
       g1arr.add("东方红");
       g1arr.add("是呢");
       g1arr.add("东方红");
       g1arr.add("地方红");
       g1arr.add("东方红");
       g1arr.add("可分红");
       g1arr.add("可分红");
       g1arr.add("可分红");
    }
}


//gv1arr泛型

public class gv1bean {
    private String text;

    @Override
    public String toString() {
        return "gv1bean{" +
                "text='" + text + '\'' +
                '}';
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public gv1bean(String text) {
        this.text = text;
    }
}


//绘制的试图的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/te"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</LinearLayout>


//Maincactivity  XML布局

<LinearLayout
    android:orientation="vertical"
    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="com.example.gridview.MainActivity">

    <GridView
        android:id="@+id/gv1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:columnWidth="90dp"
        android:numColumns="4"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        />

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值