5

I have looked at lots of implementations of endless lists. One of them is https://github.com/commonsguy/cwac-endless

All of them are using listviews. And all of them are simply adding another row to the list. But when we consider Gridview (like in Google Play Store) and add the loading view as another item in the grid, it looks ugly. How can I achive the same thing in the Play Store?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
tasomaniac
  • 10,234
  • 6
  • 52
  • 84

3 Answers3

7

i have write a scroll listener and set this scroll listener to my grid view . please correct me if found error.

EG:

final GridView g = (GridView) findViewById(R.id.myGrid);
        g.setAdapter(new ImageAdapter(this));
        EndlessScrollListener scrollListener=new EndlessScrollListener(g,new RefreshList() {

            @Override
            public void onRefresh(int pageNumber) {
                System.out.println("On Refresh invoked..");

            }
        });
        g.setOnScrollListener(scrollListener);

/////////////////////////////////////////////////////////////////////////

import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.GridView;

public class EndlessScrollListener implements OnScrollListener {

    private GridView gridView;
    private boolean isLoading;
    private boolean hasMorePages;
    private int pageNumber=0;
    private RefreshList refreshList;
    private boolean isRefreshing;

    public EndlessScrollListener(GridView gridView,RefreshList refreshList) {
        this.gridView = gridView;
        this.isLoading = false;
        this.hasMorePages = true;
        this.refreshList=refreshList;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (gridView.getLastVisiblePosition() + 1 == totalItemCount && !isLoading) {
            isLoading = true;
            if (hasMorePages&&!isRefreshing) {
                isRefreshing=true;
                refreshList.onRefresh(pageNumber);
            }
        } else {
            isLoading = false;
        }

    }

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

    }

    public void noMorePages() {
        this.hasMorePages = false;
    }

    public void notifyMorePages(){
        isRefreshing=false;
        pageNumber=pageNumber+1;
    }

    public interface RefreshList {
        public void onRefresh(int pageNumber);
    }
}
Bytecode
  • 6,551
  • 16
  • 54
  • 101
  • Ok but where is your "pending view"? Suppose you have a 2 column GridView, are you be able to add a pending view at the buttom as a whole row? (just like merging these 2 columns?); – tasomaniac Jun 14 '12 at 14:44
  • Can you provide your xml files also? – tasomaniac Jun 14 '12 at 14:45
  • use a relative layout like this and handle the visiblity in onscroll listener – Bytecode Jun 14 '12 at 17:40
  • OK suppose you are using relative layout and while loading you set pending view visible. Then if user scroll the gridview to the top, the pending view won't scroll and stuck at the bottom of the grid. Am I right? I want it to be scrolled with the gridview just like in Google Play Store.. – tasomaniac Jun 16 '12 at 19:28
  • @tasomaniac: u r right , i don't how to handle that situation. – Bytecode Jun 17 '12 at 07:16
  • No one knows it. Only the guys who wrote Google Play Store app know how to do it. – tasomaniac Jun 23 '12 at 06:18
1

Well, to literally use the same technique as is used in EndlessAdapter, you might add N items to the grid, where N is your number of columns.

Another approach is to detect when the user has scrolled near the bottom, then kick off the background work to load in more stuff. I don't have a link handy, but what I've seen uses an OnScrollListener to detect your scroll position as it changes.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • The number of columns is 2 in my case. But I want to merge these 2 layers so that they look like a complete one row. Is it possible. I can detect and auto load with OnScrollListener easily but the problem is that I cannot display a pending view of a complete row under a GridView. – tasomaniac Jun 07 '12 at 22:51
  • @tasomaniac: "Is it possible" -- they cannot be physically merged. Whether you can get them to *look* merged by sitting side-by-side, I cannot say, as I have never tried that. – CommonsWare Jun 07 '12 at 22:56
  • I only want it to look like play store. It does it. When I use your library. Pending view becomes an element of a grid. Even the heights are different in play store. – tasomaniac Jun 09 '12 at 08:17
  • @tasomaniac: Perhaps the Play Store is not using `GridView`. It certainly is not using `EndlessAdapter`. – CommonsWare Jun 09 '12 at 10:52
  • Of course it is not using endlessadapter but I think it is using gridview in the application pages. – tasomaniac Jun 10 '12 at 14:52
  • Google Play Store definitely uses GridView and they add a loading view to the bottom as a footer. I am still using endless adapter. it is great but I could not find any solution to my problem yet. – tasomaniac Jun 03 '13 at 22:59
  • any luck ? please share – AZ_ Feb 28 '14 at 02:35
0

use a relative layout like this and handle the visiblity in onscroll listener

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <GridView
        android:id="@+id/myGrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="60dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:padding="10dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />

    <View
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_alignBottom="@+id/myGrid"
        android:background="@android:color/white" />

</RelativeLayout

>

Bytecode
  • 6,551
  • 16
  • 54
  • 101
  • Sorry for late comment but this is not I wanted. When you do this and if the user scrolls up again when the data is loading they see the pending view at the bottom. I dont want it. I would like the pending view to act like a row of the scrolling area. I think the only way to do that is to make it a ListView and display 2 columns in every row. And at the bottom when the list is loading just show a one row of a pending view. – tasomaniac Jan 13 '13 at 00:08