listview enable cell refresh

This commit is contained in:
pengfei.zhou 2019-11-13 20:43:05 +08:00
parent 3933582e1e
commit 4dcc89497d
7 changed files with 75 additions and 16 deletions

View File

@ -105,8 +105,21 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
for (ViewNode node : tobeRemoved) { for (ViewNode node : tobeRemoved) {
mChildrenNode.remove(node.getId()); mChildrenNode.remove(node.getId());
} }
} else if ("subviews".equals(name)) {
// Currently do nothing,because its subview always contained in props.children
// super.blend(view, layoutParams, name, prop);
} else { } else {
super.blend(view, layoutParams, name, prop); super.blend(view, layoutParams, name, prop);
} }
} }
@Override
protected void blendSubNode(JSObject subProp) {
String subNodeId = subProp.getProperty("id").asString().value();
for (ViewNode node : mChildrenNode.values()) {
if (subNodeId.equals(node.getId())) {
node.blend(subProp, node.getLayoutParams());
}
}
}
} }

View File

@ -18,6 +18,7 @@ package pub.doric.shader;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
@ -38,6 +39,21 @@ public abstract class SuperNode<V extends View> extends ViewNode<V> {
return new ViewGroup.LayoutParams(0, 0); return new ViewGroup.LayoutParams(0, 0);
} }
@Override
protected void blend(V view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
if (name.equals("subviews")) {
JSArray subviews = prop.asArray();
for (int i = 0; i < subviews.size(); i++) {
JSObject subProp = subviews.get(i).asObject();
blendSubNode(subProp);
}
} else {
super.blend(view, layoutParams, name, prop);
}
}
protected abstract void blendSubNode(JSObject subProperties);
protected void blendSubLayoutConfig(ViewNode viewNode, JSObject jsObject) { protected void blendSubLayoutConfig(ViewNode viewNode, JSObject jsObject) {
JSValue jsValue = jsObject.getProperty("margin"); JSValue jsValue = jsObject.getProperty("margin");
JSValue widthSpec = jsObject.getProperty("widthSpec"); JSValue widthSpec = jsObject.getProperty("widthSpec");

View File

@ -26,6 +26,9 @@ import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import java.util.HashMap;
import java.util.Map;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.shader.ViewNode; import pub.doric.shader.ViewNode;
@ -58,7 +61,7 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolde
@Override @Override
public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) { public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) {
JSObject jsObject = getItemModel(position); JSObject jsObject = getItemModel(position);
holder.listItemNode.setId(String.valueOf(position)); holder.listItemNode.setId(jsObject.getProperty("id").asString().value());
holder.listItemNode.blend(jsObject.getProperty("props").asObject(), holder.itemView.getLayoutParams()); holder.listItemNode.blend(jsObject.getProperty("props").asObject(), holder.itemView.getLayoutParams());
} }
@ -101,6 +104,22 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolde
} }
public void blendSubNode(JSObject subProperties) {
String subNodeId = subProperties.getProperty("id").asString().value();
for (int i = 0; i < itemObjects.size(); i++) {
JSObject jsObject = itemObjects.valueAt(i);
if (subNodeId.equals(jsObject.getProperty("id").asString().value())) {
for (String key : subProperties.propertySet()) {
jsObject.setProperty(key, subProperties.getProperty(key));
}
int position = itemObjects.keyAt(i);
notifyItemChanged(position);
break;
}
}
}
static class DoricViewHolder extends RecyclerView.ViewHolder { static class DoricViewHolder extends RecyclerView.ViewHolder {
ListItemNode listItemNode; ListItemNode listItemNode;

View File

@ -42,6 +42,11 @@ public class ListNode extends SuperNode<RecyclerView> {
this.listAdapter = new ListAdapter(this); this.listAdapter = new ListAdapter(this);
} }
@Override
protected void blendSubNode(JSObject subProperties) {
listAdapter.blendSubNode(subProperties);
}
@Override @Override
protected RecyclerView build(JSObject jsObject) { protected RecyclerView build(JSObject jsObject) {
RecyclerView recyclerView = new RecyclerView(getContext()); RecyclerView recyclerView = new RecyclerView(getContext());

View File

@ -43,6 +43,8 @@ class ListPanel extends Panel {
it.height = 50 it.height = 50
it.onClick = () => { it.onClick = () => {
log(`Click item at ${idx}`) log(`Click item at ${idx}`)
it.bgColor = Color.parse('#000000')
log(`changed,listview is dirty:${list.isDirty()}`)
} }
}) })
} }

View File

@ -40,10 +40,6 @@ export class ListItem extends Stack {
export class List extends Superview { export class List extends Superview {
private cachedViews: Map<string, ListItem> = new Map private cachedViews: Map<string, ListItem> = new Map
subviewById(id: string): ListItem | undefined {
return this.cachedViews.get(id)
}
allSubviews() { allSubviews() {
return this.cachedViews.values() return this.cachedViews.values()
} }

View File

@ -256,7 +256,13 @@ export abstract class View implements Modeling, IView {
} }
export abstract class Superview extends View { export abstract class Superview extends View {
abstract subviewById(id: string): View | undefined subviewById(id: string): View | undefined {
for (let v of this.allSubviews()) {
if (v.viewId === id) {
return v
}
}
}
abstract allSubviews(): Iterable<View> abstract allSubviews(): Iterable<View>
isDirty() { isDirty() {
@ -271,6 +277,17 @@ export abstract class Superview extends View {
} }
return false return false
} }
toModel() {
const subviews = []
for (let v of this.allSubviews()) {
if (v.isDirty()) {
subviews.push(v.toModel())
}
}
this.dirtyProps.subviews = subviews
return super.toModel()
}
} }
export abstract class Group extends Superview { export abstract class Group extends Superview {
@ -284,15 +301,6 @@ export abstract class Group extends Superview {
} }
}) })
subviewById(id: string): View | undefined {
for (let view of this.children) {
if (view.viewId === id) {
return view
}
}
return undefined
}
allSubviews() { allSubviews() {
return this.children return this.children
} }
@ -310,7 +318,7 @@ export abstract class Group extends Superview {
return {} return {}
} }
}) })
return super.toModel() return this.nativeViewModel
} }
} }