Reputation: 89
Actually i have a list view containing 30 items.i want to implement paging ie to have two buttons as next & previous so that when i click next it displays some 5 items and so on.
Please provide me with sample code
Upvotes: 0
Views: 3172
Reputation: 843
i did something like this. You can also try. Its working nice
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
//here i handled continous move
scrolltwice = true;
return true;
}
final float x = ev.getX();
final float y = ev.getY();
switch(action & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_MOVE:
final int xDiff = (int) Math.abs(x - mLastMotionX);
yDiff = (int) (y - mLastMotionY);
final int ydif = (int) Math.abs(y - mLastMotionY);
final int touchSlop = mTouchSlop;
boolean xMoved = xDiff > touchSlop;
boolean yMoved = ydif > touchSlop;
if (xMoved || yMoved) {
if (yMoved) {
mTouchState = TOUCH_STATE_SCROLLING;
}
}
break;
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
// Remember location of down touch
mLastMotionX = x;
mLastMotionY = y;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
final int myDiff = (int) (y - mLastMotionY);
if(mTouchState == TOUCH_STATE_SCROLLING){
scrolltwice = false;
if(!scrolltwice){
Log.d("DB", " ACTION_UP fetch new records ");
FetchRecords rec = new FetchRecords();
rec.execute();
if(yDiff < 0){ // fetching next slot of records
nextRecordId = nextRecordId + previousTotal;
if(nextRecordId > totalRowCount){
nextRecordId = nextRecordId - previousTotal;
}
}else if(yDiff > 0){ // fetching previous slot of records
nextRecordId = nextRecordId - previousTotal;
if(nextRecordId < 1){
nextRecordId = 0;
}
}
}
}
scrolltwice = false;
mTouchState = TOUCH_STATE_REST;
break;
}
return false;
}
//implement ontouch listener if the view is list pass it onTouchEvent
public boolean onTouch(View v, MotionEvent event) {
if(v.equals(objListView))
onTouchEvent(event);
return false;
}
Upvotes: 0
Reputation: 1883
You could also add a footer element (Load more...).
To do this you have to:
View footer = getLayoutInflater().inflate(R.layout.item_load_more, null);
myList.addFooterView(footer);
You could also load more items when listview is at the end, like: http://benjii.me/2010/08/endless-scrolling-listview-in-android/
Hope this helps...
Upvotes: 0
Reputation: 22076
One solution is to implement an OnScrollListener
and make changes (like adding items, etc.) to the ListAdapter
at a convenient state in its onScroll method.
Adding items when the user scrolls to the end of the list.Click here
Upvotes: 1