feat:android support listItem's actions

This commit is contained in:
pengfei.zhou 2021-04-25 15:47:59 +08:00 committed by osborn
parent e18b7e781e
commit 658f0ed3b3
2 changed files with 43 additions and 1 deletions

View File

@ -15,11 +15,18 @@
*/ */
package pub.doric.shader.list; package pub.doric.shader.list;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.FrameLayout; import android.widget.FrameLayout;
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;
import java.util.ArrayList;
import java.util.List;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.shader.StackNode; import pub.doric.shader.StackNode;
@ -32,6 +39,7 @@ import pub.doric.shader.StackNode;
@DoricPlugin(name = "ListItem") @DoricPlugin(name = "ListItem")
public class ListItemNode extends StackNode { public class ListItemNode extends StackNode {
public String identifier = ""; public String identifier = "";
private JSArray actions = null;
public ListItemNode(DoricContext doricContext) { public ListItemNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
@ -40,7 +48,11 @@ public class ListItemNode extends StackNode {
@Override @Override
protected void blend(FrameLayout view, String name, JSValue prop) { protected void blend(FrameLayout view, String name, JSValue prop) {
if ("identifier".equals(name)) { if ("actions".equals(name)) {
if (prop.isArray()) {
this.actions = prop.asArray();
}
} else if ("identifier".equals(name)) {
this.identifier = prop.asString().value(); this.identifier = prop.asString().value();
} else { } else {
super.blend(view, name, prop); super.blend(view, name, prop);
@ -52,5 +64,33 @@ public class ListItemNode extends StackNode {
super.blend(jsObject); super.blend(jsObject);
getNodeView().getLayoutParams().width = getLayoutParams().width; getNodeView().getLayoutParams().width = getLayoutParams().width;
getNodeView().getLayoutParams().height = getLayoutParams().height; getNodeView().getLayoutParams().height = getLayoutParams().height;
if (this.actions != null && this.actions.size() > 0) {
getView().setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (actions == null || actions.size() == 0) {
return false;
}
String[] items = new String[actions.size()];
final String[] callbacks = new String[actions.size()];
for (int i = 0; i < actions.size(); i++) {
JSObject action = actions.get(i).asObject();
String title = action.getProperty("title").asString().value();
String callback = action.getProperty("callback").asString().value();
items[i] = title;
callbacks[i] = callback;
}
new AlertDialog.Builder(getContext())
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
callJSResponse(callbacks[which]);
dialog.dismiss();
}
}).show();
return true;
}
});
}
} }
} }

View File

@ -84,12 +84,14 @@ class ListVM extends ViewModel<ListModel, ListVH> {
actions: [ actions: [
{ {
title: "First", title: "First",
backgroundColor: Color.RED,
callback: () => { callback: () => {
modal(context).alert("First action") modal(context).alert("First action")
} }
}, },
{ {
title: "Second", title: "Second",
backgroundColor: Color.BLUE,
callback: () => { callback: () => {
modal(context).alert("Second action") modal(context).alert("Second action")
} }