Skip to content

Commit

Permalink
* [android] fix sticky not display when scrolltoelement
Browse files Browse the repository at this point in the history
  • Loading branch information
sospartan committed Nov 30, 2016
1 parent 7b410a0 commit 22d163c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
Expand All @@ -220,6 +221,7 @@
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKInstance;
Expand Down Expand Up @@ -616,15 +618,27 @@ public void onBeforeScroll(int dx, int dy) {
return;
}


RecyclerView.LayoutManager layoutManager;
boolean beforeFirstVisibleItem = false;
if((layoutManager = getHostView().getInnerView().getLayoutManager()) instanceof LinearLayoutManager){
int fVisible = ((LinearLayoutManager)layoutManager).findFirstCompletelyVisibleItemPosition();
int pos = mChildren.indexOf(stickyComponent);

if( pos <= fVisible){
beforeFirstVisibleItem = true;
}
}

int[] location = new int[2];
stickyComponent.getHostView().getLocationOnScreen(location);
int[] parentLocation = new int[2];
stickyComponent.getParentScroller().getView().getLocationOnScreen(parentLocation);

int top = location[1] - parentLocation[1];

boolean showSticky = ((WXCell) stickyComponent).lastLocationY >= 0 && top <= 0 ;
boolean removeSticky = ((WXCell) stickyComponent).lastLocationY <= 0 && top > 0 ;
boolean showSticky = beforeFirstVisibleItem && ((WXCell) stickyComponent).lastLocationY >= 0 && top <= 0 && dy >= 0;
boolean removeSticky = ((WXCell) stickyComponent).lastLocationY <= 0 && top > 0 && dy <= 0;
if (showSticky) {
bounceRecyclerView.notifyStickyShow((WXCell) stickyComponent);
} else if (removeSticky) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.taobao.weex.common.WXThread;
import com.taobao.weex.ui.component.WXComponent;
Expand Down Expand Up @@ -308,27 +309,49 @@ public void notifyStickyRemove(WXCell component) {
private void showSticky() {
WXCell headComponent = headComponentStack.pop();
headComponentStack.push(headComponent);
View headerView = headComponent.getRealView();
final View headerView = headComponent.getRealView();
if (headerView == null)
return;
headerViewStack.push(headerView);
headComponent.removeSticky();
((ViewGroup) getParent()).addView(headerView);

final ViewGroup parent = (ViewGroup) getParent();
if(parent != null){
parent.post(WXThread.secure(new Runnable() {
@Override
public void run() {
ViewGroup existedParent;
if((existedParent = (ViewGroup)headerView.getParent())!= null){
existedParent.removeView(headerView);
}
parent.addView(headerView);
}
}));
}
}

/**
* remove top stickyView
* @param component
*/
private void removeSticky(WXComponent component) {
WXCell headComponent = headComponentStack.pop();
final WXCell headComponent = headComponentStack.pop();
if (!component.getRef().equals(headComponent.getRef())) {
headComponentStack.push(headComponent);
return;
}
View headerView = headerViewStack.pop();
((ViewGroup) getParent()).removeView(headerView);
headComponent.recoverySticky();
final View headerView = headerViewStack.pop();
final ViewGroup parent = (ViewGroup) getParent();
if(parent != null){
parent.post(WXThread.secure(new Runnable() {
@Override
public void run() {
parent.removeView(headerView);
headComponent.recoverySticky();
}
}));
}

}

/**
Expand Down

0 comments on commit 22d163c

Please sign in to comment.