Created
May 12, 2017 02:48
-
-
Save talenguyen/19f57d709727257189491447fc87c61a to your computer and use it in GitHub Desktop.
Demo issue of RecyclerBinderUpdateCallback belong to fblitho (http://fblitho.com)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Color; | |
import android.support.v7.util.DiffUtil; | |
import android.support.v7.widget.OrientationHelper; | |
import com.facebook.litho.Column; | |
import com.facebook.litho.ComponentContext; | |
import com.facebook.litho.ComponentInfo; | |
import com.facebook.litho.ComponentLayout; | |
import com.facebook.litho.annotations.LayoutSpec; | |
import com.facebook.litho.annotations.OnCreateLayout; | |
import com.facebook.litho.widget.LinearLayoutInfo; | |
import com.facebook.litho.widget.Recycler; | |
import com.facebook.litho.widget.RecyclerBinder; | |
import com.facebook.litho.widget.RecyclerBinderUpdateCallback; | |
import com.facebook.litho.widget.Text; | |
import java.util.Arrays; | |
import java.util.List; | |
@LayoutSpec | |
public class PlaygroundComponentSpec { | |
@OnCreateLayout | |
static ComponentLayout onCreateLayout( | |
ComponentContext c) { | |
final RecyclerBinder recyclerBinder = new RecyclerBinder( | |
c, | |
4.0f, | |
new LinearLayoutInfo(c, OrientationHelper.VERTICAL, false)); | |
addDataToBinder(recyclerBinder, c); | |
return Column.create(c) | |
.backgroundColor(Color.WHITE) | |
.child( | |
Recycler.create(c) | |
.binder(recyclerBinder) | |
.withLayout() | |
.flex(1) | |
.build()) | |
.build(); | |
} | |
private static void addDataToBinder(RecyclerBinder recyclerBinder, final ComponentContext c) { | |
final List<String> items = Arrays.asList( | |
"Item 1", | |
"Item 2", | |
"Item 3"); | |
final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffUtil.Callback() { | |
@Override public int getOldListSize() { | |
return 0; | |
} | |
@Override public int getNewListSize() { | |
return items.size(); | |
} | |
@Override public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { | |
return false; | |
} | |
@Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { | |
return false; | |
} | |
}); | |
final RecyclerBinderUpdateCallback<String> updateCallback = RecyclerBinderUpdateCallback.acquire( | |
0, | |
items, | |
new RecyclerBinderUpdateCallback.ComponentRenderer<String>() { | |
@Override public ComponentInfo render(String item, int idx) { | |
return ComponentInfo.create() | |
.component( | |
Text.create(c) | |
.textSizeSp(20) | |
.text(item) | |
.build()) | |
.build(); | |
} | |
}, | |
recyclerBinder); | |
diffResult.dispatchUpdatesTo(updateCallback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment