为什么ListView的onScroll()被调用多发时间,每次与相同的参数AMOUNTS? [英] Why ListView onScroll() gets called mutiple times and everytime WITH SAME ARGUMENT AMOUNTS?

查看:745
本文介绍了为什么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();
            }


解决方案

从<一个href=\"http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html#onScrollStateChanged(android.widget.AbsListView,%20int)\"相对=nofollow>开发者的网站关于 AbsListView.OnScrollListener


  而在列表视图或网格视图被滚动到被调用

回调方法。如果视图被滚动,呈现在滚动的下一帧之前该方法将被调用。特别是,它会为getView(INT,查看,ViewGroup中)进行任何调用之前调用。


回调将在每帧中调用。因此,这不是用这个方法是一个好主意。

另外,你可以在你的ListView和内实现自定义适配器,它的 getView()方式检查,如果在位置变量点对名单上的最后一个项目。如果是发出请求。

I debugged several times. It's driving me crazy. My listView.onScroll() function gets called several times and every time with arguments all 0! Which causes the listView to show THE SAME list over and over when scrolled.

I even manually set the arguments but still no change.

Why on earth would onScroll get called multiple times?

It's not like it gets caught in a loop because some times the result appears on the screen!

I call a RESTfull webService wich has pagination. Here is a piece of my code in my listView Fragment. Please if any other part of it is needed leave me a comment and I will add.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_search_result_list, container, false);
        list=(ListView)rootView.findViewById(R.id.listViewSearchResult);
        list.setOnItemClickListener((OnItemClickListener) getActivity());


        list.setOnScrollListener(new OnScrollListener(){

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

               @Override
               public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                   pagination = new Pagination(firstVisibleItem+visibleItemCount,visibleItemCount);
                   int lastInScreen = firstVisibleItem + visibleItemCount;    
                   if((lastInScreen == totalItemCount) && !(loadingMore)){     
                       //grabURL(url);
                       new JSONParse(getActivity()).execute(url);
                   }
                   if (firstVisibleItem==0) 
                       totalItemCount = 5;

               }
              });
        return rootView;
    }
    //*********************************** inner class
    public class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        private SearchResultArrayListAdaptor adapter;
        Context mContext;
        int checkBoxRooms;
        public JSONParse(Context context){
            mContext = context;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected JSONObject doInBackground(String... args) {
            //vvvv
            loadingMore = true;
            JSONObject json = null;
            PropertyFilter searchFilter = SearchFilterManager.initializePropertyFilter(new PropertyFilter(), getArguments());
            JSONParser jParser = new JSONParser();
            if (pagination.getPageSize()==0)
                pagination.setPageSize(5);
            json = jParser.getJSONFromUrl(url, searchFilter, pagination);
            return json;
        }
        @Override
        protected void onPostExecute(JSONObject json) {

            loadingMore = false;
            PropertyObject propertyObject;
            ArrayList<PropertyObject> arrayList = new ArrayList<PropertyObject>();
            try {
                jsonArray = json.getJSONArray("PropertyListings");
                if (jsonArray == null || jsonArray.length()<1) 
                    return;
                for(int i = 0; i < jsonArray.length(); i++){
                    JSONObject c = jsonArray.getJSONObject(i);
                    propertyObject = new Gson().fromJson(c.toString(), new PropertyObject().getClass());
                    arrayList.add(propertyObject);
                }
                adapter = new SearchResultArrayListAdaptor(getActivity(), R.layout.list_view_items, arrayList);
                list.setAdapter(adapter);

            } 
            catch (JSONException e) {
              e.printStackTrace();
            }

解决方案

From the developer's site about AbsListView.OnScrollListener:

Callback method to be invoked while the list view or grid view is being scrolled. If the view is being scrolled, this method will be called before the next frame of the scroll is rendered. In particular, it will be called before any calls to getView(int, View, ViewGroup).

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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