sample(ui): music file browser
This commit is contained in:
@@ -52,11 +52,6 @@ runtime.init();
|
||||
},
|
||||
toString: function(o){
|
||||
return String(o);
|
||||
},
|
||||
eval: function(context, expr){
|
||||
with(context){
|
||||
return eval(expr);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ module.exports = function (runtime, global) {
|
||||
ui.findById = function (id) {
|
||||
if (!ui.view)
|
||||
return null;
|
||||
var v = ui.findByStringId(ui.view.getChildAt(0), id);
|
||||
var v = ui.findByStringId(ui.view, id);
|
||||
if (v) {
|
||||
v = decorate(v);
|
||||
}
|
||||
@@ -159,13 +159,14 @@ module.exports = function (runtime, global) {
|
||||
}
|
||||
|
||||
function evalInContext(expr, ctx) {
|
||||
return global.__exitIfError__(function () {
|
||||
log(ctx.toString());
|
||||
with(ctx){
|
||||
return eval(expr);
|
||||
return global.__exitIfError__(function() {
|
||||
with(ctx) {
|
||||
return (function(){
|
||||
return eval(expr);
|
||||
}).call(ctx);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
function decorate(view) {
|
||||
@@ -283,7 +284,9 @@ module.exports = function (runtime, global) {
|
||||
adapter.notifyItemRangeInserted(change.index, change.addedCount);
|
||||
}
|
||||
} else if (change.type == 'update') {
|
||||
adapter.notifyItemChanged(parseInt(change.name));
|
||||
try{
|
||||
adapter.notifyItemChanged(parseInt(change.name));
|
||||
}catch(e){}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -292,7 +295,25 @@ module.exports = function (runtime, global) {
|
||||
}
|
||||
|
||||
function decorateList(list) {
|
||||
|
||||
list.setOnItemTouchListener({
|
||||
onItemClick: function(listView, itemView, item, pos){
|
||||
emit("item_click", item, pos, itemView, listView);
|
||||
},
|
||||
onItemLongClick: function(listView, itemView, item, pos){
|
||||
var event = {};
|
||||
event.consumed = false;
|
||||
emit("item_long_click", event, item, pos, itemView, listView);
|
||||
return event.consumed;
|
||||
}
|
||||
});
|
||||
function emit() {
|
||||
var args = arguments;
|
||||
global.__exitIfError__(function () {
|
||||
//不支持使用apply的原因是rhino会把参数中的primitive变成object
|
||||
functionApply(list.emit, args);
|
||||
//view.emit.apply(view, args);
|
||||
});
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@@ -345,7 +366,7 @@ module.exports = function (runtime, global) {
|
||||
cache = ui.findById(name);
|
||||
if (cache) {
|
||||
ui.__view_cache__[name] = cache;
|
||||
return cache;
|
||||
return cache;
|
||||
}
|
||||
}
|
||||
return ui[name];
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
package com.stardust.autojs.core.ui.inflater.inflaters;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.stardust.autojs.core.ui.inflater.DynamicLayoutInflater;
|
||||
import com.stardust.autojs.core.ui.inflater.ResourceParser;
|
||||
import com.stardust.autojs.core.ui.inflater.ViewCreator;
|
||||
import com.stardust.autojs.core.ui.widget.JsListView;
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.autojs.workground.WrapContentLinearLayoutManager;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
@@ -29,7 +32,13 @@ public class JsListViewInflater extends BaseViewInflater<JsListView> {
|
||||
|
||||
@Override
|
||||
public boolean setAttr(JsListView view, String attr, String value, ViewGroup parent, Map<String, String> attrs) {
|
||||
return super.setAttr(view, attr, value, parent, attrs);
|
||||
switch (attr) {
|
||||
case "orientation":
|
||||
view.setLayoutManager(new WrapContentLinearLayoutManager(view.getContext(), LinearLayoutInflater.ORIENTATIONS.get(value), false));
|
||||
return true;
|
||||
default:
|
||||
return super.setAttr(view, attr, value, parent, attrs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,7 +15,7 @@ import java.util.Map;
|
||||
|
||||
public class LinearLayoutInflater<V extends LinearLayout> extends ViewGroupInflater<V> {
|
||||
|
||||
private static final ValueMapper<Integer> ORIENTATIONS = new ValueMapper<Integer>("orientation")
|
||||
static final ValueMapper<Integer> ORIENTATIONS = new ValueMapper<Integer>("orientation")
|
||||
.map("vertical", LinearLayout.VERTICAL)
|
||||
.map("horizontal", LinearLayout.HORIZONTAL);
|
||||
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
package com.stardust.autojs.core.ui.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.stardust.autojs.core.ui.inflater.DynamicLayoutInflater;
|
||||
import com.stardust.autojs.core.ui.inflater.ViewInflater;
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import java.util.Map;
|
||||
import com.stardust.autojs.workground.WrapContentLinearLayoutManager;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/3/28.
|
||||
@@ -31,11 +28,18 @@ public class JsListView extends RecyclerView {
|
||||
void setDataSource(Object dataSource);
|
||||
}
|
||||
|
||||
public interface OnItemTouchListener {
|
||||
void onItemClick(JsListView listView, View itemView, Object item, int pos);
|
||||
|
||||
boolean onItemLongClick(JsListView listView, View itemView, Object item, int pos);
|
||||
}
|
||||
|
||||
private Node mItemTemplate;
|
||||
private DynamicLayoutInflater mDynamicLayoutInflater;
|
||||
private ScriptRuntime mScriptRuntime;
|
||||
private Object mDataSource;
|
||||
private DataSourceAdapter mDataSourceAdapter;
|
||||
private OnItemTouchListener mOnItemTouchListener;
|
||||
|
||||
public JsListView(Context context, ScriptRuntime scriptRuntime) {
|
||||
super(context);
|
||||
@@ -45,7 +49,11 @@ public class JsListView extends RecyclerView {
|
||||
|
||||
private void init() {
|
||||
setAdapter(new Adapter());
|
||||
setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
setLayoutManager(new WrapContentLinearLayoutManager(getContext()));
|
||||
}
|
||||
|
||||
public void setOnItemTouchListener(OnItemTouchListener onItemTouchListener) {
|
||||
mOnItemTouchListener = onItemTouchListener;
|
||||
}
|
||||
|
||||
public void setDataSourceAdapter(DataSourceAdapter dataSourceAdapter) {
|
||||
@@ -73,6 +81,18 @@ public class JsListView extends RecyclerView {
|
||||
|
||||
public ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
itemView.setOnClickListener(v -> {
|
||||
if (mOnItemTouchListener != null) {
|
||||
int pos = getAdapterPosition();
|
||||
mOnItemTouchListener.onItemClick(JsListView.this, itemView, mDataSourceAdapter.getItem(mDataSource, pos), pos);
|
||||
}
|
||||
});
|
||||
itemView.setOnLongClickListener(v -> {
|
||||
if (mOnItemTouchListener == null)
|
||||
return false;
|
||||
int pos = getAdapterPosition();
|
||||
return mOnItemTouchListener.onItemLongClick(JsListView.this, itemView, mDataSourceAdapter.getItem(mDataSource, pos), pos);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,15 +100,24 @@ public class JsListView extends RecyclerView {
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
return new ViewHolder(mDynamicLayoutInflater.inflate(mItemTemplate, parent, false));
|
||||
try {
|
||||
return new ViewHolder(mDynamicLayoutInflater.inflate(mItemTemplate, parent, false));
|
||||
} catch (Exception e) {
|
||||
mScriptRuntime.exit(e);
|
||||
return new ViewHolder(new View(parent.getContext()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||
Object oldCtx = mScriptRuntime.ui.getBindingContext();
|
||||
mScriptRuntime.ui.setBindingContext(mDataSourceAdapter.getItem(mDataSource, position));
|
||||
applyDynamicAttrs(mItemTemplate, holder.itemView, (ViewGroup) holder.itemView.getParent());
|
||||
mScriptRuntime.ui.setBindingContext(oldCtx);
|
||||
try {
|
||||
Object oldCtx = mScriptRuntime.ui.getBindingContext();
|
||||
mScriptRuntime.ui.setBindingContext(mDataSourceAdapter.getItem(mDataSource, position));
|
||||
applyDynamicAttrs(mItemTemplate, holder.itemView, (ViewGroup) holder.itemView.getParent());
|
||||
mScriptRuntime.ui.setBindingContext(oldCtx);
|
||||
} catch (Exception e) {
|
||||
mScriptRuntime.exit(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void applyDynamicAttrs(Node node, View itemView, ViewGroup parent) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.stardust.autojs.runtime;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
import com.stardust.autojs.core.accessibility.UiCollection;
|
||||
|
||||
/**
|
||||
@@ -19,7 +21,6 @@ public class ScriptBridges {
|
||||
|
||||
Object toString(Object obj);
|
||||
|
||||
Object eval(Object context, String expr);
|
||||
}
|
||||
|
||||
private Bridges mBridges;
|
||||
@@ -50,9 +51,4 @@ public class ScriptBridges {
|
||||
return mBridges.toString(obj);
|
||||
}
|
||||
|
||||
public Object eval(Object context, String expr) {
|
||||
checkBridges();
|
||||
return mBridges.eval(context, expr);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,10 @@ public class UI extends ProxyObject {
|
||||
}
|
||||
|
||||
public void setBindingContext(Object context) {
|
||||
mProperties.put("bindingContext", context);
|
||||
if (context == null)
|
||||
mProperties.remove("bindingContext");
|
||||
else
|
||||
mProperties.put("bindingContext", context);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.stardust.autojs.workground;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/2/13.
|
||||
* http://stackoverflow.com/questions/31759171/recyclerview-and-java-lang-indexoutofboundsexception-inconsistency-detected-in
|
||||
*/
|
||||
|
||||
public class WrapContentLinearLayoutManager extends LinearLayoutManager {
|
||||
public WrapContentLinearLayoutManager(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
|
||||
super(context, orientation, reverseLayout);
|
||||
}
|
||||
|
||||
public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
|
||||
try {
|
||||
super.onLayoutChildren(recycler, state);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
Log.e("LinearLayoutManager", "Android bug ", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user