Thursday, 1 May 2014

Android List view with pull to Refresh and Swipe functionality.

Here, I have a list (PullToRefreshSwipeListView) that implementing both of PullToRefresh  and SwipeListView library.
List can do some basic functionality like pullUp/pullDown and swipe left/right.

If you decide to use PullToRefreshSwipeListView as a view, you can define it in your xml layout like this:

 <com.handmark.pulltorefresh.library.PullToRefreshSwipeListView
        xmlns:swipe="http://schemas.android.com/apk/res-auto"
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:divider="@drawable/line_separator_repeat"
        android:listSelector="#00000000"
        ptr:ptrMode="pullFromEnd"
        swipe:swipeActionLeft="reveal"
        swipe:swipeBackView="@+id/back" //raw back view
        swipe:swipeCloseAllItemsWhenMoveList="true"
        swipe:swipeFrontView="@+id/front" // raw front view
        swipe:swipeMode="both" />
  • swipeFrontView - Required - front view id.
  • swipeBackView - Required - back view id.
  • swipeActionLeft - Optional - left swipe action Default: 'reveal'
  • swipeActionRight - Optional - right swipe action Default: 'reveal'
  • swipeMode - Gestures to enable or 'none'. Default: 'both'
  • swipeCloseAllItemsWhenMoveList - Close revealed items on list motion. Default: 'true'
  • swipeOpenOnLongPress - Reveal on long press Default: 'true'
  • swipeAnimationTime - item drop animation time. Default: android configuration
  • swipeOffsetLeft - left offset
  • swipeOffsetRight - right offset
In java :
private PullToRefreshSwipeListView ptrList;
private SwipeListView swipeList;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_list);
ptrList = (PullToRefreshSwipeListView)findViewById(R.id.sphr_listview);
swipeList = ptrList.getRefreshableView();      
ptrList.setOnRefreshListener(new OnRefreshListener2<SwipeListView>() {
           @Override
           public void onPullDownToRefresh(PullToRefreshBase<SwipeListView> refreshView){
               // TODO Auto-generated method stub

           }

           @Override
           public void onPullUpToRefresh(PullToRefreshBase<SwipeListView> refreshView) {
               // TODO Auto-generated method stub

           }

       });
}

set adapter in list-view as shown below.
  private void setListData() {
   adapter = new DataAdapter(this, R.layout.item_library_list, new ArrayList<PurchasedItem>(), resultListView);
    
    swipeList.setSwipeListViewListener(new BaseSwipeListViewListener() {   
     @Override
        public void onClickFrontView(final int position) {
            //do something here on Frontview click
        } 
       @Override
        public void onClickBackView(final int position) {
            //do something here on Backview click
        } 

      @Override
        public void onOpened(int position, boolean toRight) {
            // TODO Auto-generated method stub
            super.onOpened(position-1, toRight);
            lastPos = position-1;
        }

        @Override
        public void onMove(int position, float x) {
            // TODO Auto-generated method stub
            super.onMove(position-1, x);
        }

        @Override
        public int onChangeSwipeMode(int position) {
            // TODO Auto-generated method stub
                        return SwipeListView.SWIPE_MODE_DEFAULT;
        }

        @Override
        public void onStartOpen(int position, int action, boolean right) {
            // TODO Auto-generated method stub
            super.onStartOpen(position-1, action, right);

        }

    });

    ptrList.setAdapter(adapter); 
    ptrList.setLongClickable(true); 
    swipeList.setSwipeOpenOnLongPress(false);  
    }

6 comments:

  1. hi am doing d same functionalities in my application, i have already used pull to refresh and swipe listview libraries from git hub.... now to do both the functions together, should i
    use anyother libraries????

    ReplyDelete
    Replies
    1. I have posted both libs.please check.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. I got an error in your library file...!!!

      Delete
  2. If i implemented your library in my listview than list items are not clickable. what can i do?

    ReplyDelete