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

标签 java android android-listview parse-platform onscrolllistener

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

你知道吗,我该如何使用 query.setSkip();方法并保存provoive的?

1) ListView的第一个位置,滚动到第5个位置之前 query.setLimit(mListView.getCount + 5)

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

2) 滚动到限制位置后的 ListView。它把我带到这个位置并设置跳过 - 隐藏前 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());  
                        }
                });

               }

            }
         }

提前感谢您的任何建议!

最佳答案

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

ParseQuery 有一个描述其缓存策略的枚举。如果你不想每次都ping服务器,你可以设置缓存策略先检查缓存,如果在本地缓存中没有找到ParseObject则再命中服务器。

ParseQueryAdapter 默认启用分页;一切都为您服务(即使是显示“加载更多数据”的 View )。

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

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 中设置适配器。

关于java - 如何不再加载所有 ListView 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21352807/

相关文章:

java - 在Java中复制目录

java - 如何将所有输入转义到 Velocity 模板

android - Appium 测试中的 NoSuchElementException。无法正确获取定位器

android - 在我的应用程序中获取不包含 webview 的用户代理

java - Android - 根据文件名获取文件的 R.raw.value?

java - SwipeToRefreshview 滑动错误

Android ListView setOnItemClickListener 不工作

android - 一个列表 Activity 中的两个自定义 ListView

android - 在代码下方运行时,GC 因 GC_CONCURRENT 而发疯

java - 我如何使用 Retrofit 2.3.0 解析 json 对象