dynamic listview adding “Load more items” at the end of scroll

为君一笑 提交于 2019-11-26 17:15:48

you try the following code

list.setOnScrollListener(new OnScrollListener() {

        public void onScrollStateChanged(AbsListView view, int scrollState) {


        }

        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
            {
                if(flag_loading == false)
                {
                    flag_loading = true;
                    additems();
                }
            }
        }
    });

in the additem() add the next 10 items to array list and set the flag_loading as false. and call notifydatasetchanged. it should work.

Richard Key

There is the solution, Activity has to implement interface AbsListView.OnScrollListener

In Activity:

int currentFirstVisibleItem = 0;
int currentVisibleItemCount = 0;
int totalItemCount = 0;
int currentScrollState = 0;
boolean loadingMore = false;
Long startIndex = 0L;
Long offset = 10L;
View footerView;



@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    this.currentFirstVisibleItem = firstVisibleItem;
    this.currentVisibleItemCount = visibleItemCount;
    this.totalItemCount = totalItemCount;
}

@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
    this.currentScrollState = scrollState;
    this.isScrollCompleted();
}

private void isScrollCompleted() {
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE && this.totalItemCount == (currentFirstVisibleItem + currentVisibleItemCount)) {
        /*** In this way I detect if there's been a scroll which has completed ***/
        /*** do the work for load more date! ***/
        if (!loadingMore) {
            loadingMore = true;
            new LoadMoreItemsTask(this).execute();
        }
    }
}



private class LoadMoreItemsTask extends AsyncTask<Void, Void, List<ListItem>> {

    private Activity activity;
    private View footer;

    private LoadMoreItemsTask(Activity activity) {
        this.activity = activity;
        loadingMore = true;
        footer = activity.getLayoutInflater().inflate(R.layout.base_list_item_loading_footer, null);
    }

    @Override
    protected void onPreExecute() {
        list.addFooterView(footer);
        list.setAdapter(adapter);
        super.onPreExecute();
    }

    @Override
    protected List<ListItem> doInBackground(Void... voids) {

        return getNextItems(startIndex, offset);
    }

    @Override
    protected void onPostExecute(List<ListItem> listItems) {
        if (footer != null) {
               list.removeFooterView(footer);
        }
        list.setAdapter(adapter);

        loadingMore = false;
        if (listItems.size() > 0) {
            startIndex = startIndex + listItems.size();
            setItems(listItems);
        }
        super.onPostExecute(listItems);
    }


}

Oncreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.base_list);
    list = (ListView) findViewById(R.id.list);
    list.setOnItemClickListener(this);
    list.setOnScrollListener(this);


        footerView = ((LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.base_list_item_loading_footer, null, false);



    initAdapter();


    new LoadMoreItemsTask(this).execute();

}

base_list_item_loading_footer.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_footer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="15dp" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal" >

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ProgressBar>

        <TextView
            style="@style/ItemHeadTextStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/loading_list_items" />
    </LinearLayout>
</RelativeLayout> 

I hope , it helps.

Best way to implement Dynamic Expandaple LW with footer explained below:

private int firstVisibleItem, visibleItemCount,totalItemCount;

expandapleListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScroll(AbsListView view,
                                 int firstVisibleItemm, int visibleItemCountt,
                                 int totalItemCountt) {
                firstVisibleItem = firstVisibleItemm;
                visibleItemCount = visibleItemCountt;
                totalItemCount = totalItemCountt;


            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                final int lastItem = firstVisibleItem + visibleItemCount;
                if (lastItem == totalItemCount && scrollState == SCROLL_STATE_IDLE) {
                    expandapleInt++;

                    new AsyncTask().execute();

                    //get next 10-20 items(your choice)items

                }
            }
        });

Footer implementation:

Given below xml code, should be in bottom of Layout that your ListView inside it...

 <RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center"
android:id="@+id/footer">

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/textView3"
    android:layout_toStartOf="@+id/textView3">
</ProgressBar>

<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:text="Loading"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/textView3" />

Bind it in your activity

footer = (RelativeLayout) findViewById(R.id.footer);
footer.setVisibility(View.GONE);

onPreExecute

footer.setVisibility(View.VISIBLE);

onPostExecute

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