【问题标题】:Why is my adapter's getView() executing infinite times?为什么我的适配器的 getView() 执行无限次?
【发布时间】:2013-11-13 10:26:25
【问题描述】:

在我的应用程序中,ViewPager 内部有一个 GridView

我没有在任何地方使用 match_parentwrap_content。我正在使用200dp150dp 之类的值。那么为什么我的getView() GridView 适配器会调用多次呢?

我搜索了很多类似herehereherehere

这是我的适配器类。

public class CustomGridViewAdapter1 extends BaseAdapter {
    ArrayList<Integer> list2 = new ArrayList<Integer>();
    private LayoutInflater mInflater;

    public CustomGridViewAdapter1(Context context, ArrayList<Integer> list2) {
        mInflater = LayoutInflater
                .from(context.getApplicationContext());
        this.list2 = list2;
    }

    @Override
    public int getCount() {
        return 6;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            View hView = convertView;
            if (convertView == null) {
                hView = mInflater.inflate(R.layout.greeditem, null);
                ViewHolder1 holder = new ViewHolder1();
                holder.song_pic = (ImageView) hView.findViewById(R.id.pic1);
                holder.name = (TextView) hView
                        .findViewById(R.id.tabletext1);
                holder.layout = (LinearLayout) hView
                        .findViewById(R.id.bingo_rlayout1);
                hView.setTag(holder);
            }

            ViewHolder1 holder = (ViewHolder1) hView.getTag();
            imageLoader.DisplayImage(list1.get(position), holder.song_pic);
            holder.name.setText(list.get(position));
            if (list2.get(position) == 1) {
                holder.layout.setBackgroundColor(getActivity()
                        .getResources().getColor(R.color.lightblue));
                holder.name.setTextColor(getActivity().getResources().getColor(R.color.white));
            }

            return hView;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

这是我的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="320dp"
        android:layout_height="250dp"
        android:layout_below="@+id/ticketText"
        android:numColumns="2" 
        android:background="#fff"
        android:listSelector="@android:color/white">

    </GridView>

     <TextView
        android:id="@+id/scoreId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/ticketText"
        android:layout_alignBottom="@+id/ticketText"
        android:layout_alignParentRight="true"
        android:layout_marginRight="32dp"
        android:text="Score 1/6"
        android:textColor="#000000"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/ticketText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tablayout"
        android:layout_marginLeft="16dp"
        android:text="My Ticket #1"
        android:textColor="#000000"
        android:textSize="16sp" />

</RelativeLayout>

【问题讨论】:

    标签: android android-adapter android-gridview


    【解决方案1】:

    我们需要检查getView() 中的空值条件。试试下面的代码:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        ViewHolderItem viewHolder;
    
        /*
         * The convertView argument is essentially a "ScrapView" as described is Lucas post 
         * http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
         * It will have a non-null value when ListView is asking you recycle the row layout. 
         * So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
         */
        if(convertView==null){
    
            // inflate the layout
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);
    
            // well set up the ViewHolder
            viewHolder = new ViewHolderItem();
            viewHolder.textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);
    
            // store the holder with the view.
            convertView.setTag(viewHolder);
    
        }else{
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            viewHolder = (ViewHolderItem) convertView.getTag();
        }
    
        // object item based on the position
        ObjectItem objectItem = data[position];
    
        // assign values if the object is not null
        if(objectItem != null) {
            // get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
            viewHolder.textViewItem.setText(objectItem.itemName);
            viewHolder.textViewItem.setTag(objectItem.itemId);
        }
    
        return convertView;
    
    }
    

    【讨论】:

    • 我认为可能还有其他问题。我使用了你的代码但得到了相同的结果。你能告诉我为什么会这样吗。wt是getview调用无限次的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多