GridView练习题
Posted 1011042043
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GridView练习题相关的知识,希望对你有一定的参考价值。
package com.example.wang.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class Main2Activity extends AppCompatActivity { GridView gv_1; List<MyClass> lm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); gv_1=(GridView)findViewById(R.id.gv_1); lm=new ArrayList<MyClass>(); MyClass myClass=new MyClass(R.drawable.f1,"名称1"); lm.add(myClass); lm.add(new MyClass(R.drawable.f2,"名称2")); lm.add(new MyClass(R.drawable.f3,"名称3")); lm.add(new MyClass(R.drawable.f4,"名称4")); lm.add(new MyClass(R.drawable.f5,"名称5")); lm.add(new MyClass(R.drawable.f6,"名称6")); lm.add(new MyClass(R.drawable.f7,"名称7")); lm.add(new MyClass(R.drawable.f8,"名称8")); lm.add(new MyClass(R.drawable.f9,"名称9")); lm.add(new MyClass(R.drawable.f10,"名称10")); lm.add(new MyClass(R.drawable.f10,"名称1111")); lm.add(new MyClass(R.drawable.f10,"名称11")); GridAdapter gridAdapter=new GridAdapter(); gv_1.setAdapter(gridAdapter); } class GridAdapter extends BaseAdapter { @Override public int getCount() { return lm.size(); } @Override public Object getItem(int position) { return lm.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { MyClass myClass=lm.get(position); if (convertView==null) { Log.e("TAG", "适配器 getView=" + position); convertView=View.inflate(Main2Activity.this,R.layout.activity_main21,null); } ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_1); imageView.setImageResource(myClass.getImg()); TextView textView=(TextView)convertView.findViewById(R.id.tv_1); if (myClass.getName().length()>=5||myClass.getName().trim().length()==0) { Toast.makeText(Main2Activity.this, myClass.getName()+"不符合命名规则,请重新命名", Toast.LENGTH_LONG).show(); textView.setText(""); } else { textView.setText(myClass.getName()); } return convertView; } } class MyClass { private int img; private String name; public int getImg() { return img; } public void setImg(int img) { this.img = img; } public String getName() { return name; } public void setName(String name) { this.name = name; } public MyClass(int img,String name) { this.img=img; this.name=name; } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.wang.myapplication.Main2Activity"> <GridView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/gv_1" android:numColumns="4" android:gravity="center" android:horizontalSpacing="20dp"> </GridView> </RelativeLayout>
<?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"> <ImageView android:layout_width="70dp" android:layout_height="70dp" android:id="@+id/iv_1"/> <TextView android:layout_width="70dp" android:layout_height="wrap_content" android:gravity="center" android:id="@+id/tv_1"/> </LinearLayout>
以上是关于GridView练习题的主要内容,如果未能解决你的问题,请参考以下文章