use of com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView in project incubator-weex by apache.
the class WXRecyclerTemplateList method initComponentHostView.
@Override
protected BounceRecyclerView initComponentHostView(@NonNull Context context) {
final BounceRecyclerView bounceRecyclerView = new BounceRecyclerView(context, mLayoutType, mColumnCount, mColumnGap, getOrientation());
WXAttr attrs = getDomObject().getAttrs();
String transforms = (String) attrs.get(Constants.Name.TRANSFORM);
if (transforms != null) {
bounceRecyclerView.getInnerView().addItemDecoration(RecyclerTransform.parseTransforms(getOrientation(), transforms));
}
mItemAnimator = bounceRecyclerView.getInnerView().getItemAnimator();
if (attrs.get(NAME_TEMPLATE_CACHE_SIZE) != null) {
templateCacheSize = WXUtils.getInteger(attrs.get(NAME_TEMPLATE_CACHE_SIZE), templateCacheSize);
}
boolean hasFixedSize = false;
int itemViewCacheSize = 2;
if (attrs.get(NAME_ITEM_VIEW_CACHE_SIZE) != null) {
itemViewCacheSize = WXUtils.getNumberInt(getDomObject().getAttrs().get(NAME_ITEM_VIEW_CACHE_SIZE), itemViewCacheSize);
}
if (attrs.get(NAME_HAS_FIXED_SIZE) != null) {
hasFixedSize = WXUtils.getBoolean(attrs.get(NAME_HAS_FIXED_SIZE), hasFixedSize);
}
RecyclerViewBaseAdapter recyclerViewBaseAdapter = new RecyclerViewBaseAdapter<>(this);
recyclerViewBaseAdapter.setHasStableIds(true);
bounceRecyclerView.getInnerView().setItemAnimator(null);
if (itemViewCacheSize != 2) {
bounceRecyclerView.getInnerView().setItemViewCacheSize(itemViewCacheSize);
}
if (bounceRecyclerView.getSwipeLayout() != null) {
if (WXUtils.getBoolean(getDomObject().getAttrs().get("nestedScrollingEnabled"), false)) {
bounceRecyclerView.getSwipeLayout().setNestedScrollingEnabled(true);
}
}
bounceRecyclerView.getInnerView().setHasFixedSize(hasFixedSize);
bounceRecyclerView.setRecyclerViewBaseAdapter(recyclerViewBaseAdapter);
bounceRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
bounceRecyclerView.getInnerView().clearOnScrollListeners();
bounceRecyclerView.getInnerView().addOnScrollListener(mViewOnScrollListener);
bounceRecyclerView.getInnerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
List<OnWXScrollListener> listeners = getInstance().getWXScrollListeners();
if (listeners != null && listeners.size() > 0) {
for (OnWXScrollListener listener : listeners) {
if (listener != null) {
View topView = recyclerView.getChildAt(0);
if (topView != null) {
int y = topView.getTop();
listener.onScrollStateChanged(recyclerView, 0, y, newState);
}
}
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
List<OnWXScrollListener> listeners = getInstance().getWXScrollListeners();
if (listeners != null && listeners.size() > 0) {
try {
for (OnWXScrollListener listener : listeners) {
if (listener != null) {
if (listener instanceof ICheckBindingScroller) {
if (((ICheckBindingScroller) listener).isNeedScroller(getRef(), null)) {
listener.onScrolled(recyclerView, dx, dy);
}
} else {
listener.onScrolled(recyclerView, dx, dy);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
bounceRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
BounceRecyclerView view;
if ((view = getHostView()) == null)
return;
mViewOnScrollListener.onScrolled(view.getInnerView(), 0, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
listUpdateRunnable = new Runnable() {
@Override
public void run() {
/**
* compute sticky position
*/
if (mStickyHelper != null) {
if (mStickyHelper.getStickyTypes().size() > 0) {
mStickyHelper.getStickyPositions().clear();
if (cellDataManager.listData != null) {
for (int i = 0; i < cellDataManager.listData.size(); i++) {
WXCell cell = getSourceTemplate(i);
if (cell == null) {
continue;
}
if (cell.isSticky()) {
mStickyHelper.getStickyPositions().add(i);
}
}
}
}
}
if (getHostView() != null && getHostView().getRecyclerViewBaseAdapter() != null) {
getHostView().getRecyclerViewBaseAdapter().notifyDataSetChanged();
}
if (WXEnvironment.isOpenDebugLog() && ENABLE_TRACE_LOG) {
WXLogUtils.d(TAG, "WXTemplateList notifyDataSetChanged");
}
}
};
return bounceRecyclerView;
}
use of com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView in project WeexErosFramework by bmfe.
the class BMLoadingRefresh method hide.
private void hide() {
BounceRecyclerView recyclerView = (BounceRecyclerView) mWeakReference.get().getHostView();
recyclerView.finishPullRefresh();
recyclerView.onRefreshingComplete();
mCurrentState = STATE_IDLE;
animation.stop();
iv_loading.setImageResource(R.drawable.sdk_bmloading);
}
use of com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView in project WeexErosFramework by bmfe.
the class LoadingLoadMore method hide.
private void hide() {
BounceRecyclerView recyclerView = (BounceRecyclerView) mWeakReference.get().getHostView();
recyclerView.finishPullLoad();
recyclerView.onLoadmoreComplete();
mCurrentState = STATE_IDLE;
animation.stop();
iv_loading.setImageResource(R.drawable.sdk_bmloading);
}
use of com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView in project incubator-weex by apache.
the class BasicListComponent method initComponentHostView.
@Override
protected T initComponentHostView(@NonNull Context context) {
T bounceRecyclerView = generateListView(context, getOrientation());
String transforms = (String) getDomObject().getAttrs().get(TRANSFORM);
if (transforms != null) {
bounceRecyclerView.getInnerView().addItemDecoration(RecyclerTransform.parseTransforms(getOrientation(), transforms));
}
if (getDomObject().getAttrs().get(Constants.Name.KEEP_POSITION_LAYOUT_DELAY) != null) {
keepPositionLayoutDelay = WXUtils.getNumberInt(getDomObject().getAttrs().get(Constants.Name.KEEP_POSITION_LAYOUT_DELAY), (int) keepPositionLayoutDelay);
}
if (getDomObject().getAttrs().get("appearActionDelay") != null) {
mAppearDelay = WXUtils.getNumberInt(getDomObject().getAttrs().get("appearActionDelay"), (int) mAppearDelay);
}
mItemAnimator = bounceRecyclerView.getInnerView().getItemAnimator();
RecyclerViewBaseAdapter recyclerViewBaseAdapter = new RecyclerViewBaseAdapter<>(this);
recyclerViewBaseAdapter.setHasStableIds(true);
bounceRecyclerView.setRecyclerViewBaseAdapter(recyclerViewBaseAdapter);
bounceRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
bounceRecyclerView.getInnerView().clearOnScrollListeners();
bounceRecyclerView.getInnerView().addOnScrollListener(mViewOnScrollListener);
if (getDomObject().getAttrs().get(Constants.Name.HAS_FIXED_SIZE) != null) {
boolean hasFixedSize = WXUtils.getBoolean(getDomObject().getAttrs().get(Constants.Name.HAS_FIXED_SIZE), false);
bounceRecyclerView.getInnerView().setHasFixedSize(hasFixedSize);
}
bounceRecyclerView.getInnerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
List<OnWXScrollListener> listeners = getInstance().getWXScrollListeners();
if (listeners != null && listeners.size() > 0) {
for (OnWXScrollListener listener : listeners) {
if (listener != null) {
View topView = recyclerView.getChildAt(0);
if (topView != null) {
int y = topView.getTop();
listener.onScrollStateChanged(recyclerView, 0, y, newState);
}
}
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
List<OnWXScrollListener> listeners = getInstance().getWXScrollListeners();
if (listeners != null && listeners.size() > 0) {
try {
for (OnWXScrollListener listener : listeners) {
if (listener != null) {
if (listener instanceof ICheckBindingScroller) {
if (((ICheckBindingScroller) listener).isNeedScroller(getRef(), null)) {
listener.onScrolled(recyclerView, dx, dy);
}
} else {
listener.onScrolled(recyclerView, dx, dy);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
bounceRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
T view;
if ((view = getHostView()) == null)
return;
mViewOnScrollListener.onScrolled(view.getInnerView(), 0, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
return bounceRecyclerView;
}
use of com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView in project incubator-weex by apache.
the class TemplateStickyHelper method onBeforeScroll.
/**
* dispatch scroll event, sticky header
*/
public void onBeforeScroll(int dx, int dy) {
if (stickyPositions == null || stickyPositions.size() == 0) {
return;
}
BounceRecyclerView bounceRecyclerView = recyclerTemplateList.getHostView();
RecyclerView recyclerView = recyclerTemplateList.getHostView().getInnerView();
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
int firstVisiblePosition = -1;
int lastVisiblePosition = -1;
if (layoutManager instanceof LinearLayoutManager) {
firstVisiblePosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
lastVisiblePosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
// max 3 column
int[] firstVisibleItemPositions = new int[3];
firstVisiblePosition = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(firstVisibleItemPositions)[0];
lastVisiblePosition = ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(firstVisibleItemPositions)[0];
}
if (firstVisiblePosition < 0) {
return;
}
TemplateViewHolder firstVisibleItemHolder = (TemplateViewHolder) recyclerView.findViewHolderForAdapterPosition(firstVisiblePosition);
if (firstVisibleItemHolder == null) {
return;
}
// find match sticky position
int matchStickyPosition = -1;
for (Integer headerPosition : stickyPositions) {
if (headerPosition == null) {
continue;
}
if (headerPosition <= firstVisiblePosition) {
matchStickyPosition = Math.max(matchStickyPosition, headerPosition);
} else {
break;
}
}
if (matchStickyPosition < 0) {
// remove holder for match position not found
View stickyFakeView = bounceRecyclerView.getChildAt(bounceRecyclerView.getChildCount() - 1);
if (stickyFakeView.getTag() instanceof TemplateViewHolder) {
TemplateViewHolder stickyFakeViewHolder = (TemplateViewHolder) stickyFakeView.getTag();
bounceRecyclerView.removeView(stickyFakeViewHolder.itemView);
stickyFakeViewHolder.itemView.setTranslationY(0);
if (stickyFakeViewHolder.getComponent() != null && stickyFakeViewHolder.getComponent().getDomObject() != null && stickyFakeViewHolder.getComponent().getDomObject().getEvents().contains(Constants.Event.UNSTICKY)) {
stickyFakeViewHolder.getComponent().fireEvent(Constants.Event.UNSTICKY);
}
}
/**
*check has sticky cell not visible
*/
for (int i = 0; i < recyclerView.getChildCount(); i++) {
View itemView = recyclerView.getChildAt(i);
TemplateViewHolder itemHolder = (TemplateViewHolder) recyclerView.getChildViewHolder(itemView);
if (itemHolder == null) {
continue;
}
int adapterPosition = itemHolder.getAdapterPosition();
if (!stickyPositions.contains(adapterPosition)) {
continue;
}
if (itemView.getVisibility() != View.VISIBLE) {
itemView.setVisibility(View.VISIBLE);
}
}
return;
}
// onCreate holder for match position if not exist
View stickyFakeView = bounceRecyclerView.getChildAt(bounceRecyclerView.getChildCount() - 1);
if (!(stickyFakeView.getTag() instanceof TemplateViewHolder) || ((TemplateViewHolder) stickyFakeView.getTag()).getHolderPosition() != matchStickyPosition) {
// remove previous sticky header
if (stickyFakeView.getTag() instanceof TemplateViewHolder && ((TemplateViewHolder) stickyFakeView.getTag()).getHolderPosition() != matchStickyPosition) {
TemplateViewHolder stickyFakeViewHolder = (TemplateViewHolder) stickyFakeView.getTag();
bounceRecyclerView.removeView(stickyFakeViewHolder.itemView);
stickyFakeViewHolder.itemView.setTranslationY(0);
if (stickyFakeViewHolder.getComponent() != null && stickyFakeViewHolder.getComponent().getDomObject() != null && stickyFakeViewHolder.getComponent().getDomObject().getEvents().contains(Constants.Event.UNSTICKY)) {
stickyFakeViewHolder.getComponent().fireEvent(Constants.Event.UNSTICKY);
}
}
// onCreate new sticky
int stickyHolderType = recyclerTemplateList.getItemViewType(matchStickyPosition);
TemplateViewHolder fakeStickyHolder = stickyHolderCache.get(stickyHolderType);
if (fakeStickyHolder == null) {
fakeStickyHolder = recyclerTemplateList.onCreateViewHolder(recyclerView, stickyHolderType);
stickyHolderCache.put(stickyHolderType, fakeStickyHolder);
}
recyclerTemplateList.onBindViewHolder(fakeStickyHolder, matchStickyPosition);
fakeStickyHolder.itemView.setTranslationY(0);
fakeStickyHolder.itemView.setTag(fakeStickyHolder);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
fakeStickyHolder.getComponent().clearPreLayout();
if (fakeStickyHolder.itemView.getParent() != null) {
ViewGroup parent = (ViewGroup) fakeStickyHolder.itemView.getParent();
parent.removeView(fakeStickyHolder.itemView);
}
bounceRecyclerView.addView(fakeStickyHolder.itemView, params);
fakeStickyHolder.getComponent().setLayout(fakeStickyHolder.getComponent().getDomObject());
stickyFakeView = fakeStickyHolder.itemView;
if (fakeStickyHolder.getComponent() != null && fakeStickyHolder.getComponent().getDomObject() != null && fakeStickyHolder.getComponent().getDomObject().getEvents().contains(Constants.Event.STICKY)) {
fakeStickyHolder.getComponent().fireEvent(Constants.Event.STICKY);
}
}
TemplateViewHolder stickyFakeViewHolder = (TemplateViewHolder) stickyFakeView.getTag();
for (int i = 0; i < recyclerView.getChildCount(); i++) {
View itemView = recyclerView.getChildAt(i);
TemplateViewHolder itemHolder = (TemplateViewHolder) recyclerView.getChildViewHolder(itemView);
if (itemHolder == null) {
continue;
}
int adapterPosition = itemHolder.getAdapterPosition();
if (!stickyPositions.contains(adapterPosition)) {
continue;
}
if (adapterPosition == stickyFakeViewHolder.getHolderPosition()) {
if (itemView.getVisibility() != View.INVISIBLE) {
itemView.setVisibility(View.INVISIBLE);
}
} else {
if (itemView.getVisibility() != View.VISIBLE) {
itemView.setVisibility(View.VISIBLE);
}
}
}
if (firstVisibleItemHolder.getComponent().isSticky()) {
if (firstVisibleItemHolder.itemView.getY() < 0) {
if (firstVisibleItemHolder.itemView.getVisibility() != View.INVISIBLE) {
firstVisibleItemHolder.itemView.setVisibility(View.INVISIBLE);
}
if (stickyFakeView.getVisibility() != View.VISIBLE) {
stickyFakeView.setVisibility(View.VISIBLE);
}
stickyFakeView.bringToFront();
} else {
if (firstVisibleItemHolder.itemView.getVisibility() != View.VISIBLE) {
firstVisibleItemHolder.itemView.setVisibility(View.VISIBLE);
}
if (stickyFakeView.getVisibility() != View.GONE) {
stickyFakeView.setVisibility(View.GONE);
}
}
} else {
if (stickyFakeView.getVisibility() != View.VISIBLE) {
stickyFakeView.setVisibility(View.VISIBLE);
}
}
// handle sticky is related, find next sticky position on screen
int nextVisiblePostion = firstVisiblePosition + 1;
if (lastVisiblePosition > 0) {
for (int i = nextVisiblePostion; i <= lastVisiblePosition; i++) {
if (stickyPositions.contains(i)) {
nextVisiblePostion = i;
break;
}
}
}
if (!stickyPositions.contains(nextVisiblePostion)) {
if (stickyFakeViewHolder.itemView.getTranslationY() < 0) {
stickyFakeViewHolder.itemView.setTranslationY(0);
}
return;
}
TemplateViewHolder nextStickyHolder = (TemplateViewHolder) recyclerView.findViewHolderForAdapterPosition(nextVisiblePostion);
if (nextStickyHolder == null || nextStickyHolder.getComponent() == null) {
return;
}
int translationY = (int) (nextStickyHolder.itemView.getY() - stickyFakeViewHolder.itemView.getMeasuredHeight());
if (translationY <= 0) {
stickyFakeViewHolder.itemView.setTranslationY(translationY);
} else {
stickyFakeViewHolder.itemView.setTranslationY(0);
}
}
Aggregations