Listview非常奇怪的行为

时间:2013-09-22 20:01:28

标签: android android-listview android-adapter android-scroll

我之前从未见过类似的东西,但是从最近几天开始,我遇到Listview的非常特殊的行为,直到现在我无法隔离这个问题。

我只粘贴我认为必要的代码,稍后我会告诉你我的问题。

/* tell adapter that data is done and stop the more loading progress bar*/ 
public void setDataChanged(boolean type)
{
    isLoadingData = false;
    notifyDataSetChanged();
}

/* If loading is going on size of the adapter will increase to show the additional progress bar*/ 
@Override
public int getCount() {
    int size = friendsModels.size();
    if (isLoadingData) {
        size += 1;
    }
    Log.i("size", String.valueOf(size));//to check size

    return size;
}
/* set loading true and page number of the data items*/ 
public void setLoadingData(boolean isLoadingData, int page) {
    this.isLoadingData = isLoadingData;
    this.page = page;
    notifyDataSetChanged();
}

/* MAX_ITEM number of item returning from rest webservice per page*/ 
@Override
public int getItemViewType(int position) {
    if(isLoadingData && position%MAX_ITEM==0 && position>0)
    {
        if(position/MAX_ITEM==page+1)
        return 1;
    }
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Log.e("getView", "getView");

    EventHolder eventHolder;
    int type = getItemViewType(position);
    LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView==null){
        eventHolder = new EventHolder();
        if(type==0)
        {
            convertView = li.inflate(R.layout.friends_list_items, parent,false);

            eventHolder.name   = (TextView)convertView.findViewById(R.id.textview_friend_name);

            convertView.setTag(eventHolder);

        }
        else if(type==1)
        {
            convertView = li.inflate(R.layout.progress_dialog, parent,false);
            eventHolder.progress = (RelativeLayout)convertView.findViewById(R.id.progress_layout);
            convertView.setTag(eventHolder);
        }
    }
    else
    {
        eventHolder = (EventHolder) convertView.getTag();
        if(type==0)
        {
            setFriends(eventHolder, position);
        }
    }

    return convertView;
}

现在onScroll方法 -

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    if(firstVisibleItem + visibleItemCount >= totalItemCount && !loading && totalItemCount%6==0 && totalItemCount>0 && NetworkUtil.isNetworkAvailable(activity))
    {
        loading = true;
        friendsAdapter.setLoadingData(true,pageNo);
        ControllerRequests.getPeople(FragmentClass.this, ++pageNo, search.getText().toString());
    }
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {       
}

虽然当有5个或更多项目时一切正常但是当项目大小减小时让我们说1或2然后有时getView方法不会被调用虽然日志信息显示我{{1 }} = 1或2但getCount只是不被调用。没有模式,我的意思是有时5次getView被称为它工作正常然后突然没有和那样。

1 个答案:

答案 0 :(得分:1)

这是一个奇怪的检查:

if(firstVisibleItem + visibleItemCount >= totalItemCount && !loading &&
                totalItemCount%6==0 && totalItemCount>0 && 
                                  NetworkUtil.isNetworkAvailable(activity))

我指的是:

totalItemCount % 6 == 0

因此,除非项目总数始终为6的倍数,否则此检查将阻止调用friendsAdapter.setLoadingData(true,pageNo);。这就是为什么notifyDataSetChanged();内不会执行位于setLoadingData(boolean, int)内的语句totalItemCount % 6 != 0

修改

我也想不出这样的情况:

firstVisibleItem + visibleItemCount > totalItemCount

您可以使用以下内容检查用户是否已到达列表末尾:

firstVisibleItem + visibleItemCount == totalItemCount