Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Android): fix memory leak #430

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class HMListAdapter extends RecyclerView.Adapter<HMListAdapter.ViewHolder
private JSCallback typeCallback;
private JSCallback createCallback;
private JSCallback updateCallback;
private RecycleViewPoolCallback recycleViewPoolCallback;

private Context mContext;

Expand All @@ -42,6 +43,10 @@ public void destroy() {
}
}

public void setRecycleViewPoolCallback(RecycleViewPoolCallback callback){
recycleViewPoolCallback = callback;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
Expand All @@ -56,16 +61,19 @@ public void onDetachedFromRecyclerView(RecyclerView recyclerView) {

@Override
public int getItemViewType(int position) {
if (typeCallback == null) {
return super.getItemViewType(position);
int viewType = super.getItemViewType(position);;
if (typeCallback != null) {
Object retType = typeCallback.call(position);
if (retType != null) {
viewType = ((Number) retType).intValue();
}
}

Object retType = typeCallback.call(position);
if (retType == null) {
return super.getItemViewType(position);
if(recycleViewPoolCallback != null){
recycleViewPoolCallback.updatePoolSize(viewType);
}

return ((Number) retType).intValue();
return viewType;
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Map;

/**
Expand All @@ -52,6 +53,8 @@
*/
@Component("List")
public class List extends HMBase<SmartRefreshLayout> {
private final static int MAX_RECYCLE_POOL_SIZE = 100;

private static final int MODE_LIST = 1;
private static final int MODE_GRID = 2;
private static final int MODE_WATERFALL = 3;
Expand Down Expand Up @@ -92,6 +95,20 @@ public class List extends HMBase<SmartRefreshLayout> {
private int scrollOffsetX = 0;
private int scrollOffsetY = 0;

private HashSet<Integer> registeredViewType;
private RecycleViewPoolCallback recycleViewPoolCallback = new RecycleViewPoolCallback() {
@Override
public void updatePoolSize(int viewType) {
if(registeredViewType == null){
registeredViewType = new HashSet<>();
}
if(!registeredViewType.contains(viewType)){
recyclerView.getRecycledViewPool().setMaxRecycledViews(viewType, MAX_RECYCLE_POOL_SIZE);
registeredViewType.add(viewType);
}
}
};

private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
Expand Down Expand Up @@ -171,6 +188,7 @@ public List(HummerContext context, JSValue jsValue, String viewID) {
protected SmartRefreshLayout createViewInstance(Context context) {
// 这里不用代码new一个RecyclerView,而是通过xml,是为了解决设置scrollerbar显示无效的问题
recyclerView = (RecyclerView) LayoutInflater.from(context).inflate(R.layout.recycler_view, null, false);

recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
recyclerView.setClipChildren(false);
recyclerView.setOnTouchListener((v, event) -> {
Expand Down Expand Up @@ -243,6 +261,7 @@ public void onCreate() {
super.onCreate();
recyclerView.addOnScrollListener(mOnScrollListener);
adapter = new HMListAdapter(getContext(), instanceManager);
adapter.setRecycleViewPoolCallback(recycleViewPoolCallback);
recyclerView.setAdapter(adapter);

recyclerViewNode = YogaNodeUtil.createYogaNode();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.didi.hummer.component.list;

interface RecycleViewPoolCallback {
void updatePoolSize(int viewType);
}