Endless Gridview

爱⌒轻易说出口 提交于 2019-11-29 02:33:13

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

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.

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

>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!