feat(autojs): list.on("item_bind")
This commit is contained in:
@@ -88,25 +88,12 @@ public class ViewAttributes {
|
||||
private final Drawables mDrawables;
|
||||
private final View mView;
|
||||
|
||||
public ViewAttributes(Drawables drawables, View view) {
|
||||
mDrawables = drawables;
|
||||
public ViewAttributes(ResourceParser resourceParser, View view) {
|
||||
mDrawables = resourceParser.getDrawables();
|
||||
mView = view;
|
||||
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) {
|
||||
return mAttributes.containsKey(name);
|
||||
}
|
||||
|
||||
@@ -1,22 +1,16 @@
|
||||
package com.stardust.autojs.core.ui.nativeview;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.os.Build;
|
||||
import android.view.View;
|
||||
import android.widget.CompoundButton;
|
||||
|
||||
import com.stardust.autojs.core.eventloop.EventEmitter;
|
||||
import com.stardust.autojs.core.ui.BaseEvent;
|
||||
import com.stardust.autojs.R;
|
||||
import com.stardust.autojs.core.ui.JsViewHelper;
|
||||
import com.stardust.autojs.core.ui.attribute.ViewAttributes;
|
||||
import com.stardust.autojs.core.ui.widget.JsListView;
|
||||
import com.stardust.autojs.rhino.NativeJavaObjectWithPrototype;
|
||||
|
||||
import org.mozilla.javascript.NativeJavaObject;
|
||||
import org.mozilla.javascript.ScriptRuntime;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
public class NativeView extends NativeJavaObjectWithPrototype {
|
||||
|
||||
private static final String LOG_TAG = "NativeView";
|
||||
@@ -46,15 +40,34 @@ public class NativeView extends NativeJavaObjectWithPrototype {
|
||||
|
||||
private final ViewAttributes mViewAttributes;
|
||||
private final View mView;
|
||||
private final ViewPrototype mViewPrototype;
|
||||
|
||||
public NativeView(Scriptable scope, View javaObject, Class<?> staticType, com.stardust.autojs.runtime.ScriptRuntime runtime) {
|
||||
super(scope, javaObject, staticType);
|
||||
mViewAttributes = ViewAttributes.fromView(runtime.ui.getResourceParser(), javaObject);
|
||||
mViewAttributes = new ViewAttributes(runtime.ui.getResourceParser(), javaObject);
|
||||
mView = javaObject;
|
||||
ViewPrototype viewPrototype = new ViewPrototype(mView, scope, runtime);
|
||||
prototype = new NativeJavaObject(scope, viewPrototype, viewPrototype.getClass());
|
||||
mViewPrototype = new ViewPrototype(mView, scope, runtime);
|
||||
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
|
||||
public boolean has(String name, Scriptable start) {
|
||||
@@ -80,9 +93,20 @@ public class NativeView extends NativeJavaObjectWithPrototype {
|
||||
if (attribute != null) {
|
||||
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
|
||||
public View unwrap() {
|
||||
|
||||
@@ -7,9 +7,14 @@ import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.stardust.autojs.R;
|
||||
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 org.mozilla.javascript.NativeJavaObject;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
@@ -83,7 +88,26 @@ public class JsListView extends RecyclerView {
|
||||
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) {
|
||||
super(itemView);
|
||||
@@ -99,7 +123,14 @@ public class JsListView extends RecyclerView {
|
||||
int pos = getAdapterPosition();
|
||||
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> {
|
||||
@@ -121,7 +152,9 @@ public class JsListView extends RecyclerView {
|
||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||
try {
|
||||
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);
|
||||
applyDynamicAttrs(mItemTemplate, holder.itemView, JsListView.this);
|
||||
mScriptRuntime.ui.setBindingContext(oldCtx);
|
||||
|
||||
@@ -175,8 +175,6 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
|
||||
private class WrapFactory extends org.mozilla.javascript.WrapFactory {
|
||||
|
||||
private final WeakHashMap<Object, Scriptable> mWrapCache = new WeakHashMap<>();
|
||||
|
||||
@Override
|
||||
public Object wrap(Context cx, Scriptable scope, Object obj, Class<?> staticType) {
|
||||
if (obj instanceof String) {
|
||||
@@ -191,12 +189,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
@Override
|
||||
public Scriptable wrapAsJavaObject(Context cx, Scriptable scope, Object javaObject, Class<?> staticType) {
|
||||
if (javaObject instanceof View) {
|
||||
Scriptable wrap = mWrapCache.get(javaObject);
|
||||
if(wrap == null){
|
||||
wrap = new NativeView(scope, (View) javaObject, staticType, getRuntime());
|
||||
mWrapCache.put(javaObject, wrap);
|
||||
}
|
||||
return wrap;
|
||||
return NativeView.fromView(scope, (View) javaObject, staticType, getRuntime());
|
||||
}
|
||||
return super.wrapAsJavaObject(cx, scope, javaObject, staticType);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<item name="view_tag_view_attrs" type="id"/>
|
||||
<item name="view_tag_native_view" type="id"/>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user