intrepidkarthi
intrepidkarthi

Reputation: 3102

OnScrollListener inside GridView in Android

Here is fragment which has a gridview of images. Initially I am loading the grid with 20 items. When the user scroll down the grid, I want to notify the data set has been changed and should reload the view. I have added OnScrollListener. But it doesn't seem to be working. It is not calling onScroll method, when I am scrolling through the list. And also I want to find out when the scroll, reaches the end item. What is the mistake here?

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

}

Upvotes: 0

Views: 4280

Answers (1)

tim
tim

Reputation: 1738

You need to call setOnScrollListener on your GridView

Something like

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

        }
   }

Upvotes: 2

Related Questions