如何不再次加载所有ListView项目?

时间:2014-01-25 16:01:20

标签: java android android-listview parse-platform onscrolllistener

我正在使用Parse.com框架,我最近在我的代码中添加了一个load-more(onScroll)ListView。但我发现每当加载更多进程开始时,即使之前的项目也会再次加载。当我添加query.setSkip(someCustomSkip);时,在加载更多进程之后,它只删除没有(someCustomSkip)数量的项目。

你有什么想法,我怎么能用query.setSkip();方法,也保留了这些方法?

1)在滚动到第5个位置query.setLimit(mListView.getCount + 5)

之前,ListView的第一个位置

ListView's first position, before scrolling to 5th position (<code>query.setLimit(mListView.getCount + 5</code>)

2)滚动到极限位置后的ListView。它把我扔到这个位置并设置skip - 隐藏前5项

ListView after scrolling to limit-position. It throws me to this position and sets skip - hides first 5 items

也是我的代码的一部分,这对这个问题非常重要:

public class Fragment1 extends Fragment {
    static ListView mListView;
    static AnimalAdapter mAdapter;
    static ProgressBar mProgressBar;
    static EditText mEditText;
    static LayoutInflater inflater;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.animalsfrag, container, false);
        mListView = (ListView)rootView.findViewById(R.id.animal_list);

        View header = getActivity().getLayoutInflater().inflate(R.layout.header, null);

        header.setPadding(2, 8, 4, 2);
        mListView.setVisibility(View.INVISIBLE);
        mListView.requestFocus();
        mListView.addHeaderView(header); 

        View footie = getActivity().getLayoutInflater().inflate(R.layout.footer, null);
        mListView.addFooterView(footie);
        footie.setVisibility(View.INVISIBLE);


        mProgressBar = (ProgressBar) rootView.findViewById (R.id.loading_animals);
        mProgressBar.setVisibility(View.VISIBLE);

        RemoteDataTask task = new RemoteDataTask();
        task.execute();



        return rootView;
    }



    public void updateData() { //method, which updates my data
         mListView = (ListView)getView().findViewById(R.id.animal_list);     
   final ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);


    query.setCachePolicy(CachePolicy.NETWORK_ONLY);
    query.orderByAscending("animal");


    query.setLimit(mListView.getCount() + 5);
    if (mListView.getCount() > 5) {
        query.setSkip(5);
    }

    query.findInBackground(new FindCallback<Animal>() {

        @Override
          public void done(List<Animal> animals, ParseException error) {

              if(animals != null){
                  mAdapter.clear();

                mProgressBar = (ProgressBar) getView().findViewById (R.id.loading_animals);
               mProgressBar.setVisibility(View.INVISIBLE);
             RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);  
            footie.setVisibility(View.VISIBLE);

            for (int i = 0; i < animals.size(); i++) {


                      mAdapter.add(animals.get(i));







                  }  
              }  
            }
         }); 
    } 




     private class RemoteDataTask extends AsyncTask<Void, Void, Void> {


         @Override
            protected void onPreExecute() {
                super.onPreExecute();  }

         @Override
            protected Void doInBackground(Void... params) {

                return null;



         }


         @Override
            protected void onPostExecute(Void result) {


               mListView = (ListView) getView().findViewById(R.id.animal_list);

               mEditText = (EditText) getView().findViewById(R.id.search_animal);

               mAdapter = new AnimalAdapter(getActivity(), new ArrayList<Animal>());

               mListView.setVisibility(View.VISIBLE);
               mListView.setTextFilterEnabled(true);
               mListView.setAdapter(mAdapter);




               mListView.setOnScrollListener(new OnScrollListener() { 
      //my OnScrollListener

                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                            int totalItemCount) {
                        final int lastItem = firstVisibleItem + visibleItemCount;
                        if(lastItem == totalItemCount) {




                        if (mListView.getCount() > 20) {

                            RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);

                            mListView.removeFooterView(footie);

                              }





                            else{

                                updateData();


                            }

                        }

                    }


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

                        if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
                            View currentFocus = getActivity().getCurrentFocus();
                            if(currentFocus != null) {
                                currentFocus.clearFocus();
                            }
                        }



                    }

                });
               mListView.setAdapter(mAdapter);

                mEditText.addTextChangedListener(new TextWatcher(){

                    @Override
                    public void afterTextChanged(Editable s) {}

                    @Override
                    public void beforeTextChanged(CharSequence s,
                            int start, int count, int after) {}

                    @Override
                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {

                        System.out.println("Text ["+s+"]");
                        mAdapter.getFilter().filter(s.toString());  
                        }
                });

               }

            }
         }

提前感谢任何建议!

2 个答案:

答案 0 :(得分:1)

我不知道你现在是否正在使用它,但是Parse提供了一个名为ParseQueryAdapter的类来处理所有这些问题。其中一个构造函数需要ContextQueryFactoryQueryFactory要求您覆盖create()并让您返回ParseQuery

ParseQuery有一个描述其缓存策略的枚举。如果您不想每次都ping服务器,可以先将缓存策略设置为先检查缓存,然后在本地chache中找不到ParseObject时点击服务器。

ParseQueryAdapter默认启用分页功能;这一切都得到了照顾(即使是“加载更多数据”的观点。

以下是文档中可能对您有所帮助的一小段代码:

ParseQueryAdapter<ParseObject> adapter =
  new ParseQueryAdapter<ParseObject>(this, new ParseQueryAdapter.QueryFactory<ParseObject>() {
    public ParseQuery<ParseObject> create() {
      // Here we can configure a ParseQuery to our heart's desire.
      ParseQuery query = new ParseQuery("Band");
      query.whereContainedIn("genre", Arrays.asList({ "Punk", "Metal" }));
      query.whereGreaterThanOrEqualTo("memberCount", 4);
      query.orderByDescending("albumsSoldCount");
      query.setCachePolicy(ParseQuery.CachePolicy.CACHE_ELSE_NETWORK)
      return query;
    }
  });

然后,您可以像往常一样在ListView中设置适配器。

答案 1 :(得分:0)

对于那些将来在这个问题上苦苦挣扎的人,你可以简单地将条目数分配给一个整数(在我的下面的情况下是麻木),然后在运行异步背景函数之前在列表视图中加载更多结果是为了确保列表视图中显示的条目数小于查询条目。如果没有,您可以再次运行Async任务以附加更多结果。

 L3.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view,
                    int scrollState) { // TODO Auto-generated method stub
                int threshold = 1;
                int count = L3.getCount();

                if (scrollState == SCROLL_STATE_IDLE) {
                    if (L3.getLastVisiblePosition() >= count
                            - threshold) {
                        // Execute LoadMoreDataTask AsyncTask

                    //  Toast.makeText(getApplicationContext(), count- threshold+"", 300).show();

                        if(L3.getLastVisiblePosition()+ threshold <numb)
                        {
                            //Toast.makeText(getApplicationContext(), L3.getLastVisiblePosition()+"", 300).show();
                             new LoadMoreDataTask().execute();
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "لا توجد مزيد من النتائج حاليآ...", 300).show();
                        }

                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub

            }

        });

希望它有所帮助!!