如图是效果图

今天看到看到这个代码发现一个问题  就是我的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 再在上面添加 产品

先定义产品的适配器

 package org.xml.demo;

 import ogg.huanxin.huadong.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView; public class ProducteGridAdapter extends BaseAdapter {
private Context context; public ProducteGridAdapter(Context context) {
super();
this.context = context;
} private int[] costs = { 900, 768, 868, 554, 610, 152, 199, 299, 544, 366 };
private String[] title = { "休闲男装", "女装", " 儿童装", "手机", "休闲男装", "女装",
" 儿童装", "手机", "休闲男装休闲男装休闲男装", "休闲男装" }; @Override
public int getCount() {
// 在此适配器中所代表的数据集中的条目数
return costs.length;
} @Override
public Object getItem(int arg0) {
// (获取数据集中与指定索引对应的数据项)
return arg0;
} @Override
public long getItemId(int arg0) {
// 取在列表中与指定索引对应的行id
return arg0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// 获取一个在数据集中指定索引的视图来显示数据
Holder holder = null;
if (convertView == null) {
holder = new Holder();
// 根据自定义的布局来加载布局
LayoutInflater mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(R.layout.home_produce, null);
holder.ll_left = (LinearLayout) convertView
.findViewById(R.id.ll_left);
holder.ll_right = (LinearLayout) convertView
.findViewById(R.id.ll_right);
holder.product_cost = (TextView) convertView
.findViewById(R.id.product_cost);
holder.product_title = (TextView) convertView
.findViewById(R.id.product_title);
// 将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag
convertView.setTag(holder); } else { holder = (Holder) convertView.getTag(); } holder.product_cost.setText(costs[position] + "");
holder.product_title.setText(title[position]); return convertView;
} private static final class Holder {
private TextView product_title;
TextView product_cost;
LinearLayout ll_left;
LinearLayout ll_right;
}
}

其中R.layout.home_produce

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/product"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:gravity="center_horizontal"
android:orientation="vertical" > <ImageView
android:id="@+id/image_product"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="fitXY"
android:src="@drawable/name" /> <TextView
android:id="@+id/product_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_horizontal"
android:maxLines="1"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:text="@string/product"
android:textSize="15sp" /> <LinearLayout
android:id="@+id/product_ll"
android:layout_width="match_parent"
android:layout_height="30dp" > <LinearLayout
android:id="@+id/ll_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingLeft="15dp" > <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="@string/money"
android:textColor="@android:color/holo_blue_light"
android:textSize="16sp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:id="@+id/product_cost"
android:gravity="center_vertical"
android:text="@string/price"
android:textSize="16sp" >
</TextView>
</LinearLayout> <LinearLayout
android:id="@+id/ll_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="15dp" > <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="@string/xiaoshou"
android:textColor="@android:color/holo_blue_light"
android:textSize="16sp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="@string/much"
android:textSize="16sp" >
</TextView>
</LinearLayout>
</LinearLayout> </LinearLayout>

主代码

package org.xml.demo;

import ogg.huanxin.huadong.R;
import android.app.Activity;
import android.os.Bundle; public class MyProducte extends Activity {
// private GridView productGridView;
private MyGridView product_gridView;
private ProducteGridAdapter producteGridAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//定义布局变量
super.setContentView(R.layout.home_product);
//取得控件
product_gridView = (MyGridView) super.findViewById(R.id.product);
//设置适配器
producteGridAdapter = new ProducteGridAdapter(this);
product_gridView.setAdapter(producteGridAdapter);
} }

布局xml

先定义一个MyGridView

 package org.xml.demo;

 import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView; public class MyGridView extends GridView { public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyGridView(Context context) {
super(context);
} public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} @Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
} }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:paddingBottom="4dp"
android:paddingTop="5dp"
android:text="所有产品"
android:textSize="15sp" /> <View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/white" /> <ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <org.xml.demo.MyGridView
android:id="@+id/product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:numColumns="2"
android:paddingBottom="10dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="5dp"
android:verticalSpacing="8dp" />
</LinearLayout>
</ScrollView> </LinearLayout>

GridView简单使用的更多相关文章

  1. Android零基础入门第45节:GridView简单使用

    原文:Android零基础入门第45节:GridView简单使用 前面一共用了8期来学习ListView列表的相关操作,其实学习的ListView的知识完全适用于AdapterView的其他子类,如G ...

  2. Gridview转发

    首页 开源项目 问答 动弹 博客 翻译 资讯 专题 城市圈 [ 登录 | 注册 ] 博客专区 > Reya滴水心的博客详情 Asp.net中GridView使用详解(很全,很经典) Reya滴水 ...

  3. GridView的详细用法

    l GridView无代码分页排序 l GridView选中,编辑,取消,删除 l GridView正反双向排序 l GridView和下拉菜单DropDownList结合 l GridView和Ch ...

  4. Android 可拖拽的GridView效果实现, 长按可拖拽和item实时交换

    转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17718579),请尊重他人的辛勤劳动成果,谢谢! 在And ...

  5. Asp.net中GridView使用详解(很全,很经典 转来的)

    Asp.net中GridView使用详解 效果图参考:http://hi.baidu.com/hello%5Fworld%5Fws/album/asp%2Enet中以gv开头的图片 l         ...

  6. Asp.net中GridView使用详解(很全,很经典)

    http://blog.csdn.net/hello_world_wusu/article/details/4052844 Asp.net中GridView使用详解 效果图参考:http://hi.b ...

  7. android 有弹性的ScrollView 简单实现,与处理ScrollView和ListView,GridView之间的冲突

    处理ScrollView和ListView,GridView之间的冲突, 最好的办法就是继承这两个类,重写他们的onMeasure方法即可: ListView: import android.widg ...

  8. [习题] FindControl 简单练习--GridView + CheckBox,点选多列数据(复选删除)#3 List或数组

    [习题] FindControl 简单练习--GridView + CheckBox,点选多列数据(复选删除)#3 List或数组 之前的范例,使用字符串.文字来记录将删除的文章ID 后续会有很多小缺 ...

  9. 小工具:天气查询 Vs自定义设置 DevGridControl中GridView排序问题 小工具:火车票查询 小工具:邮件发送 小工具:截图&简单图像处理

    小工具:天气查询   开发一个天气查询的工具主要由两步构成,一是数据的获取,二是数据的展示.  一.数据获取 数据获取又可以分为使用其它公司提供的API和手动抓取其它网站数据. 1. 某公司提供的AP ...

随机推荐

  1. POJ2449 Remmarguts' Date A*算法

    题意是让求从st的ed第k短路... 考虑A*算法:先把终点到每个点最短路跑出来(注意要建反图),当做估价函数h(u),然后跑A* 每次取出总代价最小的,即g(u)+h(u)最小的进行扩展,注意如果u ...

  2. Logistic Regression-Cost Fuction

    1. 二分类问题 样本:  ,训练样本包含  个: 其中  ,表示样本 包含 个特征:  ,目标值属于0.1分类: 训练数据:  输入神经网络时样本数据的形状: 目标数据的形状: 2. logisti ...

  3. Json与jsonpath再认识与初识

    一.json格式的数据 1.认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写.同时也方便了机器进行解析和生成.适用于 ...

  4. Spring Annotation(注解)

    Spring Boot Annotation @SpringBootApplication 必须作用在main 方法所在类 @RequestMapping @GetMapping @PostMappi ...

  5. nginx优化项目

    隐藏版本信息 server_tokensSyntax:     server_tokens on | off | build | string;Default:     server_tokens o ...

  6. python从字符串内取两个符号之间的内容

    #取字符串中两个符号之间的东东 def txt_wrap_by(self,start_str, end, html): start = html.find(start_str) if start &g ...

  7. IE Error: '__doPostBack' is undefined 问题解决

    突然遇到个很奇怪的BUG,翻页控件,其他浏览器一切正常,IE无法翻页,会提示 '__doPostBack' is undefined 后来搜索发现: [原文發表地址] Bug and Fix: ASP ...

  8. TOJ 1840 Jack Straws

    Description In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

  9. [openStack]使用Fuel安装OpenStack juno的fuel_master

    安装OpenStack是一件很复杂的事情,特别是在想目中,如果一个组件一个组件,一台一台的coding部署,估计太消耗时间,而且出错的概率很高,所以使用工具推送部署的效率就很高了,而且必须得可靠.mi ...

  10. 九度oj题目1518:反转链表

    题目1518:反转链表 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2567 解决:948 题目描述: 输入一个链表,反转链表后,输出链表的所有元素.(hint : 请务必使用链表) ...