码迷,mamicode.com
首页 > 其他好文 > 详细

View(视图)——GridView作业

时间:2016-05-21 01:21:09      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.wang.testapp2.TestActivity10">
11 
12     <GridView
13         android:layout_width="match_parent"
14         android:layout_height="match_parent"
15         android:id="@+id/gv_1"
16         android:numColumns="4"
17         android:stretchMode="columnWidth"
18         android:gravity="center"
19         android:horizontalSpacing="30dp"
20         ></GridView>
21 
22 </LinearLayout>
.xml
技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6 
 7     <ImageView
 8         android:layout_width="110dp"
 9         android:layout_height="110dp"
10         android:id="@+id/iv_3"/>
11     <TextView
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:id="@+id/tv_9"
15         android:text="内容"/>
16 
17 </LinearLayout>
listview_layout
技术分享
  1 package com.example.wang.testapp2;
  2 
  3 import android.app.ActionBar;
  4 import android.support.v7.app.AppCompatActivity;
  5 import android.os.Bundle;
  6 import android.util.Log;
  7 import android.view.View;
  8 import android.view.ViewGroup;
  9 import android.widget.AbsListView;
 10 import android.widget.BaseAdapter;
 11 import android.widget.GridView;
 12 import android.widget.ImageView;
 13 import android.widget.ListView;
 14 import android.widget.TextView;
 15 
 16 import java.util.ArrayList;
 17 import java.util.List;
 18 
 19 public class TestActivity10 extends AppCompatActivity {
 20 
 21     GridView gv_1;
 22 
 23     List<MyClass> lm;
 24 //    List<Integer> liv;
 25 
 26     @Override
 27     protected void onCreate(Bundle savedInstanceState) {
 28         super.onCreate(savedInstanceState);
 29         setContentView(R.layout.activity_test10);
 30 
 31         GridView gv_1=(GridView)findViewById(R.id.gv_1);
 32 
 33         lm=new ArrayList<>();
 34 
 35         MyClass myClass=new MyClass(R.drawable.f1,"美食1");
 36 
 37         lm.add(myClass);
 38 
 39         lm.add(new MyClass(R.drawable.f2,"美食2"));
 40         lm.add(new MyClass(R.drawable.f3,"美食3"));
 41         lm.add(new MyClass(R.drawable.f4,"美食4"));
 42         lm.add(new MyClass(R.drawable.f5,"美食5"));
 43         lm.add(new MyClass(R.drawable.f6,"美食6"));
 44         lm.add(new MyClass(R.drawable.f7,"美食7"));
 45         lm.add(new MyClass(R.drawable.f8,"美食8"));
 46         lm.add(new MyClass(R.drawable.f9,"美食9"));
 47         lm.add(new MyClass(R.drawable.f10,"美食10"));
 48 
 49         GridAdapter gridAdapter=new GridAdapter();
 50 
 51         gv_1.setAdapter(gridAdapter);
 52 
 53 
 54 
 55 
 56 
 57 //        //1 数据 图片的id
 58 //        liv =new ArrayList<>();
 59 //        liv.add(R.drawable.f1);
 60 //        liv.add(R.drawable.f2);
 61 //        liv.add(R.drawable.f3);
 62 //        liv.add(R.drawable.f4);
 63 //        liv.add(R.drawable.f5);
 64 //        liv.add(R.drawable.f6);
 65 //        liv.add(R.drawable.f7);
 66 //        liv.add(R.drawable.f8);
 67 //        liv.add(R.drawable.f9);
 68 //        liv.add(R.drawable.f10);
 69 //
 70 //        gv_1.setAdapter(new ImageAdapter());
 71 
 72     }
 73 
 74     class  MyClass
 75     {
 76         private int img;
 77 
 78         private String name;
 79 
 80 
 81         public int getImg() {
 82             return img;
 83         }
 84 
 85         public void setImg(int img) {
 86             this.img = img;
 87         }
 88 
 89         public String getName() {
 90             return name;
 91         }
 92 
 93         public void setName(String name) {
 94             this.name = name;
 95         }
 96 
 97         public MyClass(int img,String name)
 98         {
 99             this.img=img;
100             this.name=name;
101         }
102     }
103 
104 
105     // 适配器
106 
107 //    class  ImageAdapter extends BaseAdapter
108 //    {
109 //        @Override
110 //        public int getCount() {
111 //            return lm.size();
112 //        }
113 //
114 //        @Override
115 //        public Object getItem(int position) {
116 //            return lm.get(position);
117 //        }
118 //
119 //        @Override
120 //        public long getItemId(int position) {
121 //            return 0;
122 //        }
123 //
124 //        @Override
125 //        public View getView(int position, View convertView, ViewGroup parent) {
126 
127 //            //1. 得到数据 图片的id
128 //            Integer ivid=liv.get(position);
129 //
130 //            //2.准备View
131 //            if (convertView==null)
132 //            {
133 //                convertView = new ImageView(TestActivity10.this);
134 //            }
135 //
136 //            //3.适配
137 //            // 转换
138 //            ImageView imageView=(ImageView)convertView;
139 //
140 //            imageView.setImageResource(ivid);
141 //
142 ////            imageView.setMaxWidth(70);
143 ////            imageView.setMaxHeight(70);
144 //
145 //            imageView.setLayoutParams(new GridView.LayoutParams(110,110));
146 //
147 //
148 //            return imageView;
149 
150     class GridAdapter extends BaseAdapter
151     {
152         @Override
153         public int getCount() {
154             return lm.size();
155         }
156 
157         @Override
158         public Object getItem(int position) {
159             return lm.get(position);
160         }
161 
162         @Override
163         public long getItemId(int position) {
164             return 0;
165         }
166 
167         @Override
168         public View getView(int position, View convertView, ViewGroup parent) {
169             MyClass myClass=lm.get(position);
170 
171             if (convertView==null)
172             {
173                 Log.e("TAG","适配器getView="+position);
174 
175                 convertView=View.inflate(TestActivity10.this,R.layout.listview_layout,null);
176             }
177 
178             ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_3);
179             imageView.setImageResource(myClass.getImg());
180 
181             TextView textView=(TextView)convertView.findViewById(R.id.tv_9);
182             textView.setText(myClass.getName());
183 
184 
185             return convertView;
186         }
187     }
188 
189 }
.java

技术分享

View(视图)——GridView作业

标签:

原文地址:http://www.cnblogs.com/arxk/p/5513870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!