Android中内置了WebKit模块,而该模块的Java层视图类就是WebView,所有需要使用Web浏览器功能的Android都需要创建该视图类对象显示和处理请求的网络资源。目前WebKit支持Http、Https、Ftp和JavaScript请求。下面是在Android中调用JavaScript方法以及如何在js中调用本地方法。
1、在Assets下放一个简单的html文件jstest.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html40/strict.dtd">
<HTML>
<HEAD>
<meta name="viewport" content="width=device-width, target-densitydpi=device-dpi" />
<META http-equiv="Content-Type" content="text/htmlcharset=UTF-8">
<script>
function showMsg(){
alert("hello world!")
}
function showMsgInAndroid(){
myjs.showMsg('hello in android!')
}
</script>
</HEAD>
<BODY>
<span>测试js使用</span>
<button id='btntest' onclick='showMsgInAndroid()'>调用android方法</button>
</BODY>
</HTML>
2、布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/rl_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<WebView
android:id="@+id/wv_test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/btn_showmsg"/>
<Button
android:id="@+id/btn_showmsg"
android:layout_width="200dip"
android:layout_height="40dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="调用html中js方法"/>
</RelativeLayout>
3、然后是Activity,MainActivity.java
package com.harold.jstest
import com.harold.base.JSKit
import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.view.View.OnClickListener
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.widget.Button
public class MainActivity extends Activity {
private WebView mWebView
private Button btnShowInfo
private JSKit js
private Handler mHandler = new Handler()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
//初始化控件
mWebView = (WebView) findViewById(R.id.wv_test)
btnShowInfo = (Button) findViewById(R.id.btn_showmsg)
//实例化js对象
js = new JSKit(this)
//设置参数
mWebView.getSettings().setBuiltInZoomControls(true)
//内容的渲染需要webviewChromClient去实现,设置webviewChromClient基类,解决js中alert不弹出的问题和其他内容渲染问题
mWebView.setWebChromeClient(new WebChromeClient())
mWebView.getSettings().setJavaScriptEnabled(true)
//把js绑定到全局的myjs上,myjs的作用域是全局的,初始化后可随处使用
mWebView.addJavascriptInterface(js, "myjs")
mWebView.loadUrl("file:///android_asset/jstest.html")
btnShowInfo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mHandler.post(new Runnable() {
@Override
public void run() {
//调用 HTML 中的javaScript 函数
mWebView.loadUrl("javascript:showMsg()")
}
})
}
})
}
}
4、最后是绑定全局js的类JSKit.java
package com.harold.base
import android.widget.Toast
import com.harold.jstest.MainActivity
public class JSKit {
private MainActivity ma
public JSKit(MainActivity context) {
this.ma = context
}
public void showMsg(String msg) {
Toast.makeText(ma, msg, Toast.LENGTH_SHORT).show()
}
}
例子比较简单,代码里都加了注释,这里就不多说了,本示例用的本地的html,如果访问网络中的网页,别忘记在AndroidManifest.xml中加权限
<uses-permission android:name="android.permission.INTERNET"/>
项目中常用的吸顶效果,如果页面没有固定定位的元素是比较容易实现的,但是如果页面结构比较复杂会有兼容性问题
常见兼容性问题:吸顶元素无法固定,会随着页面滚动抖动
解决方案:页面整体为弹性布局,中间加载部分自适应高度,总体结构为 顶部固定+内容+底部固定
注意整体弹性布局的时候如果要实现吸顶效果,必须将定位元素放到flex=1元素的外层,吸顶的元素需要用两个div,一个是正常显示的,一个是滚动到一定高度固定到顶部的
html:
<div class="wrap" id="wrapId">
<div class="isFixed" v-if="is_fixed">
<div class="topBar" id="fixedTopFixed" ref="topBar">
<div class="item" v-for="(item,index) in barList" >吸顶内容</div>
</div>
</div>
<div class="flexWrap" :class="is_fixed? 'wrapTop' : 'flex'">
<div class="myScroll" v-infinite-scroll="loadMore" infinite-scroll-throttle-delay="500" infinite-scroll-immediate-check="true" infinite-scroll-disabled="busy" infinite-scroll-distance="50">
<div class="flexContent">
<div class="top" ref="top">
<div class="banner"><img src="../../../assets/images/文件名/banner.jpg" alt="" srcset=""></div>
<div class="topBar" id="fixedTop" ref="topBar" v-show="!is_fixed">
<div class="item" v-for="(item,index) in barList" @click="tab(index,item)">
不吸顶时展示的内容
</div>
</div>
</div>
<div class="scrollContent" id="wrap-content" ref="contentH">
<div class="memberList" v-show="infoList.length>0" id="content">
<div class="myScroll" v-infinite-scroll="loadMore" infinite-scroll-throttle-delay="500" infinite-scroll-immediate-check="true" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
<div class="memberItem" v-for="(item,index) in infoList">
加载内容
</div>
<div class="loading" v-if="loading">
<span id="load-text">{{loadText}}</span>
</div>
</div>
</div>
<div class="empty" v-show="noData">最新达成情况正在更新中...请稍后再来~</div>
</div>
</div>
</div>
</div>
<div class="footer">底部固定</div>
</div>
js:
data: {
return {
busy: false,
pageNum: 1,
pageSize: 10,
loading: false,
noData: false,
infoList: []
}
}
mounted() {
<!--监听滚动-->
that.$nextTick(() =>{
let scrollDOM = document.querySelector('.flexContent')
scrollDOM.addEventListener('scroll',that.handleScroll)
})
},
methods: {
handleScroll () {
let scrollDOM = document.querySelector('.flexContent')
let scrollTop = scrollDOM.scrollTop
<!--计算滚动高度-->
let clientHeight = document.documentElement.clientHeight
if (scrollTop >document.querySelector('.banner').offsetHeight) {
this.is_fixed = true
} else {
this.is_fixed = false
}
},
<!--//触发加载-->
loadMore() {
if(this.pageNum<this.pages) {
this.loading = true
this.pageNum+=1
this.busy = true
this.loadData(接口参数,this.pageNum)
}
},
//加载时触发的接口调用
loadData() {
api.XXX({},function(success,data,err){
if(success) {
if(data.status==200) {
//加载逻辑判断
if(data.body.list.length>0 &&pageNum>0){
that.infoList = that.infoList.concat(data.body.list)
that.loading = false
}
if(pageNum==data.body.pages || data.body.list<20) {
that.loading = true
that.loadText = '没有更多数据了'
}
if(pageNum==1 &&data.body.list.length==0) {
that.noData = true
that.infoList = []
}
}
}
})
}
}
css:
.isFixed {
width: 100%
height: 1rem
position: absolute
top: 0
left: 0
z-index: 100
}
.myScroll {
height: 100%
}
.wrap {
width: 7.5rem
margin: auto
height: 100%
overflow: hidden
display: flex
flex-direction: column
}
.flex {
flex: 1
}
.wrapTop {
padding-top:0.45rem
}
.flexWrap {
width: 100%
height: 100%
}
.flexContent {
width: 100%
height: 100%
display: flex
flex-direction: column
overflow: scroll
-webkit-overflow-scrolling: touch
}
.top {
.banner {
position: relative
img {
width: 100%
display: block
}
.month {
position: absolute
bottom: 0.33rem
left: 0.45rem
color: #fff
font-size: 0.45rem
}
}
}
.scrollContent {
width: 100%
background: #fff
flex: 1
-webkit-overflow-scrolling: touch
.memberList {
height: 100%
padding-top: 0.2rem
}
}
.footer {
position: fixed
bottom: 0
width:7.5rem
margin: auto
height:auto
background:rgba(0,0,0,.7)
}