从片段 WebView JavaScript 更新活动图像视图
update activity imageview from fragment webview javascript
有一个活动扩展了RoboFragmentActivity,它的主要UI是一个ViewPager,包含3个片段,xml如下:
<android.support.v4.view.ViewPager
android:id="@+id/goods_details_vp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1.0"
android:background="@color/white"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:background="#cc000000"
android:orientation="horizontal"
android:paddingLeft="10dp" >
<RelativeLayout
android:id="@+id/collection_view_lay"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="2" >
<ImageView
android:id="@+id/collection_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/icon_goods_detials_collct_common" />
</RelativeLayout>
</LinearLayout>
每个片段都有一个 WebView,XML:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/goods_details"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
第一个片段中的 WebView 将调用如下方法:
class InJavaScript {
@JavascriptInterface
public void runOnAndroidJavaScript(String status) {
collectionStatus.setCollectionStatus(status);
}
}
活动中的回调方法:
@Override
public void setCollectionStatus(String str) {
// TODO 设置收藏图标
if ("true".equals(str)) {
collectionView
.setImageResource(R.drawable.icon_goods_detials_collct_pressed);
} else {
collectionView
.setImageResource(R.drawable.icon_goods_detials_collct_common);
}
}
现在,问题是,如果我让集合视图设置图像资源,片段将全部消失。该方法已经运行了,我甚至可以像"true"一样得到上面的str.如果我像这样更改上面的代码:
@Override
public void setCollectionStatus(String str) {
Log.e("tag", str);
}
日志完美运行。我对这个问题感到困惑,希望你能帮助我。多谢。
哦,无法从子视图更新父视图,所以我们需要在回调方法中添加一个runOnUiThread
。代码如下:
@Override
public void setCollectionStatus(final String str) {
goodsDetailsActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
if ("true".equals(str)) {
collectionView
.setImageResource(R.drawable.icon_goods_detials_collct_pressed);
} else {
collectionView
.setImageResource(R.drawable.icon_goods_detials_collct_common);
}
}
});
}
相关文章:
- jQuery UI选项卡基于活动选项卡显示/隐藏图像
- 从片段 WebView JavaScript 更新活动图像视图
- 如何使图像滑块的项目符号在活动时看起来不同
- 如何在图像加载时通过预加载和显示活动指示器快速加载网页中的图像
- 传递数组列表<ImageItems>从一个活动到另一个活动.图像项包含位图和字符串
- 通过单击链接切换图像并禁用活动链接
- 在活动幻灯片上扩展图像
- 图像库中的活动拇指
- 在Internet Explorer中捕获当前活动选项卡的图像并将其传递给Flex应用程序
- Paper.js加载图像和活动层
- 如何删除引导程序转盘中的活动图像
- 如何使较大的图像活动链接后,点击其缩略图
- 在活动<灯>上更改图像
- 在下拉菜单中获取要切换为活动状态的图像
- 由图像组成的按钮的Javascript活动状态
- 如何创建具有活动区域的图像
- 将非活动图像放在顶部
- 触发图像时,我点击提交按钮,并在当前活动选项卡
- 我做了一个自行车2幻灯片.我想在滑块中的特定图像处于活动状态时触发JQuery函数
- 我怎样才能使推特bootstrap 3旋转木马中心的活动图像和显示下一个和以前的图像的一部分