Skip to content

Commit

Permalink
feat(android): restructure pull header and footer
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and siguangli committed Jun 7, 2022
1 parent 0593d20 commit b55013b
Show file tree
Hide file tree
Showing 26 changed files with 615 additions and 953 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,28 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tencent.mtt.hippy.common.HippyMap;

import java.util.Map;

@SuppressWarnings({"deprecation", "unused"})
public class PullFooterRenderNode extends ListItemRenderNode {

public PullFooterRenderNode(int id, @Nullable Map<String, Object> props, @NonNull String className,
@Nullable ViewGroup rootView, @NonNull ControllerManager componentManager,
boolean isLazyLoad) {
super(id, props, className, rootView, componentManager, isLazyLoad);
}

@Override
public int getItemViewType() {
return this.getClassName().hashCode();
}

@Override
public boolean isPullFooter() {
return true;
}
public PullFooterRenderNode(int id, @Nullable Map<String, Object> props,
@NonNull String className, @Nullable ViewGroup rootView,
@NonNull ControllerManager componentManager, boolean isLazyLoad) {
super(id, props, className, rootView, componentManager, isLazyLoad);
}

@Override
public boolean shouldSticky() {
return false;
}

@Override
public int getItemViewType() {
return this.getClassName().hashCode();
}

@Override
public boolean isPullFooter() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,34 @@
*/

package com.tencent.mtt.hippy.uimanager;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tencent.mtt.hippy.common.HippyMap;

import java.util.Map;

@SuppressWarnings({"deprecation", "unused"})
public class PullHeaderRenderNode extends ListItemRenderNode {

public PullHeaderRenderNode(int id, @Nullable Map<String, Object> props, @NonNull String className,
@Nullable ViewGroup rootView, @NonNull ControllerManager componentManager,
boolean isLazyLoad) {
super(id, props, className, rootView, componentManager, isLazyLoad);
}

@Override
public int getItemViewType() {
return this.getClassName().hashCode();
}

@Override
public boolean isPullHeader() {
return true;
}
public PullHeaderRenderNode(int id, @Nullable Map<String, Object> props,
@NonNull String className, @Nullable ViewGroup rootView,
@NonNull ControllerManager componentManager, boolean isLazyLoad) {
super(id, props, className, rootView, componentManager, isLazyLoad);
}

@Override
public boolean shouldSticky() {
return false;
}

@Override
public int getItemViewType() {
return this.getClassName().hashCode();
}

@Override
public boolean isPullHeader() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import android.widget.FrameLayout;
import com.tencent.mtt.hippy.uimanager.DiffUtils;
import com.tencent.mtt.hippy.uimanager.ListItemRenderNode;
import com.tencent.mtt.hippy.uimanager.PullFooterRenderNode;
import com.tencent.mtt.hippy.uimanager.PullHeaderRenderNode;
import com.tencent.mtt.hippy.uimanager.RenderNode;
import com.tencent.mtt.hippy.views.list.IRecycleItemTypeChange;
import com.tencent.mtt.hippy.views.refresh.HippyPullFooterView;
import com.tencent.mtt.hippy.views.refresh.HippyPullHeaderView;
import com.tencent.mtt.hippy.views.hippylist.recyclerview.helper.skikcy.IStickyItemsProvider;
import com.tencent.renderer.NativeRender;
Expand All @@ -49,8 +51,8 @@ public class HippyRecyclerListAdapter<HRCV extends HippyRecyclerView> extends Ad
protected final HRCV hippyRecyclerView;
protected final HippyItemTypeHelper hippyItemTypeHelper;
protected int positionToCreateHolder;
protected PullFooterEventHelper footerEventHelper;
protected PullHeaderEventHelper headerEventHelper;
protected PullFooterRefreshHelper footerRefreshHelper;
protected PullHeaderRefreshHelper headerRefreshHelper;
protected PreloadHelper preloadHelper;
@NonNull
private final NativeRender mNativeRenderer;
Expand All @@ -77,9 +79,13 @@ public HippyRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType
boolean needsDelete = renderNode.needDeleteExistRenderView();
View renderView = createRenderView(renderNode);
if (isPullHeader(positionToCreateHolder)) {
((HippyPullHeaderView) renderView).setParentView(hippyRecyclerView);
initPullHeadEventHelper((PullHeaderRenderNode) renderNode, renderView);
return new HippyRecyclerViewHolder(headerEventHelper.getView(), renderNode);
((HippyPullHeaderView) renderView).setRecyclerView(hippyRecyclerView);
initHeaderRefreshHelper(renderView, renderNode);
return new HippyRecyclerViewHolder(headerRefreshHelper.getView(), renderNode);
} else if (renderView instanceof HippyPullFooterView) {
((HippyPullFooterView) renderView).setRecyclerView(hippyRecyclerView);
initFooterRefreshHelper(renderView, renderNode);
return new HippyRecyclerViewHolder(footerRefreshHelper.getView(), renderNode);
} else if (isStickyPosition(positionToCreateHolder)) {
return new HippyRecyclerViewHolder(getStickyContainer(parent, renderView), renderNode);
} else {
Expand Down Expand Up @@ -159,13 +165,6 @@ private FrameLayout getStickyContainer(ViewGroup parent, View renderView) {
return container;
}

private void initPullHeadEventHelper(PullHeaderRenderNode renderNode, View renderView) {
if (headerEventHelper == null) {
headerEventHelper = new PullHeaderEventHelper(hippyRecyclerView, renderNode);
}
headerEventHelper.setRenderNodeView(renderView);
}

@Override
public String toString() {
return "HippyRecyclerAdapter: itemCount:" + getItemCount();
Expand All @@ -191,35 +190,62 @@ public void onBindViewHolder(HippyRecyclerViewHolder hippyRecyclerViewHolder, in
}
newNode.setRecycleItemTypeChangeListener(this);
hippyRecyclerViewHolder.bindNode = newNode;
enablePullFooter(position, hippyRecyclerViewHolder.itemView);
}

/**
* 检测最后一个item是否是footer,如果是,需要对这个itemView设置监控,footer显示就通知前端加载下一页
*/
private void enablePullFooter(int position, View itemView) {
if (position == getItemCount() - 1) {
ListItemRenderNode renderNode = getChildNodeByAdapterPosition(position);
if (renderNode.isPullFooter()) {
if (footerEventHelper == null) {
footerEventHelper = new PullFooterEventHelper(hippyRecyclerView);
}
footerEventHelper.enableFooter(itemView);
} else {
if (footerEventHelper != null) {
footerEventHelper.disableFooter();
}
}
public void onFooterRefreshCompleted() {
if (footerRefreshHelper != null) {
footerRefreshHelper.onRefreshCompleted();
}
}

public void onFooterDestroy() {
if (footerRefreshHelper != null) {
footerRefreshHelper.onDestroy();
}
}

public void onHeaderRefreshCompleted() {
if (headerRefreshHelper != null) {
headerRefreshHelper.onRefreshCompleted();
}
}

public void onHeaderDestroy() {
if (headerRefreshHelper != null) {
headerRefreshHelper.onDestroy();
}
}

public void enableHeaderRefresh() {
if (headerRefreshHelper != null) {
headerRefreshHelper.enableRefresh();
}
}

private void initHeaderRefreshHelper(View itemView, RenderNode node) {
if (headerRefreshHelper == null) {
headerRefreshHelper = new PullHeaderRefreshHelper(hippyRecyclerView, node);
}
headerRefreshHelper.setItemView(itemView);
}

private void initFooterRefreshHelper(View itemView, RenderNode node) {
if (footerRefreshHelper == null) {
footerRefreshHelper = new PullFooterRefreshHelper(hippyRecyclerView, node);
}
footerRefreshHelper.setItemView(itemView);
}

/**
* 设置View的LayoutParams排版属性,宽高由render节点提供
* 对于LinearLayout的排版,竖向排版,宽度强行顶满,横向排版,高度强行顶满
*/
protected void setLayoutParams(View itemView, int position) {
LayoutParams childLp = getLayoutParams(itemView);
RenderNode childNode = getChildNodeByAdapterPosition(position);
if (childNode instanceof PullFooterRenderNode || childNode instanceof PullHeaderRenderNode) {
return;
}
if (HippyListUtils.isLinearLayout(hippyRecyclerView)) {
boolean isVertical = HippyListUtils.isVerticalLayout(hippyRecyclerView);
childLp.height = isVertical ? childNode.getHeight() : MATCH_PARENT;
Expand Down Expand Up @@ -332,8 +358,15 @@ public int getRenderNodeHeight(int position) {
ListItemRenderNode childNode = getChildNode(position);
if (childNode != null) {
if (childNode.isPullHeader()) {
if (headerEventHelper != null) {
return headerEventHelper.getVisibleHeight();
if (headerRefreshHelper != null) {
return headerRefreshHelper.getVisibleHeight();
}

return 0;
}
if (childNode.isPullFooter()) {
if (footerRefreshHelper != null) {
return footerRefreshHelper.getVisibleHeight();
}

return 0;
Expand All @@ -354,6 +387,20 @@ public int getItemWidth(int position) {
public int getRenderNodeWidth(int position) {
ListItemRenderNode childNode = getChildNode(position);
if (childNode != null) {
if (childNode.isPullHeader()) {
if (headerRefreshHelper != null) {
return headerRefreshHelper.getVisibleWidth();
}

return 0;
}
if (childNode.isPullFooter()) {
if (footerRefreshHelper != null) {
return footerRefreshHelper.getVisibleWidth();
}

return 0;
}
return childNode.getWidth();
}
return 0;
Expand Down Expand Up @@ -398,22 +445,6 @@ private boolean isPullHeader(int position) {
return false;
}

public void resetPullHeaderPositionIfNeeded(int offsetY) {
ListItemRenderNode renderNode = getChildNodeByAdapterPosition(0);
if (renderNode != null && renderNode.isPullHeader()) {
if (headerEventHelper != null && offsetY <= headerEventHelper.getVisibleHeight()) {
headerEventHelper.resetPullHeaderPositionIfNeeded();
}
}
}

/**
* 获取下拉刷新的事件辅助器
*/
public PullHeaderEventHelper getHeaderEventHelper() {
return headerEventHelper;
}

public PreloadHelper getPreloadHelper() {
return preloadHelper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
public class HippyRecyclerView<ADP extends HippyRecyclerListAdapter> extends HippyRecyclerViewBase
implements IHeaderAttachListener, IHippyViewAboundListener {

private static int DEFAULT_ITEM_VIEW_CACHE_SIZE = 8;
protected ADP listAdapter;
protected boolean isEnableScroll = true; //使能ListView的滚动功能
protected StickyHeaderHelper stickyHeaderHelper; //支持吸顶
Expand Down Expand Up @@ -91,6 +92,7 @@ public void setHeaderHost(IHeaderHost headerHost) {
public void initRecyclerView() {
setAdapter(new HippyRecyclerListAdapter<HippyRecyclerView>(this));
intEventHelper();
setItemViewCacheSize(DEFAULT_ITEM_VIEW_CACHE_SIZE);
}


Expand Down Expand Up @@ -139,7 +141,6 @@ public void setListData() {
renderNodeCount = getAdapter().getRenderNodeCount();
dispatchLayout();
if (renderNodeCount > 0) {
getAdapter().resetPullHeaderPositionIfNeeded(getContentOffsetY());
if (mInitialContentOffset > 0 && getChildCount() > 0) {
scrollToInitContentOffset();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ public void setInitialContentOffset(HRW viewWrapper, int offset) {
viewWrapper.getRecyclerView().setInitialContentOffset((int) PixelUtil.dp2px(offset));
}

@HippyControllerProps(name = "itemViewCacheSize", defaultType = HippyControllerProps.NUMBER, defaultNumber = 0)
public void setItemViewCacheSize(HRW viewWrapper, int size) {
viewWrapper.getRecyclerView().setItemViewCacheSize(Math.max(size, 2));
}

@Override
public void onAfterUpdateProps(HRW viewWrapper) {
super.onAfterUpdateProps(viewWrapper);
Expand Down Expand Up @@ -244,7 +249,7 @@ public void dispatchFunction(HRW view, String functionName, HippyArray dataArray
break;
}
case COLLAPSE_PULL_HEADER: {
getAdapter(view).getHeaderEventHelper().onHeaderRefreshFinish();
getAdapter(view).onHeaderRefreshCompleted();
break;
}
case COLLAPSE_PULL_HEADER_WITH_OPTIONS: {
Expand All @@ -261,18 +266,15 @@ public void dispatchFunction(HRW view, String functionName, HippyArray dataArray
view.postDelayed(new Runnable() {
@Override
public void run() {
PullHeaderEventHelper helper = adapter.getHeaderEventHelper();
if (helper != null) {
helper.onHeaderRefreshFinish();
}
adapter.onHeaderRefreshCompleted();
}
}, time);
} else {
adapter.getHeaderEventHelper().onHeaderRefreshFinish();
adapter.onHeaderRefreshCompleted();
}
}
case EXPAND_PULL_HEADER: {
getAdapter(view).getHeaderEventHelper().onHeaderRefresh();
getAdapter(view).onHeaderRefreshCompleted();
break;
}
}
Expand Down
Loading

0 comments on commit b55013b

Please sign in to comment.