Android-gridview的adapter中getview问题

Android-gridview的adapter中getview问题

清晨说ぺ晚安 发布于 2016-10-23 字数 329 浏览 1025 回复 1

我在gridview的adapter中getview这样写:

public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView=getLayoutInflater().inflate(R.layout.grid_view_item, null);
}
return convertView;
}

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2017-03-04 1 楼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

你把上面的height=“match_parent”修改为85dip试试看,这个应该是因为getView的时候会去计算高度,如果你的高度不是一个“确切”的值,他会不停的重复计算。

我试了一下,你的代码,并没有出现你说的那些Log问题:
下面是我的Demo Code,你可以跑下看看

package com.hoo.testgetviewfunction;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

/**
*
* @author kesenhoo
* @since 2012-09-25
*
*/
public class MainActivity extends Activity
{
private static final String TAG = "[Demo]";
private ListView mTrackListView = null;
private TrackListAdapter mAdapter = null;
private ArrayList<Bitmap> mTrackListData = new ArrayList<Bitmap>(100);

@Override
protected void onCreate(Bundle paramBundle)
{
Log.i(TAG, "This is onCreate");
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);

mTrackListView = (ListView) this.findViewById(R.id.demolistview);

if (mAdapter == null)
{
mAdapter = new TrackListAdapter(this);
}
mTrackListView.setAdapter(mAdapter);
}

/**
* Quit Activity and Clear all value
*/
@Override
protected void onDestroy()
{
Log.i(TAG, "This is onDestroy");
super.onDestroy();

mTrackListView.setAdapter(null);
mAdapter = null;
}

@Override
protected void onResume()
{
Log.i(TAG, "This is onResume");
super.onResume();
}

@Override
protected void onPause()
{
Log.i(TAG, "This is onPause");
super.onPause();
}

/**
*
* @author hoo
*
*/
class TrackListAdapter extends BaseAdapter
{
private LayoutInflater mInflater = null;
private Bitmap mDefaultBmp = null;

public TrackListAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
mDefaultBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
for (int i = 0; i < 100; i++)
{
mTrackListData.add(i, mDefaultBmp);
}
}

@Override
public int getCount()
{
if (mTrackListData != null)
{
int size = mTrackListData.size();
Log.i(TAG, "This is getCount = " + size);
return size;
}
return 0;
}

@Override
public Object getItem(int position)
{
if (mTrackListData != null && mTrackListData.size() > position)
{
Log.i(TAG, "This is getItem");
return mTrackListData.get(position);
}
return null;
}

@Override
public long getItemId(int position)
{
Log.i(TAG, "This is getItemId");
return position;
}

class ViewHolder
{
public ImageView listImageView = null;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Log.i(TAG, "--- getView + Begin: Position = " + position);
if (convertView == null)
{
Log.i(TAG, "do inflate layout");
convertView = mInflater.inflate(R.layout.listitem, null);
}
return convertView;
}
}
}

打印的Log如下:

09-25 13:02:57.471: I/[Demo](21068): This is onCreate
09-25 13:02:57.541: I/[Demo](21068): This is getCount = 100
09-25 13:02:57.541: I/[Demo](21068): This is getCount = 100
09-25 13:02:57.541: I/[Demo](21068): This is getCount = 100
09-25 13:02:57.541: I/[Demo](21068): This is getItemId
09-25 13:02:57.541: I/[Demo](21068): This is getItemId
09-25 13:02:57.551: I/[Demo](21068): This is onResume
09-25 13:02:57.581: I/[Demo](21068): This is getCount = 100
09-25 13:02:57.711: I/[Demo](21068): This is getCount = 100
09-25 13:02:57.721: I/[Demo](21068): --- getView + Begin: Position = 0
09-25 13:02:57.721: I/[Demo](21068): do inflate layout
09-25 13:02:57.721: I/[Demo](21068): --- getView + Begin: Position = 1
09-25 13:02:57.721: I/[Demo](21068): do inflate layout
09-25 13:02:57.721: I/[Demo](21068): --- getView + Begin: Position = 2
09-25 13:02:57.721: I/[Demo](21068): do inflate layout
09-25 13:02:57.731: I/[Demo](21068): --- getView + Begin: Position = 3
09-25 13:02:57.731: I/[Demo](21068): do inflate layout
09-25 13:02:57.731: I/[Demo](21068): --- getView + Begin: Position = 4
09-25 13:02:57.731: I/[Demo](21068): do inflate layout
09-25 13:02:57.731: I/[Demo](21068): --- getView + Begin: Position = 5
09-25 13:02:57.731: I/[Demo](21068): do inflate layout
09-25 13:02:57.761: I/[Demo](21068): This is getCount = 100
09-25 13:02:57.761: I/[Demo](21068): This is getCount = 100
09-25 13:03:25.328: I/[Demo](21068): This is onPause
09-25 13:03:26.760: I/[Demo](21068): This is onResume

开始滚动后Log如下:

09-25 13:07:43.740: I/[Demo](21068): --- getView + Begin: Position = 6
09-25 13:07:43.740: I/[Demo](21068): do inflate layout
09-25 13:07:43.951: I/[Demo](21068): --- getView + Begin: Position = 7
09-25 13:07:44.962: I/[Demo](21068): --- getView + Begin: Position = 8
09-25 13:07:45.672: I/[Demo](21068): --- getView + Begin: Position = 9
09-25 13:07:45.792: I/[Demo](21068): --- getView + Begin: Position = 10
09-25 13:07:46.143: I/[Demo](21068): --- getView + Begin: Position = 11
09-25 13:07:46.663: I/[Demo](21068): --- getView + Begin: Position = 12
09-25 13:07:46.974: I/[Demo](21068): --- getView + Begin: Position = 13
09-25 13:07:47.314: I/[Demo](21068): --- getView + Begin: Position = 14
09-25 13:07:47.564: I/[Demo](21068): --- getView + Begin: Position = 15
09-25 13:07:47.924: I/[Demo](21068): --- getView + Begin: Position = 16
09-25 13:07:48.495: I/[Demo](21068): --- getView + Begin: Position = 17
09-25 13:07:48.635: I/[Demo](21068): --- getView + Begin: Position = 18
09-25 13:07:49.126: I/[Demo](21068): --- getView + Begin: Position = 19
09-25 13:07:49.236: I/[Demo](21068): --- getView + Begin: Position = 20
09-25 13:07:49.556: I/[Demo](21068): --- getView + Begin: Position = 21
09-25 13:07:49.806: I/[Demo](21068): --- getView + Begin: Position = 22
09-25 13:07:50.026: I/[Demo](21068): --- getView + Begin: Position = 23
09-25 13:07:50.447: I/[Demo](21068): --- getView + Begin: Position = 24