本文介绍了为什么ListView的onScroll()被调用多发时间,每次与相同的参数AMOUNTS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调试几次。它的驾驶我疯了。
我listView.onScroll()函数被调用多次,并与所有的参数0每一次!
这会导致列表视图一遍滚动时在显示相同的列表。

我甚至手动设置的参数,但仍然没有改变。

到底为什么onScroll被调用多次?

,因为有些时候,结果显示在屏幕上它不象它被夹在一个循环!

我称之为REST风格的web服务至极具有分页。
这里是我的ListView片段我的一块code的。如有它需要的任何其他部分给我留下了评论,我会补充。

  @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_search_result_list,集装箱,FALSE);
        名单=(ListView控件)rootView.findViewById(R.id.listViewSearchResult);
        list.setOnItemClickListener((OnItemClickListener)getActivity());
        list.setOnScrollListener(新OnScrollListener(){               @覆盖
               公共无效onScrollStateChanged(AbsListView观点,诠释scrollState){}               @覆盖
               公共无效onScroll(AbsListView观点,诠释firstVisibleItem,诠释visibleItemCount,诠释totalItemCount){
                   分页=分页新(firstVisibleItem + visibleItemCount,visibleItemCount);
                   INT lastInScreen = firstVisibleItem + visibleItemCount;
                   如果((lastInScreen == totalItemCount)及&放大器;!(loadingMore)){
                       // grabURL(URL);
                       新JSONParse(getActivity())执行(URL);
                   }
                   如果(firstVisibleItem == 0)
                       totalItemCount = 5;               }
              });
        返回rootView;
    }
    // ***********内部类
    公共类JSONParse扩展的AsyncTask<字符串,字符串,JSONObject的> {
        私人ProgressDialog pDialog;
        私人SearchResultArrayListAdaptor适配器;
        上下文mContext;
        INT checkBoxRooms;
        公共JSONParse(上下文的背景下){
            mContext =背景;
        }
        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
        }
        @覆盖
        受保护的JSONObject doInBackground(字符串参数... args){
            // vvvv中
            loadingMore = TRUE;
            JSONObject的JSON = NULL;
            PropertyFilter searchFilter = SearchFilterManager.initializePropertyFilter(新PropertyFilter(),getArguments());
            JSONParser jParser =新JSONParser();
            如果(pagination.getPageSize()== 0)
                pagination.setPageSize(5);
            JSON = jParser.getJSONFromUrl(URL,searchFilter,分页);
            返回JSON;
        }
        @覆盖
        保护无效onPostExecute(JSON的JSONObject){            loadingMore = FALSE;
            PropertyObject propertyObject;
            ArrayList的< PropertyObject> ArrayList的=新的ArrayList< PropertyObject>();
            尝试{
                jsonArray = json.getJSONArray(PropertyListings);
                如果(jsonArray == NULL || jsonArray.length()。1)
                    返回;
                的for(int i = 0; I< jsonArray.length();我++){
                    JSONObject的C = jsonArray.getJSONObject(I)
                    。propertyObject =新GSON()fromJson(c.toString(),新PropertyObject()的getClass());
                    arrayList.add(propertyObject);
                }
                适配器=新SearchResultArrayListAdaptor(getActivity(),R.layout.list_view_items,ArrayList的);
                list.setAdapter(适配器);            }
            赶上(JSONException E){
              e.printStackTrace();
            }


解决方案

从开发者的网站关于 AbsListView.OnScrollListener

The callback will be called in every frame. So it is not a good idea to use this method.

Alternately, you can implement a custom adapter in your listview and inside its getView() method check if the position variable point's to the last item on the list. If yes make the request.

这篇关于为什么ListView的onScroll()被调用多发时间,每次与相同的参数AMOUNTS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 01:23