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

Commit

Permalink
fix #63 #57 provide default handler to layoutHelper background
Browse files Browse the repository at this point in the history
  • Loading branch information
longerian committed May 14, 2017
1 parent 1e96824 commit e484414
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.alibaba.android.vlayout.layout;

import com.alibaba.android.vlayout.LayoutManagerHelper;
import com.alibaba.android.vlayout.R;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.VirtualLayoutManager.LayoutStateWrapper;

Expand Down Expand Up @@ -89,6 +90,7 @@ public int getItemCount() {
return mItemCount;
}

@Override
public void setItemCount(int itemCount) {
this.mItemCount = itemCount;
}
Expand Down Expand Up @@ -138,6 +140,9 @@ public void beforeLayout(RecyclerView.Recycler recycler, RecyclerView.State stat
} else {
// if no layoutView is required, remove it
if (mLayoutView != null) {
if (mLayoutViewUnBindListener != null) {
mLayoutViewUnBindListener.onUnbind(mLayoutView, this);
}
helper.removeChildView(mLayoutView);
mLayoutView = null;
}
Expand Down Expand Up @@ -173,9 +178,9 @@ public void afterLayout(RecyclerView.Recycler recycler, RecyclerView.State state

if (!mLayoutRegion.isEmpty()) {
if (isValidScrolled(scrolled)) {
if (helper.getOrientation() == VirtualLayoutManager.VERTICAL)
if (helper.getOrientation() == VirtualLayoutManager.VERTICAL) {
mLayoutRegion.offset(0, -scrolled);
else {
} else {
mLayoutRegion.offset(-scrolled, 0);
}
}
Expand Down Expand Up @@ -230,6 +235,9 @@ public void afterLayout(RecyclerView.Recycler recycler, RecyclerView.State state
public final void clear(LayoutManagerHelper helper) {
// remove LayoutViews if there is one
if (mLayoutView != null) {
if (mLayoutViewUnBindListener != null) {
mLayoutViewUnBindListener.onUnbind(mLayoutView, this);
}
helper.removeChildView(mLayoutView);
mLayoutView = null;
}
Expand Down Expand Up @@ -309,14 +317,83 @@ public interface LayoutViewUnBindListener {
void onUnbind(View layoutView, BaseLayoutHelper baseLayoutHelper);
}


public interface LayoutViewHelper {

/**
* Implement it by maintaining a map between layoutView and image url or setting a unique tag to view. It's up to your choice.
* @param layoutView view ready to be binded with an image
* * @param id layoutView's identifier
*/
void onBindViewSuccess(View layoutView, String id);
}

private LayoutViewUnBindListener mLayoutViewUnBindListener;

private LayoutViewBindListener mLayoutViewBindListener;

/**
* Helper to decide whether call {@link LayoutViewBindListener#onBind(View, BaseLayoutHelper)}.
* Here is a performance issue: {@link LayoutViewBindListener#onBind(View, BaseLayoutHelper)} is called during layout phase,
* when binding image to it would cause view tree to relayout, then the same {@link LayoutViewBindListener#onBind(View, BaseLayoutHelper)} would be called.
* User should provide enough information to tell LayoutHelper whether image has been bind success.
* If image has been successfully binded , no more dead loop happens.
*
* Of course you can handle this logic by yourself and ignore this helper.
*/
public static class DefaultLayoutViewHelper implements LayoutViewBindListener, LayoutViewUnBindListener, LayoutViewHelper {

private final LayoutViewBindListener mLayoutViewBindListener;

private final LayoutViewUnBindListener mLayoutViewUnBindListener;

public DefaultLayoutViewHelper(
LayoutViewBindListener layoutViewBindListener,
LayoutViewUnBindListener layoutViewUnBindListener) {
mLayoutViewBindListener = layoutViewBindListener;
mLayoutViewUnBindListener = layoutViewUnBindListener;
}

@Override
public void onBindViewSuccess(View layoutView, String id) {
layoutView.setTag(R.id.tag_layout_helper_bg, id);
}

@Override
public void onBind(View layoutView, BaseLayoutHelper baseLayoutHelper) {
if (layoutView.getTag(R.id.tag_layout_helper_bg) == null) {
if (mLayoutViewBindListener != null) {
mLayoutViewBindListener.onBind(layoutView, baseLayoutHelper);
}
}
}

@Override
public void onUnbind(View layoutView, BaseLayoutHelper baseLayoutHelper) {
if (mLayoutViewUnBindListener != null) {
mLayoutViewUnBindListener.onUnbind(layoutView, baseLayoutHelper);
}
layoutView.setTag(R.id.tag_layout_helper_bg, null);
}
}

public void setLayoutViewHelper(DefaultLayoutViewHelper layoutViewHelper) {
mLayoutViewBindListener = layoutViewHelper;
mLayoutViewUnBindListener = layoutViewHelper;
}

/**
* Better to use {@link #setLayoutViewHelper(DefaultLayoutViewHelper)}
* @param bindListener
*/
public void setLayoutViewBindListener(LayoutViewBindListener bindListener) {
mLayoutViewBindListener = bindListener;
}

/**
* Better to use {@link #setLayoutViewHelper(DefaultLayoutViewHelper)}
* @param layoutViewUnBindListener
*/
public void setLayoutViewUnBindListener(
LayoutViewUnBindListener layoutViewUnBindListener) {
mLayoutViewUnBindListener = layoutViewUnBindListener;
Expand All @@ -338,7 +415,9 @@ public void bindLayoutView(@NonNull final View layoutView) {
}

protected void handleStateOnResult(LayoutChunkResult result, View view) {
if (view == null) return;
if (view == null) {
return;
}

RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ public void setBgColor(int bgColor) {
mRangeStyle.setBgColor(bgColor);
}

@Override
public void setLayoutViewHelper(DefaultLayoutViewHelper layoutViewHelper) {
mRangeStyle.setLayoutViewHelper(layoutViewHelper);
}

@Override
public void setLayoutViewBindListener(LayoutViewBindListener bindListener) {
mRangeStyle.setLayoutViewBindListener(bindListener);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.alibaba.android.vlayout.layout;

import java.lang.reflect.Array;
import java.util.Arrays;

import com.alibaba.android.vlayout.LayoutManagerHelper;
import com.alibaba.android.vlayout.Range;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.BaseLayoutHelper.DefaultLayoutViewHelper;
import com.alibaba.android.vlayout.layout.BaseLayoutHelper.LayoutViewBindListener;
import com.alibaba.android.vlayout.layout.BaseLayoutHelper.LayoutViewUnBindListener;

import android.graphics.Rect;
import android.os.Parcelable.ClassLoaderCreator;
import android.support.annotation.NonNull;
import android.support.v4.util.ArrayMap;
import android.support.v4.util.SimpleArrayMap;
Expand Down Expand Up @@ -379,6 +378,9 @@ public void beforeLayout(RecyclerView.Recycler recycler, RecyclerView.State stat
} else {
// if no layoutView is required, remove it
if (mLayoutView != null) {
if (mLayoutViewUnBindListener != null) {
mLayoutViewUnBindListener.onUnbind(mLayoutView, getLayoutHelper());
}
helper.removeChildView(mLayoutView);
mLayoutView = null;
}
Expand Down Expand Up @@ -515,6 +517,11 @@ public void bindLayoutView(@NonNull final View layoutView) {
mLayoutRegion.set(0, 0, 0, 0);
}

public void setLayoutViewHelper(DefaultLayoutViewHelper layoutViewHelper) {
mLayoutViewBindListener = layoutViewHelper;
mLayoutViewUnBindListener = layoutViewHelper;
}

public void setLayoutViewBindListener(LayoutViewBindListener bindListener) {
mLayoutViewBindListener = bindListener;
}
Expand All @@ -530,6 +537,9 @@ public void setBgColor(int bgColor) {

public void onClear(LayoutManagerHelper helper) {
if (mLayoutView != null) {
if (mLayoutViewUnBindListener != null) {
mLayoutViewUnBindListener.onUnbind(mLayoutView, getLayoutHelper());
}
helper.removeChildView(mLayoutView);
mLayoutView = null;
}
Expand Down
4 changes: 4 additions & 0 deletions vlayout/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="tag_layout_helper_bg" type="id" />
</resources>

0 comments on commit e484414

Please sign in to comment.