How to add swipe listview inside loadmorelistview in android
<p>I am creating an android application in which what i am doing is loading more items in the list view once the list view reaches end of the list it add more items to the list this part works fine but want i need now is to add an extra option of swiping the list item to make edit or delete option for which i have seen an example of swiping the list view to edit or delete.</p>

<p>How could i use both these libraries in a single list view because both libraries have different codes in it.Even i have combined these two codes but it didn't works.</p>

<p>LoadMoreListView:</p>

<pre><code>public class LoadMoreListView extends ListView implements OnScrollListener {
private static final String TAG = "LoadMoreListView";

/**
 * Listener that will receive notifications every time the list scrolls.
 */
private OnScrollListener mOnScrollListener;
private LayoutInflater mInflater;

// footer view
private RelativeLayout mFooterView;
// private TextView mLabLoadMore;
private ProgressBar mProgressBarLoadMore;

// Listener to process load more items when user reaches the end of the list
private OnLoadMoreListener mOnLoadMoreListener;
// To know if the list is loading more items
private boolean mIsLoadingMore = false;
private int mCurrentScrollState;

public LoadMoreListView(Context context) {
    super(context);
    init(context);
}

public LoadMoreListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context) {

    mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // footer
    mFooterView = (RelativeLayout) mInflater.inflate(
            R.layout.load_more_footer, this, false);
    /*
     * mLabLoadMore = (TextView) mFooterView
     * .findViewById(R.id.load_more_lab_view);
     */
    mProgressBarLoadMore = (ProgressBar) mFooterView
            .findViewById(R.id.load_more_progressBar);

    addFooterView(mFooterView);

    super.setOnScrollListener(this);
}

@Override
public void setAdapter(ListAdapter adapter) {
    super.setAdapter(adapter);
}

/**
 * Set the listener that will receive notifications every time the list
 * scrolls.
 *
 * @param l The scroll listener.
 */
@Override
public void setOnScrollListener(AbsListView.OnScrollListener l) {
    mOnScrollListener = l;
}

/**
 * Register a callback to be invoked when this list reaches the end (last
 * item be visible)
 *
 * @param onLoadMoreListener The callback to run.
 */

public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
    mOnLoadMoreListener = onLoadMoreListener;
}

public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {

    if (mOnScrollListener != null) {
        mOnScrollListener.onScroll(view, firstVisibleItem,
                visibleItemCount, totalItemCount);
    }

    if (mOnLoadMoreListener != null) {

        if (visibleItemCount == totalItemCount) {
            mProgressBarLoadMore.setVisibility(View.GONE);
            // mLabLoadMore.setVisibility(View.GONE);
            return;
        }

        boolean loadMore = firstVisibleItem + visibleItemCount &gt;= totalItemCount;

        if (!mIsLoadingMore &amp;&amp; loadMore
                &amp;&amp; mCurrentScrollState != SCROLL_STATE_IDLE) {
            mProgressBarLoadMore.setVisibility(View.VISIBLE);
            // mLabLoadMore.setVisibility(View.VISIBLE);
            mIsLoadingMore = true;
            onLoadMore();
        }

    }

}

public void onScrollStateChanged(AbsListView view, int scrollState) {

    //bug fix: listview was not clickable after scroll
    if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
        view.invalidateViews();
    }

    mCurrentScrollState = scrollState;

    if (mOnScrollListener != null) {
        mOnScrollListener.onScrollStateChanged(view, scrollState);
    }

}

public void onLoadMore() {
    Log.d(TAG, "onLoadMore");
    if (mOnLoadMoreListener != null) {
        mOnLoadMoreListener.onLoadMore();
    }
}

/**
 * Notify the loading more operation has finished
 */
public void onLoadMoreComplete() {
    mIsLoadingMore = false;
    mProgressBarLoadMore.setVisibility(View.GONE);
}

/**
 * Interface definition for a callback to be invoked when list reaches the
 * last item (the user load more items in the list)
 */
public interface OnLoadMoreListener {
    /**
     * Called when the list reaches the last item (the last item is visible
     * to the user)
     */
    public void onLoadMore();
}
</code></pre>

<p>}</p>

<p>SwipeListView:</p>

<pre><code>public class SwipeMenuListView extends ListView{
private static final int TOUCH_STATE_NONE = 0;
private static final int TOUCH_STATE_X = 1;
private static final int TOUCH_STATE_Y = 2;

public static final int DIRECTION_LEFT = 1;
public static final int DIRECTION_RIGHT = -1;
private int mDirection = 1;//swipe from right to left by default

private int MAX_Y = 5;
private int MAX_X = 3;
private float mDownX;
private float mDownY;
private int mTouchState;
private int mTouchPosition;
private SwipeMenuLayout mTouchView;
private OnSwipeListener mOnSwipeListener;

private SwipeMenuCreator mMenuCreator;
private OnMenuItemClickListener mOnMenuItemClickListener;
private OnMenuStateChangeListener mOnMenuStateChangeListener;
private Interpolator mCloseInterpolator;
private Interpolator mOpenInterpolator;

public SwipeMenuListView(Context context) {
    super(context);
    init();
}

public SwipeMenuListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public SwipeMenuListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    MAX_X = dp2px(MAX_X);
    MAX_Y = dp2px(MAX_Y);
    mTouchState = TOUCH_STATE_NONE;
}

@Override
public void setAdapter(ListAdapter adapter) {
    super.setAdapter(new SwipeMenuAdapter(getContext(), adapter) {
        @Override
        public void createMenu(SwipeMenu menu) {
            if (mMenuCreator != null) {
                mMenuCreator.create(menu);
            }
        }

        @Override
        public void onItemClick(SwipeMenuView view, SwipeMenu menu,
                                int index) {
            boolean flag = false;
            if (mOnMenuItemClickListener != null) {
                flag = mOnMenuItemClickListener.onMenuItemClick(
                        view.getPosition(), menu, index);
            }
            if (mTouchView != null &amp;&amp; !flag) {
                mTouchView.smoothCloseMenu();
            }
        }
    });
}

public void setCloseInterpolator(Interpolator interpolator) {
    mCloseInterpolator = interpolator;
}

public void setOpenInterpolator(Interpolator interpolator) {
    mOpenInterpolator = interpolator;
}

public Interpolator getOpenInterpolator() {
    return mOpenInterpolator;
}

public Interpolator getCloseInterpolator() {
    return mCloseInterpolator;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    //在拦截处处理，在滑动设置了点击事件的地方也能swip，点击时又不能影响原来的点击事件
    int action = ev.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mDownX = ev.getX();
            mDownY = ev.getY();
            boolean handled = super.onInterceptTouchEvent(ev);
            mTouchState = TOUCH_STATE_NONE;
            mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
            View view = getChildAt(mTouchPosition - getFirstVisiblePosition());

            //只在空的时候赋值 以免每次触摸都赋值，会有多个open状态
            if (view instanceof SwipeMenuLayout) {
                //如果有打开了 就拦截.
                if (mTouchView != null &amp;&amp; mTouchView.isOpen() &amp;&amp; !inRangeOfView(mTouchView.getMenuView(), ev)) {
                    return true;
                }
                mTouchView = (SwipeMenuLayout) view;
                mTouchView.setSwipeDirection(mDirection);
            }
            //如果摸在另外个view
            if (mTouchView != null &amp;&amp; mTouchView.isOpen() &amp;&amp; view != mTouchView) {
                handled = true;
            }

            if (mTouchView != null) {
                mTouchView.onSwipe(ev);
            }
            return handled;
        case MotionEvent.ACTION_MOVE:
            float dy = Math.abs((ev.getY() - mDownY));
            float dx = Math.abs((ev.getX() - mDownX));
            if (Math.abs(dy) &gt; MAX_Y || Math.abs(dx) &gt; MAX_X) {
                //每次拦截的down都把触摸状态设置成了TOUCH_STATE_NONE 只有返回true才会走onTouchEvent 所以写在这里就够了
                if (mTouchState == TOUCH_STATE_NONE) {
                    if (Math.abs(dy) &gt; MAX_Y) {
                        mTouchState = TOUCH_STATE_Y;
                    } else if (dx &gt; MAX_X) {
                        mTouchState = TOUCH_STATE_X;
                        if (mOnSwipeListener != null) {
                            mOnSwipeListener.onSwipeStart(mTouchPosition);
                        }
                    }
                }
                return true;
            }
    }
    return super.onInterceptTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() != MotionEvent.ACTION_DOWN &amp;&amp; mTouchView == null)
        return super.onTouchEvent(ev);
    int action = ev.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            int oldPos = mTouchPosition;
            mDownX = ev.getX();
            mDownY = ev.getY();
            mTouchState = TOUCH_STATE_NONE;

            mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());

            if (mTouchPosition == oldPos &amp;&amp; mTouchView != null
                    &amp;&amp; mTouchView.isOpen()) {
                mTouchState = TOUCH_STATE_X;
                mTouchView.onSwipe(ev);
                return true;
            }

            View view = getChildAt(mTouchPosition - getFirstVisiblePosition());

            if (mTouchView != null &amp;&amp; mTouchView.isOpen()) {
                mTouchView.smoothCloseMenu();
                mTouchView = null;
                // return super.onTouchEvent(ev);
                // try to cancel the touch event
                MotionEvent cancelEvent = MotionEvent.obtain(ev);
                cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
                onTouchEvent(cancelEvent);
                if (mOnMenuStateChangeListener != null) {
                    mOnMenuStateChangeListener.onMenuClose(oldPos);
                }
                return true;
            }
            if (view instanceof SwipeMenuLayout) {
                mTouchView = (SwipeMenuLayout) view;
                mTouchView.setSwipeDirection(mDirection);
            }
            if (mTouchView != null) {
                mTouchView.onSwipe(ev);
            }
            break;
        case MotionEvent.ACTION_MOVE:
            //有些可能有header,要减去header再判断
            mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY()) - getHeaderViewsCount();
            //如果滑动了一下没完全展现，就收回去，这时候mTouchView已经赋值，再滑动另外一个不可以swip的view
            //会导致mTouchView swip 。 所以要用位置判断是否滑动的是一个view
            if (!mTouchView.getSwipEnable() || mTouchPosition != mTouchView.getPosition()) {
                break;
            }
            float dy = Math.abs((ev.getY() - mDownY));
            float dx = Math.abs((ev.getX() - mDownX));
            if (mTouchState == TOUCH_STATE_X) {
                if (mTouchView != null) {
                    mTouchView.onSwipe(ev);
                }
                getSelector().setState(new int[]{0});
                ev.setAction(MotionEvent.ACTION_CANCEL);
                super.onTouchEvent(ev);
                return true;
            } else if (mTouchState == TOUCH_STATE_NONE) {
                if (Math.abs(dy) &gt; MAX_Y) {
                    mTouchState = TOUCH_STATE_Y;
                } else if (dx &gt; MAX_X) {
                    mTouchState = TOUCH_STATE_X;
                    if (mOnSwipeListener != null) {
                        mOnSwipeListener.onSwipeStart(mTouchPosition);
                    }
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mTouchState == TOUCH_STATE_X) {
                if (mTouchView != null) {
                    boolean isBeforeOpen = mTouchView.isOpen();
                    mTouchView.onSwipe(ev);
                    boolean isAfterOpen = mTouchView.isOpen();
                    if (isBeforeOpen != isAfterOpen &amp;&amp; mOnMenuStateChangeListener != null) {
                        if (isAfterOpen) {
                            mOnMenuStateChangeListener.onMenuOpen(mTouchPosition);
                        } else {
                            mOnMenuStateChangeListener.onMenuClose(mTouchPosition);
                        }
                    }
                    if (!isAfterOpen) {
                        mTouchPosition = -1;
                        mTouchView = null;
                    }
                }
                if (mOnSwipeListener != null) {
                    mOnSwipeListener.onSwipeEnd(mTouchPosition);
                }
                ev.setAction(MotionEvent.ACTION_CANCEL);
                super.onTouchEvent(ev);
                return true;
            }
            break;
    }
    return super.onTouchEvent(ev);
}

public void smoothOpenMenu(int position) {
    if (position &gt;= getFirstVisiblePosition()
            &amp;&amp; position &lt;= getLastVisiblePosition()) {
        View view = getChildAt(position - getFirstVisiblePosition());
        if (view instanceof SwipeMenuLayout) {
            mTouchPosition = position;
            if (mTouchView != null &amp;&amp; mTouchView.isOpen()) {
                mTouchView.smoothCloseMenu();
            }
            mTouchView = (SwipeMenuLayout) view;
            mTouchView.setSwipeDirection(mDirection);
            mTouchView.smoothOpenMenu();
        }
    }
}

public void smoothCloseMenu(){
    if (mTouchView != null &amp;&amp; mTouchView.isOpen()) {
        mTouchView.smoothCloseMenu();
    }
}

private int dp2px(int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            getContext().getResources().getDisplayMetrics());
}

public void setMenuCreator(SwipeMenuCreator menuCreator) {
    this.mMenuCreator = menuCreator;
}

public void setOnMenuItemClickListener(
        OnMenuItemClickListener onMenuItemClickListener) {
    this.mOnMenuItemClickListener = onMenuItemClickListener;
}

public void setOnSwipeListener(OnSwipeListener onSwipeListener) {
    this.mOnSwipeListener = onSwipeListener;
}

public void setOnMenuStateChangeListener(OnMenuStateChangeListener onMenuStateChangeListener) {
    mOnMenuStateChangeListener = onMenuStateChangeListener;
}

public static interface OnMenuItemClickListener {
    boolean onMenuItemClick(int position, SwipeMenu menu, int index);
}

public static interface OnSwipeListener {
    void onSwipeStart(int position);

    void onSwipeEnd(int position);
}

public static interface OnMenuStateChangeListener {
    void onMenuOpen(int position);

    void onMenuClose(int position);
}

public void setSwipeDirection(int direction) {
    mDirection = direction;
}

/**
 * 判断点击事件是否在某个view内
 *
 * @param view
 * @param ev
 * @return
 */
public static boolean inRangeOfView(View view, MotionEvent ev) {
    int[] location = new int[2];
    view.getLocationOnScreen(location);
    int x = location[0];
    int y = location[1];
    if (ev.getRawX() &lt; x || ev.getRawX() &gt; (x + view.getWidth()) || ev.getRawY() &lt; y || ev.getRawY() &gt; (y + view.getHeight())) {
        return false;
    }
    return true;
}
</code></pre>

<p>}</p>

<p>Xml view:</p>

<pre><code> &lt;codingtown.coconut.libraries.loadmorelist.LoadMoreListView
    android:id="@+id/farmlistmain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/toolbar"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:paddingTop="5dp"
    android:layout_alignBottom="@+id/empty" /&gt;
</code></pre>

<p>How could i combine both these libraries in single xml file </p>