feat(autojs): list.on("item_bind")
This commit is contained in:
@@ -6,6 +6,7 @@ ui.layout(
|
|||||||
<vertical>
|
<vertical>
|
||||||
<text id="name" textSize="16sp" textColor="#000000" text="姓名: {{name}}"/>
|
<text id="name" textSize="16sp" textColor="#000000" text="姓名: {{name}}"/>
|
||||||
<text id="age" textSize="16sp" textColor="#000000" text="年龄: {{age}}岁"/>
|
<text id="age" textSize="16sp" textColor="#000000" text="年龄: {{age}}岁"/>
|
||||||
|
<button id="deleteItem" text="删除"/>
|
||||||
</vertical>
|
</vertical>
|
||||||
</list>
|
</list>
|
||||||
</frame>
|
</frame>
|
||||||
@@ -28,6 +29,14 @@ var items = [
|
|||||||
|
|
||||||
ui.list.setDataSource(items);
|
ui.list.setDataSource(items);
|
||||||
|
|
||||||
ui.list.on("item_click", function(i, item, itemView, listView){
|
ui.list.on("item_click", function(item, i, itemView, listView){
|
||||||
toast("被点击的人名字为: " + item.name + ",年龄为: " + item.age);
|
toast("被点击的人名字为: " + item.name + ",年龄为: " + item.age);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ui.list.on("item_bind", function(itemView, itemHolder){
|
||||||
|
itemView.deleteItem.on("click", function(){
|
||||||
|
let item = itemHolder.item;
|
||||||
|
toast("被删除的人名字为: " + item.name + ",年龄为: " + item.age);
|
||||||
|
items.splice(itemHolder.position, 1);
|
||||||
|
});
|
||||||
|
})
|
||||||
@@ -88,25 +88,12 @@ public class ViewAttributes {
|
|||||||
private final Drawables mDrawables;
|
private final Drawables mDrawables;
|
||||||
private final View mView;
|
private final View mView;
|
||||||
|
|
||||||
public ViewAttributes(Drawables drawables, View view) {
|
public ViewAttributes(ResourceParser resourceParser, View view) {
|
||||||
mDrawables = drawables;
|
mDrawables = resourceParser.getDrawables();
|
||||||
mView = view;
|
mView = view;
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ViewAttributes fromView(ResourceParser parser, View view) {
|
|
||||||
ViewAttributes attributes;
|
|
||||||
Object tag = view.getTag(R.id.view_tag_view_attrs);
|
|
||||||
if (!(tag instanceof ViewAttributes)) {
|
|
||||||
attributes = new ViewAttributes(parser.getDrawables(), view);
|
|
||||||
view.setTag(R.id.view_tag_view_attrs, attributes);
|
|
||||||
} else {
|
|
||||||
attributes = (ViewAttributes) tag;
|
|
||||||
}
|
|
||||||
return attributes;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public boolean contains(String name) {
|
public boolean contains(String name) {
|
||||||
return mAttributes.containsKey(name);
|
return mAttributes.containsKey(name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,16 @@
|
|||||||
package com.stardust.autojs.core.ui.nativeview;
|
package com.stardust.autojs.core.ui.nativeview;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.CompoundButton;
|
|
||||||
|
|
||||||
import com.stardust.autojs.core.eventloop.EventEmitter;
|
import com.stardust.autojs.R;
|
||||||
import com.stardust.autojs.core.ui.BaseEvent;
|
import com.stardust.autojs.core.ui.JsViewHelper;
|
||||||
import com.stardust.autojs.core.ui.attribute.ViewAttributes;
|
import com.stardust.autojs.core.ui.attribute.ViewAttributes;
|
||||||
import com.stardust.autojs.core.ui.widget.JsListView;
|
|
||||||
import com.stardust.autojs.rhino.NativeJavaObjectWithPrototype;
|
import com.stardust.autojs.rhino.NativeJavaObjectWithPrototype;
|
||||||
|
|
||||||
import org.mozilla.javascript.NativeJavaObject;
|
import org.mozilla.javascript.NativeJavaObject;
|
||||||
import org.mozilla.javascript.ScriptRuntime;
|
import org.mozilla.javascript.ScriptRuntime;
|
||||||
import org.mozilla.javascript.Scriptable;
|
import org.mozilla.javascript.Scriptable;
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
public class NativeView extends NativeJavaObjectWithPrototype {
|
public class NativeView extends NativeJavaObjectWithPrototype {
|
||||||
|
|
||||||
private static final String LOG_TAG = "NativeView";
|
private static final String LOG_TAG = "NativeView";
|
||||||
@@ -46,15 +40,34 @@ public class NativeView extends NativeJavaObjectWithPrototype {
|
|||||||
|
|
||||||
private final ViewAttributes mViewAttributes;
|
private final ViewAttributes mViewAttributes;
|
||||||
private final View mView;
|
private final View mView;
|
||||||
|
private final ViewPrototype mViewPrototype;
|
||||||
|
|
||||||
public NativeView(Scriptable scope, View javaObject, Class<?> staticType, com.stardust.autojs.runtime.ScriptRuntime runtime) {
|
public NativeView(Scriptable scope, View javaObject, Class<?> staticType, com.stardust.autojs.runtime.ScriptRuntime runtime) {
|
||||||
super(scope, javaObject, staticType);
|
super(scope, javaObject, staticType);
|
||||||
mViewAttributes = ViewAttributes.fromView(runtime.ui.getResourceParser(), javaObject);
|
mViewAttributes = new ViewAttributes(runtime.ui.getResourceParser(), javaObject);
|
||||||
mView = javaObject;
|
mView = javaObject;
|
||||||
ViewPrototype viewPrototype = new ViewPrototype(mView, scope, runtime);
|
mViewPrototype = new ViewPrototype(mView, scope, runtime);
|
||||||
prototype = new NativeJavaObject(scope, viewPrototype, viewPrototype.getClass());
|
prototype = new NativeJavaObject(scope, mViewPrototype, mViewPrototype.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static NativeView fromView(Scriptable scope, View view, Class<?> staticType, com.stardust.autojs.runtime.ScriptRuntime runtime) {
|
||||||
|
Object tag = view.getTag(R.id.view_tag_native_view);
|
||||||
|
if (tag instanceof NativeView) {
|
||||||
|
return (NativeView) tag;
|
||||||
|
} else {
|
||||||
|
NativeView nativeView = new NativeView(scope, view, staticType, runtime);
|
||||||
|
view.setTag(R.id.view_tag_native_view, nativeView);
|
||||||
|
return nativeView;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NativeView fromView(View view) {
|
||||||
|
Object tag = view.getTag(R.id.view_tag_native_view);
|
||||||
|
if (tag instanceof NativeView)
|
||||||
|
return (NativeView) tag;
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean has(String name, Scriptable start) {
|
public boolean has(String name, Scriptable start) {
|
||||||
@@ -80,9 +93,20 @@ public class NativeView extends NativeJavaObjectWithPrototype {
|
|||||||
if (attribute != null) {
|
if (attribute != null) {
|
||||||
return attribute.get();
|
return attribute.get();
|
||||||
}
|
}
|
||||||
return super.get(name, start);
|
if (super.has(name, start)) {
|
||||||
|
return super.get(name, start);
|
||||||
|
} else {
|
||||||
|
View view = JsViewHelper.findViewByStringId(mView, name);
|
||||||
|
if (view != null) {
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Scriptable.NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ViewPrototype getViewPrototype() {
|
||||||
|
return mViewPrototype;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View unwrap() {
|
public View unwrap() {
|
||||||
|
|||||||
@@ -7,9 +7,14 @@ import android.support.v7.widget.RecyclerView;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
import com.stardust.autojs.R;
|
||||||
import com.stardust.autojs.core.ui.inflater.DynamicLayoutInflater;
|
import com.stardust.autojs.core.ui.inflater.DynamicLayoutInflater;
|
||||||
|
import com.stardust.autojs.core.ui.nativeview.NativeView;
|
||||||
|
import com.stardust.autojs.core.ui.nativeview.ViewPrototype;
|
||||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||||
|
|
||||||
|
import org.mozilla.javascript.NativeJavaObject;
|
||||||
|
import org.mozilla.javascript.Scriptable;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
@@ -83,7 +88,26 @@ public class JsListView extends RecyclerView {
|
|||||||
mItemTemplate = itemTemplate;
|
mItemTemplate = itemTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ViewHolder extends RecyclerView.ViewHolder {
|
|
||||||
|
public static class ItemHolder {
|
||||||
|
private final ViewHolder mViewHolder;
|
||||||
|
|
||||||
|
ItemHolder(ViewHolder viewHolder) {
|
||||||
|
mViewHolder = viewHolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPosition() {
|
||||||
|
return mViewHolder.getAdapterPosition();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getItem() {
|
||||||
|
return mViewHolder.item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ViewHolder extends RecyclerView.ViewHolder {
|
||||||
|
|
||||||
|
Object item = null;
|
||||||
|
|
||||||
public ViewHolder(View itemView) {
|
public ViewHolder(View itemView) {
|
||||||
super(itemView);
|
super(itemView);
|
||||||
@@ -99,7 +123,14 @@ public class JsListView extends RecyclerView {
|
|||||||
int pos = getAdapterPosition();
|
int pos = getAdapterPosition();
|
||||||
return mOnItemTouchListener.onItemLongClick(JsListView.this, itemView, mDataSourceAdapter.getItem(mDataSource, pos), pos);
|
return mOnItemTouchListener.onItemLongClick(JsListView.this, itemView, mDataSourceAdapter.getItem(mDataSource, pos), pos);
|
||||||
});
|
});
|
||||||
|
NativeView nativeView = NativeView.fromView(JsListView.this);
|
||||||
|
if (nativeView != null) {
|
||||||
|
ViewPrototype prototype = nativeView.getViewPrototype();
|
||||||
|
prototype.emit("item_bind", itemView, new ItemHolder(this));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
|
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
|
||||||
@@ -121,7 +152,9 @@ public class JsListView extends RecyclerView {
|
|||||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||||
try {
|
try {
|
||||||
Object oldCtx = mScriptRuntime.ui.getBindingContext();
|
Object oldCtx = mScriptRuntime.ui.getBindingContext();
|
||||||
mScriptRuntime.ui.setBindingContext(mDataSourceAdapter.getItem(mDataSource, position));
|
Object item = mDataSourceAdapter.getItem(mDataSource, position);
|
||||||
|
holder.item = item;
|
||||||
|
mScriptRuntime.ui.setBindingContext(item);
|
||||||
mDynamicLayoutInflater.setInflateFlags(DynamicLayoutInflater.FLAG_JUST_DYNAMIC_ATTRS);
|
mDynamicLayoutInflater.setInflateFlags(DynamicLayoutInflater.FLAG_JUST_DYNAMIC_ATTRS);
|
||||||
applyDynamicAttrs(mItemTemplate, holder.itemView, JsListView.this);
|
applyDynamicAttrs(mItemTemplate, holder.itemView, JsListView.this);
|
||||||
mScriptRuntime.ui.setBindingContext(oldCtx);
|
mScriptRuntime.ui.setBindingContext(oldCtx);
|
||||||
|
|||||||
@@ -175,8 +175,6 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
|||||||
|
|
||||||
private class WrapFactory extends org.mozilla.javascript.WrapFactory {
|
private class WrapFactory extends org.mozilla.javascript.WrapFactory {
|
||||||
|
|
||||||
private final WeakHashMap<Object, Scriptable> mWrapCache = new WeakHashMap<>();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object wrap(Context cx, Scriptable scope, Object obj, Class<?> staticType) {
|
public Object wrap(Context cx, Scriptable scope, Object obj, Class<?> staticType) {
|
||||||
if (obj instanceof String) {
|
if (obj instanceof String) {
|
||||||
@@ -191,12 +189,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
|||||||
@Override
|
@Override
|
||||||
public Scriptable wrapAsJavaObject(Context cx, Scriptable scope, Object javaObject, Class<?> staticType) {
|
public Scriptable wrapAsJavaObject(Context cx, Scriptable scope, Object javaObject, Class<?> staticType) {
|
||||||
if (javaObject instanceof View) {
|
if (javaObject instanceof View) {
|
||||||
Scriptable wrap = mWrapCache.get(javaObject);
|
return NativeView.fromView(scope, (View) javaObject, staticType, getRuntime());
|
||||||
if(wrap == null){
|
|
||||||
wrap = new NativeView(scope, (View) javaObject, staticType, getRuntime());
|
|
||||||
mWrapCache.put(javaObject, wrap);
|
|
||||||
}
|
|
||||||
return wrap;
|
|
||||||
}
|
}
|
||||||
return super.wrapAsJavaObject(cx, scope, javaObject, staticType);
|
return super.wrapAsJavaObject(cx, scope, javaObject, staticType);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<item name="view_tag_view_attrs" type="id"/>
|
<item name="view_tag_native_view" type="id"/>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user