There are many apps that allow you to refresh content by simply pulling down the list. I had a similar requirement in one of my projects. I decided to write a blog post that how easily I implemented this feature.
For simplicity I will remove code of setting Adapter to ListView etc.I will only show code to detect pull down on ListView
First lets see the layout of my screen
listData.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
//we will check if user has moved on ListView and we
//are not already refreshing
if (event.getAction()==MotionEvent.ACTION_MOVE
&& !refreshing){
if (listData.getFirstVisiblePosition()==0){
//get current Y on moving
currentRefreshY=event.getY();
//if this is not the first time user has moved
if(lastRefreshY!=-1){
float diff=currentRefreshY-lastRefreshY;
if (diff>20 && !refreshing){
refreshing=true;
//reset currentRefreshY
currentRefreshY=-1;
//before this we can add some
//TextView to layoutRefresh showing user that
//we are refreshing data
startRefreshing();
}
}
//save current Y of move
lastRefreshY=currentRefreshY;
}
}
return false;
});
Explanation of above code snippet
event.getY() will return Y co-ordinate of current movement
We are storing this in currentRefreshY and then subtract lastRefreshY from currentRefreshY to get the distance covered. If distance is more than 20 (our threshold) we can initiate refresh if it is not refreshing already.
listData.getFirstVisiblePosition() will return the index of first item visible in ListView. We have to be sure that we only refresh when user has pulled down our list while our first item is visible. So we compare this value for index 0
For simplicity I will remove code of setting Adapter to ListView etc.I will only show code to detect pull down on ListView
First lets see the layout of my screen
<?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="match_parent"
android:orientation="vertical" >
<!-- this linear layout will contain our content which we will show -->
<!-- to user while refreshing -->
<LinearLayout android:id="@+id/layoutRefresh"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></LinearLayout>
<!-- this is our listview that will contain data -->
<ListView android:id="@+id/listLocations"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Now our Java code
Some required global variables
private ListView listData; //this is our ListViewCode for checking for pulldown
//these two variables will be used to calculate if its time to refresh
private float lastRefreshY=-1,currentRefreshY=-1;
//this variable will be manipulated based on refreshing status
private boolean refreshing=false;
listData.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
//we will check if user has moved on ListView and we
//are not already refreshing
if (event.getAction()==MotionEvent.ACTION_MOVE
&& !refreshing){
if (listData.getFirstVisiblePosition()==0){
//get current Y on moving
currentRefreshY=event.getY();
//if this is not the first time user has moved
if(lastRefreshY!=-1){
float diff=currentRefreshY-lastRefreshY;
if (diff>20 && !refreshing){
refreshing=true;
//reset currentRefreshY
currentRefreshY=-1;
//before this we can add some
//TextView to layoutRefresh showing user that
//we are refreshing data
startRefreshing();
}
}
//save current Y of move
lastRefreshY=currentRefreshY;
}
}
return false;
});
Explanation of above code snippet
event.getY() will return Y co-ordinate of current movement
We are storing this in currentRefreshY and then subtract lastRefreshY from currentRefreshY to get the distance covered. If distance is more than 20 (our threshold) we can initiate refresh if it is not refreshing already.
listData.getFirstVisiblePosition() will return the index of first item visible in ListView. We have to be sure that we only refresh when user has pulled down our list while our first item is visible. So we compare this value for index 0
Comments
Post a Comment
Share your wisdom