如图是效果图
今天看到看到这个代码发现一个问题 就是我的listView的布局不对 我的GridView的 android:layout_height="wrap_content"这样
写会导致getView()方法被重复调用了
这是什么样的情况了,看了网上的资料以后我知道原来没有设置器listview 的布局方式不是fill-parent,
而是wrap-content,会计算父控件的高度所以造成了一种反复调用情况,从而次数不确定。
而为什么会有很多组次调用呢?
问题就在于在layout中的决定ListView或者它的父元素的height和width属性的定义了。fill_parent会好一点,计算 方法会比较简单,只要跟父元素的大小相似就行,但是即使是fill_parent,也不能给View当饭吃,还是要计算出来具体的dip,所以 measure还是会被调用,只是可能比wrap_content的少一点。至于自适应的它会一直考量它的宽和高,根据内容(也就是它的子Item)计算 宽高。可能这个measure过程会反复执行,如果父元素也是wrap_content,这个过程会更加漫长。
所以,解决方法就是尽量避免自适应,除非是万不得已,固定大小或者填充的效果会比较好一些。
定义一个 GridView 再在上面添加 产品
先定义产品的适配器
1 package org.xml.demo; 2 3 import ogg.huanxin.huadong.R; 4 import android.content.Context; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 import android.widget.LinearLayout; 10 import android.widget.TextView; 11 12 public class ProducteGridAdapter extends BaseAdapter { 13 private Context context; 14 15 public ProducteGridAdapter(Context context) { 16 super(); 17 this.context = context; 18 } 19 20 private int[] costs = { 900, 768, 868, 554, 610, 152, 199, 299, 544, 366 }; 21 private String[] title = { "休闲男装", "女装", " 儿童装", "手机", "休闲男装", "女装", 22 " 儿童装", "手机", "休闲男装休闲男装休闲男装", "休闲男装" }; 23 24 @Override 25 public int getCount() { 26 // 在此适配器中所代表的数据集中的条目数 27 return costs.length; 28 } 29 30 @Override 31 public Object getItem(int arg0) { 32 // (获取数据集中与指定索引对应的数据项) 33 return arg0; 34 } 35 36 @Override 37 public long getItemId(int arg0) { 38 // 取在列表中与指定索引对应的行id 39 return arg0; 40 } 41 42 @Override 43 public View getView(int position, View convertView, ViewGroup parent) { 44 // 获取一个在数据集中指定索引的视图来显示数据 45 Holder holder = null; 46 if (convertView == null) { 47 holder = new Holder(); 48 // 根据自定义的布局来加载布局 49 LayoutInflater mInflater = LayoutInflater.from(context); 50 convertView = mInflater.inflate(R.layout.home_produce, null); 51 holder.ll_left = (LinearLayout) convertView 52 .findViewById(R.id.ll_left); 53 holder.ll_right = (LinearLayout) convertView 54 .findViewById(R.id.ll_right); 55 holder.product_cost = (TextView) convertView 56 .findViewById(R.id.product_cost); 57 holder.product_title = (TextView) convertView 58 .findViewById(R.id.product_title); 59 // 将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag 60 convertView.setTag(holder); 61 62 } else { 63 64 holder = (Holder) convertView.getTag(); 65 66 } 67 68 holder.product_cost.setText(costs[position] + ""); 69 holder.product_title.setText(title[position]); 70 71 return convertView; 72 } 73 74 private static final class Holder { 75 private TextView product_title; 76 TextView product_cost; 77 LinearLayout ll_left; 78 LinearLayout ll_right; 79 } 80 }