如图是效果图

今天看到看到这个代码发现一个问题  就是我的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. 用python脚本 从xls文件中读取数据

    导入 xlrd 第三方模块 import xlrd data = xlrd.open_workbook('test.xlsx') # 打开xls文件 table = data.sheets()[0] ...

  2. C#实现,一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第35位数是多少, 用递归算法实现

    方法函数可以通过调用自身来进行递归.计算理论可以证明递归的作用可以完全取代循环. static void Main(string[] args) { Console.WriteLine(Ra()); ...

  3. javascrip 中排序的函数怎么理解

    其中s是数组[888,2222,9,4]:我不明白sort函数中参数是如何作用的,function中的a和b又是干什么的? 那个function的作用就是比较两个数的大小用的,然后返回结果的正负作为排 ...

  4. 006 Android XML 文件布局及组件属性设置技巧汇总

    1.textview 组件文本实现替换(快速实现字符资源的调用) android 应用资源位置在 project(工程名)--->app--->res--->values 在stri ...

  5. LeetCode215. 数组中的第K个最大元素

    215. 数组中的第K个最大元素 问题描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 示例 1: 输入: [3 ...

  6. an concreate example

    Step 1: Creating Parts 1. Split the geometry 2. Create the INLET part. 3. Create the OUTLET part. 4. ...

  7. JavaScript 精简笔记

    JavaScript 精简笔记,摘自 廖雪峰的官方网站. [From] https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51 ...

  8. postgreSQL 常用命令 二

    本次测试基与PostgreSQL 10.x版本 创建用户 [postgres@rtm2 data]$ /opt/pgsql-10/bin/createuser rentaomin [postgres@ ...

  9. 克隆kvm虚拟机报错ImportError: No module named 'requests.packages.urllib3'

    2018-06-21 更新系统造成kvm克隆命令报错 virt-clone -o centos--update-clone -n centos--maven-test -f /var/lib/vmdk ...

  10. linux下目录、文件显示颜色的设置生效

    Centos系统 拷贝/etc/DIR_COLORS文件为当前主目录的 .dir_colors 命令:cp /etc/DIR_COLORS ~/.dir_colors 修改~/.dir_colors中 ...