ListView和GridView多次调用GetView的现象和解决办法

原文出自:http://blog.csdn.net/u011889786/article/details/52143268

 

背景:以前遇到过,最近又碰到了,就总结一下吧

一.ListView

(1)出现原因

是因为listView的高度不确定,一般用的是wrap_content,导致系统需要不断地测量,也就是多测调用onMeasure方法,所以就多次调用getView,所以解决方法也挺简单,把宽高写死(精确给个数字,或者match_parent).

 

(2)解决方法

adapter.Java

 

[java] view plain copy  在CODE上查看代码片派生到我的代码片

  1. package com.dx.text0808;  
  2.   
  3. import android.content.Context;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseAdapter;  
  8. import android.widget.TextView;  
  9.   
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12.   
  13. /** 
  14.  * Created by xiongchao on 2016/7/18. 
  15.  */  
  16. public class UpdateOtherGridAdapter extends BaseAdapter {  
  17.     private LayoutInflater inflater;  
  18.     private Context mContext;  
  19.     private List<String> contentList = new ArrayList<>();  
  20.   
  21.   
  22.     public UpdateOtherGridAdapter(Context context, ArrayList<String> list) {  
  23.         this.mContext = context;  
  24.         inflater = LayoutInflater.from(context);  
  25.         this.contentList = list;  
  26.     }  
  27.   
  28.     public void setContentList(List<String> contentList) {  
  29.         this.contentList = contentList;  
  30.     }  
  31.   
  32.     public void update() {  
  33.         this.notifyDataSetChanged();  
  34.     }  
  35.   
  36.     @Override  
  37.     public int getCount() {  
  38.         return contentList.size();  
  39.     }  
  40.   
  41.     @Override  
  42.     public Object getItem(int arg0) {  
  43.         return null;  
  44.     }  
  45.   
  46.     @Override  
  47.     public long getItemId(int arg0) {  
  48.         return 0;  
  49.     }  
  50.   
  51.     @Override  
  52.     public View getView(int position, View convertView, ViewGroup parent) {  
  53.         System.out.println("xcqw getView  ***position"+position);  
  54.         ViewHolder holder = null;  
  55.         if (convertView == null) {  
  56.             convertView = inflater.inflate(R.layout.item,  
  57.                     parent, false);  
  58.             holder = new ViewHolder();  
  59.             holder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);  
  60.             convertView.setTag(holder);  
  61.         } else {  
  62.             holder = (ViewHolder) convertView.getTag();  
  63.         }  
  64.   
  65.         String content = contentList.get(position);  
  66.         holder.tvContent.setText(content);  
  67.         return convertView;  
  68.     }  
  69.   
  70.     public class ViewHolder {  
  71.         public TextView tvContent;  
  72.     }  
  73.   
  74.   
  75. }  

 

 

 

 

 

<1>会出现多次调用getView的写法

activity_main.xml

 

[java] view plain copy  在CODE上查看代码片派生到我的代码片

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.     >  
  9.   
  10.     <Button  
  11.         android:id="@+id/bt_refresh"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="点我更新listView"/>  
  15.   
  16.     <ListView  
  17.         android:id="@+id/gv"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content">  
  20.     </ListView>  
  21. </LinearLayout>  

注意看到 ListView的Layout_height中写的是wrap_content

 

 

实际打印如下:
08-07 16:39:18.604 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position0
08-07 16:39:18.609 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position1
08-07 16:39:18.609 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position2
08-07 16:39:18.668 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position0
08-07 16:39:18.669 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position1
08-07 16:39:18.679 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position2
08-07 16:39:18.691 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position0
08-07 16:39:18.693 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position1
08-07 16:39:18.707 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position2
08-07 16:39:18.784 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position0
08-07 16:39:18.785 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position1
08-07 16:39:18.786 25086-25086/com.dx.text0808 I/System.out: xcqw getView  ***position2

 

 

<2>不会出现多次调用getView的写法

 

[java] view plain copy  在CODE上查看代码片派生到我的代码片

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.     >  
  9.   
  10.     <Button  
  11.         android:id="@+id/bt_refresh"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="点我更新listView"/>  
  15.   
  16.     <ListView  
  17.         android:id="@+id/gv"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="match_parent">  
  20.     </ListView>  
  21. </LinearLayout>  


注意看到现在ListView中的Layout_height 写的是Match_parent

 

 

实际打印如下:

 

[java] view plain copy  在CODE上查看代码片派生到我的代码片

  1. 08-07 16:42:48.892 28432-28432/com.dx.text0808 I/System.out: xcqw getView  ***position0  
  2. 08-07 16:42:48.905 28432-28432/com.dx.text0808 I/System.out: xcqw getView  ***position1  
  3. 08-07 16:42:48.910 28432-28432/com.dx.text0808 I/System.out: xcqw getView  ***position2  

 

 

 

 

 

 

二.GridView

(1)出现原因

貌似不管长宽写不写死都是会多次调用getView,不知道为啥,日了狗了。。。原因待补充

 

(2)解决方法

<1>出现的情形

adapter.xml

 

[java] view plain copy  在CODE上查看代码片派生到我的代码片

  1. package com.dx.text0807;  
  2.   
  3. import android.content.Context;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseAdapter;  
  8. import android.widget.TextView;  
  9.   
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12.   
  13. /** 
  14.  * Created by xiongchao on 2016/7/18. 
  15.  */  
  16. public class UpdateOtherGridAdapter extends BaseAdapter {  
  17.     private LayoutInflater inflater;  
  18.     private Context mContext;  
  19.     private List<String> contentList = new ArrayList<>();  
  20.   
  21.   
  22.     public UpdateOtherGridAdapter(Context context, ArrayList<String> list) {  
  23.         this.mContext = context;  
  24.         inflater = LayoutInflater.from(context);  
  25.         this.contentList = list;  
  26.     }  
  27.   
  28.     public void update() {  
  29.         this.notifyDataSetChanged();  
  30.     }  
  31.   
  32.     @Override  
  33.     public int getCount() {  
  34.         return contentList.size();  
  35.     }  
  36.   
  37.     @Override  
  38.     public Object getItem(int arg0) {  
  39.         return null;  
  40.     }  
  41.   
  42.     @Override  
  43.     public long getItemId(int arg0) {  
  44.         return 0;  
  45.     }  
  46.   
  47.     @Override  
  48.     public View getView(int position, View convertView, ViewGroup parent) {  
  49.         System.out.println("xcqw getView  1***position"+position);  
  50.   
  51.         ViewHolder holder = null;  
  52.         if (convertView == null) {  
  53.             convertView = inflater.inflate(R.layout.item,  
  54.                     parent, false);  
  55.             holder = new ViewHolder();  
  56.             holder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);  
  57.             convertView.setTag(holder);  
  58.         } else {  
  59.             holder = (ViewHolder) convertView.getTag();  
  60.         }  
  61.   
  62.   
  63.         if(((MyGridView) parent).isOnMeasure){  
  64.             //如果是onMeasure调用的就立即返回  
  65.             return convertView;  
  66.         }  
  67.         System.out.println("xcqw getView  2***position"+position);  
  68.   
  69.         String content = contentList.get(position);  
  70.         holder.tvContent.setText(content);  
  71.   
  72.   
  73.     //如果不是onMeasure调用的就可以正常操作了  
  74.     //赋值操作  
  75.         return convertView;  
  76.     }  
  77.   
  78.     public class ViewHolder {  
  79.         public TextView tvContent;  
  80.     }  
  81.   
  82.   
  83. }  

 

 

 

 

 

 

activity_main.xml

 

[java] view plain copy  在CODE上查看代码片派生到我的代码片

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="com.dx.text0807.MainActivity">  
  8.   
  9.     <com.dx.text0807.MyGridView  
  10.         android:id="@+id/gv"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="match_parent"  
  13.         >  
  14.     </com.dx.text0807.MyGridView>  
  15.     <!--android:layout_height="wrap_content"-->  
  16.     <!--android:layout_height="50dp"-->  
  17. </RelativeLayout>  

 

 

 

 

 

注意了!!!,不管你长宽怎么写都是会多次调用getView,不知道网上说写死就可以的人是怎么整的。。。

 

<2>解决方法

不让他多次调用getView是不可能了,所以只能说在他onMeasure调用getView的时候我们不要去对convertView做任务赋值的操作,只有当onLayout的时候我们再对ConvertView操作

首先我们要重写一个gridView

MyGridView.java

 

[java] view plain copy  在CODE上查看代码片派生到我的代码片

  1. public class MyGridView extends GridView {  
  2.     public boolean isOnMeasure;  
  3.     public MyGridView(Context context) {  
  4.         super(context);  
  5.     }  
  6.   
  7.     public MyGridView(Context context, AttributeSet attrs) {  
  8.         super(context, attrs);  
  9.     }  
  10.   
  11.   
  12.     public MyGridView(Context context, AttributeSet attrs, int defStyle) {  
  13.         super(context, attrs, defStyle);  
  14.     }  
  15.   
  16.     @Override  
  17.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  18.         isOnMeasure = true;  
  19.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  24.         isOnMeasure = false;  
  25.         super.onLayout(changed, l, t, r, b);  
  26.     }  
  27. }  

 

 

getView中对ConvertView的处理

 

[java] view plain copy  在CODE上查看代码片派生到我的代码片

  1. @Override  
  2.     public View getView(int position, View convertView, ViewGroup parent) {  
  3.         System.out.println("xcqw getView  1***position"+position);  
  4.   
  5.         ViewHolder holder = null;  
  6.         if (convertView == null) {  
  7.             convertView = inflater.inflate(R.layout.item,  
  8.                     parent, false);  
  9.             holder = new ViewHolder();  
  10.             holder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);  
  11.             convertView.setTag(holder);  
  12.         } else {  
  13.             holder = (ViewHolder) convertView.getTag();  
  14.         }  
  15.   
  16.   
  17.         if(((MyGridView) parent).isOnMeasure){  
  18.             //如果是onMeasure调用的就立即返回  
  19.             return convertView;  
  20.         }  
  21.         System.out.println("xcqw getView  2***position"+position);  
  22.   
  23.         String content = contentList.get(position);  
  24.         holder.tvContent.setText(content);  
  25.   
  26.   
  27.     //如果不是onMeasure调用的就可以正常操作了  
  28.     //赋值操作  
  29.         return convertView;  
  30.     }  

 

 

 

 

 

实际打印

08-07 17:11:37.119 17294-17294/com.dx.text0807 I/System.out: xcqw getView  1***position0
08-07 17:11:37.178 17294-17294/com.dx.text0807 I/System.out: xcqw getView  1***position0
08-07 17:11:37.268 17294-17294/com.dx.text0807 I/System.out: xcqw getView  1***position0
08-07 17:11:37.268 17294-17294/com.dx.text0807 I/System.out: xcqw getView  2***position0
08-07 17:11:37.269 17294-17294/com.dx.text0807 I/System.out: xcqw getView  1***position1
08-07 17:11:37.271 17294-17294/com.dx.text0807 I/System.out: xcqw getView  2***position1
08-07 17:11:37.272 17294-17294/com.dx.text0807 I/System.out: xcqw getView  1***position2
08-07 17:11:37.273 17294-17294/com.dx.text0807 I/System.out: xcqw getView  2***position2
08-07 17:11:37.274 17294-17294/com.dx.text0807 I/System.out: xcqw getView  1***position3
08-07 17:11:37.276 17294-17294/com.dx.text0807 I/System.out: xcqw getView  2***position3
08-07 17:11:37.279 17294-17294/com.dx.text0807 I/System.out: xcqw getView  1***position0
08-07 17:11:37.281 17294-17294/com.dx.text0807 I/System.out: xcqw getView  1***position0
08-07 17:11:37.373 17294-17294/com.dx.text0807 I/System.out: xcqw getView  1***position0
08-07 17:11:37.376 17294-17294/com.dx.text0807 I/System.out: xcqw getView  1***position0

 

可以看到  getView2***  打印只有四次  ,效果达到了,哈哈

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值