Android中的GridView内的OnScrollListener

时间:2012-12-13 06:27:37

标签: android android-fragments android-gridview

这是具有图像网格视图的片段。最初我正在加载20个项目的网格。当用户向下滚动网格时,我想通知数据集已更改并应重新加载视图。我添加了OnScrollListener。但它似乎没有奏效。当我滚动列表时,它不会调用onScroll方法。而且我想知道滚动时到达最终项目。这里有什么错误?

public class AllStoresFragment extends Fragment implements OnScrollListener{
       GridView gridview;
       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                Bundle savedInstanceState) {

          view = inflater.inflate(R.layout.allstores_fragment, container,
            false);     
          gridview = (GridView) view.findViewById(R.id.grid_view_storestab);
          gridview.setAdapter(new ImageAdapter(jsonresultinitial));
    }
   }   


private void notifyMe()
{
    ((BaseAdapter) ((GridView) view.findViewById(R.id.grid_view_storestab))
            .getAdapter()).notifyDataSetChanged();

    gridview.setAdapter(new ImageAdapter(
            jsonresult));

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    Log.v("first visible count",firstVisibleItem+"");
    Log.v("visible count",visibleItemCount+"");
    Log.v("total item count",totalItemCount+"");
    notifyMe();

}

1 个答案:

答案 0 :(得分:2)

您需要在GridView

上调用setOnScrollListener

这样的东西
    public class AllStoresFragment extends Fragment {
           GridView gridview;
           @Override
           public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                    Bundle savedInstanceState) {

              view = inflater.inflate(R.layout.allstores_fragment, container,
                false);     
              gridview = (GridView) view.findViewById(R.id.grid_view_storestab);
              gridview.setAdapter(new ImageAdapter(jsonresultinitial));
              gridview.setOnScrollListener(new GVOnScrollListener());
        }
       }   


    private void notifyMe()
    {
        ((BaseAdapter) ((GridView) view.findViewById(R.id.grid_view_storestab))
                .getAdapter()).notifyDataSetChanged();

        gridview.setAdapter(new ImageAdapter(
                jsonresult));

    }

   public final class GVOnScrollListener implements AbsListView.OnScrollListener {
       @Override 
       public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            Log.v("first visible count",firstVisibleItem+"");
            Log.v("visible count",visibleItemCount+"");
            Log.v("total item count",totalItemCount+"");
            notifyMe();

        }
   }