Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
Add listener for sticky status.
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAfc committed Feb 25, 2019
1 parent 8b94f2c commit c53f13b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
GROUP=com.alibaba.android
ARTIFACT=vlayout
VERSION=1
VERSION_NAME=1.2.17
VERSION_NAME=1.2.20
PACKAGING_TYPE=aar
useNewSupportLibrary=true
systemProp.compileSdkVersion=25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@

package com.alibaba.android.vlayout;

import com.alibaba.android.vlayout.extend.PerformanceMonitor;
import com.alibaba.android.vlayout.extend.ViewLifeCycleHelper;
import com.alibaba.android.vlayout.extend.ViewLifeCycleListener;
import com.alibaba.android.vlayout.layout.BaseLayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.alibaba.android.vlayout.layout.FixAreaAdjuster;
import com.alibaba.android.vlayout.layout.FixAreaLayoutHelper;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
Expand All @@ -47,14 +39,21 @@
import android.view.ViewGroup;
import android.view.ViewParent;

import com.alibaba.android.vlayout.extend.PerformanceMonitor;
import com.alibaba.android.vlayout.extend.ViewLifeCycleHelper;
import com.alibaba.android.vlayout.extend.ViewLifeCycleListener;
import com.alibaba.android.vlayout.layout.BaseLayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.alibaba.android.vlayout.layout.FixAreaAdjuster;
import com.alibaba.android.vlayout.layout.FixAreaLayoutHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;


Expand All @@ -77,7 +76,7 @@ public class VirtualLayoutManager extends ExposeLinearLayoutManagerEx implements
private static final String TRACE_LAYOUT = "VLM onLayoutChildren";
private static final String TRACE_SCROLL = "VLM scroll";

private static boolean sDebuggable = false;
public static boolean sDebuggable = false;

public static void enableDebugging(boolean isDebug) {
sDebuggable = isDebug;
Expand Down Expand Up @@ -744,7 +743,9 @@ protected void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State st

// no item consumed
if (layoutState.mCurrentPosition == position) {
Log.w(TAG, "layoutHelper[" + layoutHelper.getClass().getSimpleName() + "@" + layoutHelper.toString() + "] consumes no item!");
if (sDebuggable) {
Log.w(TAG, "layoutHelper[" + layoutHelper.getClass().getSimpleName() + "@" + layoutHelper.toString() + "] consumes no item!");
}
// break as no item consumed
result.mFinished = true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@

package com.alibaba.android.vlayout.layout;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.LayoutManagerHelper;
import com.alibaba.android.vlayout.OrientationHelperEx;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.VirtualLayoutManager.LayoutStateWrapper;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import static android.support.v7.widget.LinearLayoutManager.VERTICAL;


Expand All @@ -45,18 +45,27 @@
* @since 1.0.0
*/
public class StickyLayoutHelper extends FixAreaLayoutHelper {
public interface StickyListener {
void onSticky(int pos, View view);

void onUnSticky(int pos, View view);
}

private static final String TAG = "StickyStartLayoutHelper";

private int mPos = -1;

private boolean mStickyStart = true;
private int mOffset = 0;

private int mOffset = 0;

private View mFixView = null;

private boolean mDoNormalHandle = false;

private boolean isLastStatusSticking = true;

private StickyListener stickyListener;

public StickyLayoutHelper() {
this(true);
Expand Down Expand Up @@ -173,7 +182,9 @@ public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state
top = orientationHelper.getStartAfterPadding() + mMarginTop + mOffset + mAdjuster.top;
bottom = top + result.mConsumed;
} else {
Log.i("Sticky", "remainingSpace: " + remainingSpace + " offset: " + mOffset);
if (VirtualLayoutManager.sDebuggable) {
Log.i("Sticky", "remainingSpace: " + remainingSpace + " offset: " + mOffset);
}
}
}

Expand Down Expand Up @@ -223,8 +234,6 @@ public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state
} else {
// result.mConsumed += mOffset;
}


}


Expand Down Expand Up @@ -292,15 +301,28 @@ public void afterLayout(RecyclerView.Recycler recycler, RecyclerView.State state
fixLayoutStateInCase2(orientationHelper, recycler, startPosition, endPosition, helper);
}

if (mFixView != null){
if (mFixView != null) {
mFixView.setBackgroundColor(mBgColor);
}

if (stickyListener != null) {
if (isLastStatusSticking && !isStickyNow()) {
stickyListener.onUnSticky(mPos, mFixView);
isLastStatusSticking = false;
} else if (!isLastStatusSticking && isStickyNow()) {
stickyListener.onSticky(mPos, mFixView);
isLastStatusSticking = true;
}
}
}

private void fixLayoutStateFromAbnormal2Normal(OrientationHelperEx orientationHelper, RecyclerView.Recycler recycler, int startPosition, int endPosition,
LayoutManagerHelper helper) {
LayoutManagerHelper helper) {
//fix status, from abnormal to normal
Log.i(TAG, "abnormal pos: " + mPos + " start: " + startPosition + " end: " + endPosition);
if (VirtualLayoutManager.sDebuggable) {
Log.i(TAG, "abnormal pos: " + mPos + " start: " + startPosition + " end: " + endPosition);
}

if (mFixView != null) {
int top, bottom;
View refer = null;
Expand Down Expand Up @@ -348,7 +370,7 @@ private void fixLayoutStateFromAbnormal2Normal(OrientationHelperEx orientationHe
}

private void fixLayoutStateInCase1(OrientationHelperEx orientationHelper, RecyclerView.Recycler recycler, int startPosition, int endPosition,
LayoutManagerHelper helper) {
LayoutManagerHelper helper) {
// considering the case when last layoutHelper has margin bottom
// 1. normal flow to abnormal flow; 2. abnormal flow to normal flow
if ((mStickyStart && endPosition >= mPos) || (!mStickyStart && startPosition <= mPos)) {
Expand Down Expand Up @@ -491,7 +513,7 @@ private void fixLayoutStateInCase1(OrientationHelperEx orientationHelper, Recycl
}

private void fixLayoutStateInCase2(OrientationHelperEx orientationHelper, RecyclerView.Recycler recycler, int startPosition, int endPosition,
LayoutManagerHelper helper) {
LayoutManagerHelper helper) {
// 1. normal flow to abnormal flow; 2. abnormal flow to normal flow
// (mDoNormalHandle && mFixView != null) || (!mDoNormalHandle && mFixView == null)
View eView = mFixView;
Expand Down Expand Up @@ -728,5 +750,9 @@ private void doMeasure(View view, LayoutManagerHelper helper) {
}

}

public void setStickyListener(StickyListener stickyListener) {
this.stickyListener = stickyListener;
}
}

0 comments on commit c53f13b

Please sign in to comment.