Android的列表视图:getView被称为不可观察的观点多次 [英] Android Listview: getView being called multiple times on unobservable views

查看:125
本文介绍了Android的列表视图:getView被称为不可观察的观点多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我已经看到<一个href=\"http://stackoverflow.com/questions/2618272/custom-listview-adapter-getview-method-being-called-multiple-times-and-in-no-co\">this类似的问题)
我有一个的ListView 为此我写了一个自定义适配器,以及 onitemclicklistener 。我在哪里,在选择该列表中的任何元件的问题, getView 被称为(两次)对于每个的ListView ,即使这些内容是不可见的。出现这种情况,即使我不叫 notifyDataSetChanged 适配器上 - 那些最初的4次被取无论两次。这是正常的行为呢?我的问题是没有那么多,它的被称为两次他们,但它是被称为在所有的更新并不需要他们的时候。

(I have already seen this similar question) I have a ListView for which I've written a custom adapter, and an onitemclicklistener. I'm having an issue where, when any element of the list is selected, getView is called (twice) for each of the top 4 elements of the ListView, even if those elements are not visible. This happens even if I don't call notifyDataSetChanged on the adapter - those first 4 views are fetched twice regardless. Is this normal behavior? My issue is not as much that it's being called twice for them, but that it is being called at all when updating them is not needed.

顺便说一句,我不使用WRAP_CONTENT为ListView的高度或宽度 - 高度match_parent,宽度为DP固定数量的

By the way, I am not using wrap_content for the height or width of the listview - the height is match_parent and the width is a fixed number of dp.

在OnItemClickListener的 onItemClick()方法是在这里:

The onItemClick() method of the OnItemClickListener is here:

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

      mPicture = pictures[position];
      mPicturesAdapter.setCurrentPicture(mPicture);
      mPicturesAdapter.notifyDataSetChanged();
}

getView()从我的自定义适配器(延伸BaseAdapter)是在这里:

getView() from my custom Adapter (which extends BaseAdapter) is here:

public View getView(int position, View convertView, ViewGroup parent) {


    Log.v("tag", "Getting view for position "+position);

    LayoutInflater inflater = LayoutInflater.from(mContext);
    LinearLayout layout = (LinearLayout)
            inflater.inflate(R.layout.picture_thumbnail, parent, false);

// set up the linearlayout here ...

    return layout;
} 

在任何项目的点击,getView()被调用的位置0 - 3无论两次单击了哪个项目

On any item click, getView() is called for positions 0 - 3 twice regardless of which item was clicked.

推荐答案

其实无论你是使用填充的ListView列表/组,你需要先清空,然后调用它。例如,如果您使用利斯塔填充的ListView,在第二或任何连续更新您需要先清空利斯塔然后添加项目,然后填充使用它。

Actually whatever List/Group you are using to populate the ListView, you need to first empty it and then recall it. For example, if you use ListA to populate the ListView, in the second or any consecutive update you need to empty the ListA first and then add items and then populate using it.

这篇关于Android的列表视图:getView被称为不可观察的观点多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆