2016-03-29 5 views
0

より多くのデータをグリッドビューにロードした後、ロード後に最後のアイテムからスクロールし続けるようにします。 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) { 
        } 

       } 

      }); 

     } 
    } 

答えて

1

まず、あなたは毎回GridViewのためにあなたのアダプタクラス(productAdapter)onPostExecute方法で毎回の新しいオブジェクトとセットアダプタを作成する必要がない場合、データの変更やネットワークのコール.Thisの新しい応答あなたのアダプタクラスにsetterメソッドを作成することができます。

ArrayList <productDetails> productDetailsArr; 

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

とonPostExecute方法で確認するには、次のコードを書き留めadaperだけヌルまたはヌルnot.Ifある場合は、あなただけの新しいデータセットを提供する必要があり、ちょうどnotifyDataSetChangedを呼ぶ新しいinstance.Otherwiseを作成する必要があります()。

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

}else{ 

    productAdapter.setProductDetailsArr(productDetailsArr); 
    productAdapter.notifyDataSetChanged(); 
    } 
+0

ありがとう、それは私の作品です:D –

関連する問題