Skip to main content

Android pull down to refresh implementation

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
<?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 ListView
//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;
Code for checking for pulldown
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

Popular posts from this blog

Decoding JPEG image file using libavcodec

I got a chance to work on a video encoding application that decodes series of jpeg files and convert them into ogg theora video file. I used the infamous libavcodec library that is used in FFMPEG . I decided to write blog posts explaining how I decode jpeg images and convert them into ogg video file. This is the first part and in this I will explain how to decode jpeg images using libavcodec. To learn how to write decoded images as a ogg video file please read http://random-stuff-mine.blogspot.com/2017/07/encoding-raw-images-to-ogg-theora-video.html Before reading this blog post you must be aware of using and setting up libavcodec. I highly recommend this tutorial to get basics of using libavcodec http://www.ffmpeg.org/doxygen/0.6/api-example_8c-source.html Allocating input format context We will first allocate input format for reading the file. We will use avformat_open_input function that will allocate AVFormatContext structure passed to it , the function detects input typ

CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information

I created a new Blazor Server app in Visual Studio 2019 and tried to run it. But I was getting this error CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information. I couldn't find any reason or solution to this problem. I tried creating the project multiple times but same error. I created a new .Net Core Web App and added a new razor component and included that component in a razor page (cshtml file) like this @(await Html.RenderComponentAsync<GeofenceWork>(RenderMode.ServerPrerendered)) and <component type="typeof(GeofenceWork)" render-mode="serverprerendered" /> As soon as I navigate to this page that has component added I got the same error: CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information. This was very frustrating. After hours of trying and searching I figured out the solution. 

Encoding raw images to Ogg Theora video using libavcodec

In one of the blog posts we learned how to decode jpeg images using libavcodec. This is the second part of that post. In this we will learn how to encode decoded images (raw images) to theora and write them in ogg video file. In the end of the first part we saved our raw image in raw_data variable and its length in raw_data_size variable. Lets assume that we packaged all our decoding code in one function called "decode_jpeg_image" which has following signature int decode_jpeg_image(char *filename,int file_name_size,uint8_t *raw_data,int *raw_data_size) filename = name of jpeg file to decode file_name_size = length of jpeg file's name raw_data = contains decoded raw image on return raw_data_size = contains length of raw_data on return Now let's start working on how to encode this image in raw_data to theora and write that image to ogg video file. Finding Theora encoder We first have to find encoder for THEORA which is represented by AVCodec structure. He