com.fortysevendeg.android.swipelistview.RefreshSwipeListView.java Source code

Java tutorial

Introduction

Here is the source code for com.fortysevendeg.android.swipelistview.RefreshSwipeListView.java

Source

/*
 * Copyright (C) 2013 47 Degrees, LLC
 * http://47deg.com
 * hello@47deg.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fortysevendeg.android.swipelistview;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewConfigurationCompat;
import android.util.AttributeSet;
import android.view.ViewConfiguration;

/**
 * ListView subclass that provides the swipe functionality
 */
public class RefreshSwipeListView extends SwipeListView {

    /**
     * If you create a View pragmatically you need send back and front identifier
     * @param context Context
     * @param swipeBackView Back Identifier
     * @param swipeFrontView Front Identifier
     */
    public RefreshSwipeListView(Context context, int swipeBackView, int swipeFrontView) {
        super(context, swipeBackView, swipeFrontView);
    }

    /**
     * @see android.widget.ListView#ListView(android.content.Context, android.util.AttributeSet)
     */
    public RefreshSwipeListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * @see android.widget.ListView#ListView(android.content.Context, android.util.AttributeSet, int)
     */
    public RefreshSwipeListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * Init ListView
     *
     * @param attrs AttributeSet
     */
    @Override
    protected void init(AttributeSet attrs) {

        int swipeMode = SWIPE_MODE_BOTH;
        boolean swipeOpenOnLongPress = true;
        boolean swipeCloseAllItemsWhenMoveList = true;
        long swipeAnimationTime = 0;
        float swipeOffsetLeft = 0;
        float swipeOffsetRight = 0;

        int swipeActionLeft = SWIPE_ACTION_REVEAL;
        int swipeActionRight = SWIPE_ACTION_REVEAL;

        if (attrs != null) {
            TypedArray styled = getContext().obtainStyledAttributes(attrs, R.styleable.SwipeListView);
            swipeMode = styled.getInt(R.styleable.SwipeListView_swipeMode, SWIPE_MODE_BOTH);
            swipeActionLeft = styled.getInt(R.styleable.SwipeListView_swipeActionLeft, SWIPE_ACTION_REVEAL);
            swipeActionRight = styled.getInt(R.styleable.SwipeListView_swipeActionRight, SWIPE_ACTION_REVEAL);
            swipeOffsetLeft = styled.getDimension(R.styleable.SwipeListView_swipeOffsetLeft, 0);
            swipeOffsetRight = styled.getDimension(R.styleable.SwipeListView_swipeOffsetRight, 0);
            swipeOpenOnLongPress = styled.getBoolean(R.styleable.SwipeListView_swipeOpenOnLongPress, true);
            swipeAnimationTime = styled.getInteger(R.styleable.SwipeListView_swipeAnimationTime, 0);
            swipeCloseAllItemsWhenMoveList = styled
                    .getBoolean(R.styleable.SwipeListView_swipeCloseAllItemsWhenMoveList, true);
            mSwipeFrontView = styled.getResourceId(R.styleable.SwipeListView_swipeFrontView, 0);
            mSwipeBackView = styled.getResourceId(R.styleable.SwipeListView_swipeBackView, 0);
        }

        if (mSwipeFrontView == 0 || mSwipeBackView == 0) {
            mSwipeFrontView = getContext().getResources().getIdentifier(SWIPE_DEFAULT_FRONT_VIEW, "id",
                    getContext().getPackageName());
            mSwipeBackView = getContext().getResources().getIdentifier(SWIPE_DEFAULT_BACK_VIEW, "id",
                    getContext().getPackageName());

            if (mSwipeFrontView == 0 || mSwipeBackView == 0) {
                throw new RuntimeException(String.format(
                        "You forgot the attributes swipeFrontView or swipeBackView. You can add this attributes or use '%s' and '%s' identifiers",
                        SWIPE_DEFAULT_FRONT_VIEW, SWIPE_DEFAULT_BACK_VIEW));
            }
        }

        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
        mTouchListener = new SwipeRefreshListViewTouchListener(this, mSwipeFrontView, mSwipeBackView);
        if (swipeAnimationTime > 0) {
            mTouchListener.setAnimationTime(swipeAnimationTime);
        }
        mTouchListener.setRightOffset(swipeOffsetRight);
        mTouchListener.setLeftOffset(swipeOffsetLeft);
        mTouchListener.setSwipeActionLeft(swipeActionLeft);
        mTouchListener.setSwipeActionRight(swipeActionRight);
        mTouchListener.setSwipeMode(swipeMode);
        mTouchListener.setSwipeClosesAllItemsWhenListMoves(swipeCloseAllItemsWhenMoveList);
        mTouchListener.setSwipeOpenOnLongPress(swipeOpenOnLongPress);
        setOnTouchListener(mTouchListener);
        setOnScrollListener(mTouchListener.makeScrollListener());
    }

    public void setRefreshSwipeListener(RefreshSwipeListener refreshSwipeListener) {
        ((SwipeRefreshListViewTouchListener) mTouchListener).setRefreshSwipeListener(refreshSwipeListener);
    }
}