Reputation: 315
I am adding List Items to list view like
If I have 50 List Items, I am loading first 10 Items and then remaining 10 Items and so on...
You can find some portion of code here for reverence...
itemsPerPage=10;
list = (ListView) findViewById(R.id.list1);
footerView = ((LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.footer, null, false);
list.addFooterView(footerView);
adapter.addAll(getItemsBetween(0, itemsPerPage));// Loading first 10 Items
and Here is my Scrolling code...
list.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (totalItemCount <= totalCount) {
// what is the bottom iten that is visible
int lastInScreen = firstVisibleItem + visibleItemCount;
if ((lastInScreen == totalItemCount)){
int currentItemCount= mostViewdAdapter.getCount();
mostViewdAdapter.addAll(getItemsBetween(currentItemCount, currentItemCount+itemsPerPage));
mostViewdAdapter.notifyDataSetChanged();
}
}else{
System.out.println("End of the list view reached");
footerView.setVisibility(View.GONE);
}
}
});
Here I am sharing my footer XML:
<ProgressBar
android:id="@+id/footerBar"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center" />
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="5dp"
android:text="Loading..." />
Upvotes: 0
Views: 968
Reputation: 1378
Have a look at following code
1) Add custom_listview_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="7dip"
android:paddingTop="7dip" >
<Button
android:id="@+id/btn_Invite"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="0"
android:text="Load More"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
2) In your activity call
View footerView = ((LayoutInflater) _con
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.custom_listview_footer, null, false);
3) Call Button of the footer view
Button btnInvite = (Button) footerView.findViewById(R.id.btn_Invite);
4) Lastly Add footer to the listview
lvPhnContacts.addFooterView(footerView);
Upvotes: 1
Reputation: 6846
try with below code.
itemsPerPage=10;
list = (ListView) findViewById(R.id.list1);
footerView = ((LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.footer, null, false);
list.addFooterView(footerView);
adapter.addAll(getItemsBetween(0, itemsPerPage));// Loading first 10 Items
// after set footer
list.setAdapter(your adapter);//set adapter
list.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
final int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount) {
//load more data
// footerView visible
}
}
});
Upvotes: 0
Reputation: 1073
Can you post more code about when you call Listview.setAdapter()
?
You should call addFooterView()
before you set adapter to your listview.
Upvotes: 0