【问题标题】:How to not load all ListView items again?如何不再加载所有 ListView 项目?
【发布时间】:2023-03-14 18:27:02
【问题描述】:

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

你有什么想法吗,我该如何使用 query.setSkip();方法并保存现有方法?

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

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

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

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());  
                        }
                });

               }

            }
         }

提前感谢您的任何建议!

【问题讨论】:

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


    【解决方案1】:

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

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

    ParseQueryAdapter 默认启用分页;这一切都由您负责(即使是“加载更多数据”的视图。

    这是来自文档的小代码 sn-p 可能对您有所帮助:

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

    【讨论】:

    • 哦,非常感谢!我不知道那个适配器!我可以覆盖它并在那里添加我的附加适配器数据吗?我应该使用MyAdapter extends ParseQueryAdapter 不应该吗?
    • 是的,您应该根据您的查询自定义create()。我的只是一个例子。
    • 再次感谢,帮了大忙。我会去做的
    • 没问题。很高兴能提供帮助。
    【解决方案2】:

    对于那些将来会在这个问题上苦苦挣扎的人,您可以简单地将条目数分配给一个整数(在我下面的例子中它是麻木的),然后在运行异步后台函数之前在列表视图中加载更多结果是确保列表视图中出现的条目数少于查询条目。如果没有,您可以再次运行 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
    
                }
    
            });
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      相关资源
      最近更新 更多