【问题标题】:Is there a way to prevent the gridview from scrolling to its top position when its adapter's data is changed?当适配器的数据发生变化时,有没有办法防止gridview滚动到它的顶部位置?
【发布时间】:2016-03-29 08:59:00
【问题描述】:

在加载更多数据后,网格视图返回顶部,我想让它在加载后从最后一个项目继续滚动。 我尝试使用onScrollStateChanged,并使其加载到state == SCROLL_STATE_TOUCH_SCROLL,但我遇到了同样的问题。

 @Override
    protected void onPostExecute(ArrayList<productDetails> AProducts) {
        final ArrayList<productDetails> products = AProducts;
        super.onPostExecute(products);

        productAdapter = new productAdapter(category.this, productDetailsArr);
        gridView.setAdapter(productAdapter);
        productAdapter.notifyDataSetChanged();


        if (products != null) {
            gridView.setOnScrollListener(new AbsListView.OnScrollListener() {

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    if (firstVisibleItem + visibleItemCount >= totalItemCount) {
                        // End has been reached
                        limit = Integer.parseInt(((productDetails) (products.get(products.size() - 1))).getProductID());
                        // limit = Integer.parseInt(products.get(3).get(products.get(3).size() - 1));


                        if (flimit != limit) {
                            flimit = limit; //to stop using last element in fatching last products more than one time.
                            if (UtilityClass.isInternetAvailable(getApplicationContext())) {

                                                                    new getProductsTask().execute(category);


                            } else {
                                Intent internet = new Intent(getBaseContext(), NointernetConnection.class);
                                startActivity(internet);
                                finish();

                            }
                        } else {
                            Log.d("FLimit", ">>" + "END");
                        }

                    } else {
                        progressDialog.dismiss();
                    }

                }

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

                }

            });

        }
    }

【问题讨论】:

    标签: android gridview scroll android-asynctask scrollview


    【解决方案1】:

    首先,您不需要每次在 onPostExecute 方法中创建适配器类(productAdapter)的新对象,并在每次数据更改或网络调用的新响应时将适配器设置为 gridview。这会导致 gridview 滚动到顶部位置。相反,您可以在适配器类中创建一个 setter 方法。

    ArrayList <productDetails> productDetailsArr;
    
     public void setProductDetailsArr(ArrayList<productDetails> productDetailsArr) {
                this.productDetailsArr = productDetailsArr;
            }
    

    并在 onPostExecute 方法中写下以下代码以检查 adaper 是否为 null。如果为 null,则只需创建一个新实例。否则,您只需提供新数据集并调用 notifyDataSetChanged()。

     if(productAdapter == null)
     {
        productAdapter = new productAdapter(category.this, productDetailsArr);
        gridView.setAdapter(productAdapter);
    
     }else{
    
         productAdapter.setProductDetailsArr(productDetailsArr);
         productAdapter.notifyDataSetChanged();
      }
    

    【讨论】:

    • 谢谢!节省了我很多时间。
    猜你喜欢
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 2011-08-13
    • 2018-09-08
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多