移动混合开发之android文件管理demo
框架采用cordova,android编译环境为android studio.系统为mac,cordova 环境搭建参考网址:http://cordova.apache.org/docs/en/5.0.0/cordova/plugins/pluginapis.html#Plugin%20APIs
1.index.html
- <!DOCTYPE html>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
- -->
- <html>
- <head>
- <!--
- Customize this policy to fit your own app's needs. For more guidance, see:
- https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
- Some notes:
- * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
- * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
- * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
- * Enable inline JS: add 'unsafe-inline' to default-src
- -->
- <meta http-equiv="Content-Security-Policy"
- content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
- <meta name="format-detection" content="telephone=no">
- <meta name="msapplication-tap-highlight" content="no">
- <meta charset="utf-8">
- <meta name="viewport"
- content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
- <link rel="stylesheet" type="text/css" href="css/index.css"><br>
- <!----引入jQuery----!>
- <!--引入滑动第三方库,让列表滑动-->
- <script type="text/javascript" src="js/jquery-2.2.4.min.js"></script>
- <script type="text/javascript" src="js/iscroll-lite.js"></script>
- <script type="text/javascript" src="cordova.js"></script>
- <script type="text/javascript" src="js/index.js"></script>
- <title>Hello World</title>
- </head>
- <body>
- <div id="log"></div>
- <div class="head">
- 浏览器
- </div>
- <!--自定义布局,没有使用第三方框架-->
- <div class="content">
- <div class="nav">
- <div id="current-dir"></div>
- <div id="upper">上一级</div>
- </div>
- <div class="list" id="file-list">
- <ul >
- <li class="t">
- <div class="file-icon"></div>
- <div class="file-name">This is file name1</div>
- </li>
- </ul>
- </div>
- </div>
- </body>
- </html>
2.index.js
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- var app = {
- // Application Constructor
- initialize: function () {
- this.bindEvents();
- },
- // Bind Event Listeners
- //
- // Bind any events that are required on startup. Common events are:
- // 'load', 'deviceready', 'offline', and 'online'.
- bindEvents: function () {
- document.addEventListener('deviceready', this.onDeviceReady, false);
- },
- // deviceready Event Handler
- //
- // The scope of 'this' is the event. In order to call the 'receivedEvent'
- // function, we must explicitly call 'app.receivedEvent(...);'
- onDeviceReady: function () {
- // app.receivedEvent('deviceready');
- $(function () {
- myDeviceReady();
- });
- },
- // Update DOM on a Received Event
- receivedEvent: function (id) {
- var parentElement = document.getElementById(id);
- var listeningElement = parentElement.querySelector('.listening');
- var receivedElement = parentElement.querySelector('.received');
- listeningElement.setAttribute('style', 'display:none;');
- receivedElement.setAttribute('style', 'display:block;');
- console.log('Received Event: ' + id);
- }
- };
- app.initialize();
- var myscroll = null;
- function myDeviceReady() {
- var size = $(window).width() / 18
- //设计字体宽度
- $('html').css('font-size', size);
- myscroll = new IScroll("#file-list");
- //打开文件夹
- window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, function (entry) {
- openEntry(entry);
- }, function () {
- alert('fail');
- });
- }
- /*<li>
- <div class="file-icon"></div>
- <div class="file-name">This is file name1</div>
- </li>*/
- function openEntry(entry) {
- //使用克隆方式时,重新更新页面不能用,$('#file-list ul ).html(");
- $('#file-list ul li.item').remove();
- $('#current-dir').text("当前目录:" + entry.name);
- $('#upper').bindtouch(
- function () {
- entry.getParent(function (entry) {
- openEntry(entry);
- }, function () {
- alert('get Parent Error');
- });
- }
- );
- entry.createReader().readEntries(function (entries) {
- sortEntrise(entries);
- for (var i = 0; i < entries.length; i++) {
- var entry = entries[i];
- //采用克隆方式,比较方便
- var jObjectLi = $('.t').clone().removeClass('t').addClass('item');
- jObjectLi.find('.file-name').text(entry.name);
- jObjectLi.data('entry', entry);
- $('.list ul').append(jObjectLi);
- jObjectLi.bindtouch(function () {
- var liEntry = $(this).data('entry');
- if (!liEntry.isFile) {
- openEntry(liEntry);
- }
- });
- }
- myscroll.refresh();
- },
- function (error) {
- alert(error);
- });
- }
- //将文件和文件夹分离
- function sortEntrise(entries) {
- entries.sort(function (a, b) {
- if (a.isFile && !b.isFile) {
- return 1;
- } else if (!a.isFile && b.isFile) {
- return -1;
- } else {
- return a.name < b.name;
- }
- })
- }
- //扩展为jQuery自定义函数
- $.fn.bindtouch = function (cb) {
- attachMyEvent($(this), cb);
- };
- var timeID = null;
- function attachMyEvent(sr, cb) {
- //click事件反应时间为300毫秒,因此取消click采用手动
- //手指按下,若手指移动,则触发取消
- sr.unbind();
- clearTimeout(timeID);
- var point_one = {};
- var point_two = {};
- sr.on('touchstart', function (event) {
- var me = $(this)
- me.data('touch', true);
- var touch = event.originalEvent.targetTouches[0];
- point_one.x = touch.pageX;
- point_one.y = touch.pageY;
- //设计timeID如果点下时滑动就不用触发
- timeID = setTimeout(function () {
- me.addClass('touchInside');
- }, 100);
- });
- sr.on('touchend', function () {
- clearTimeout(timeID);
- var me = $(this);
- if (me.data('touch') == true) {
- //改变回调函数的this指针为sr
- //触发回调函数
- cb.bind(this)();
- }
- me.removeClass('touchInside')
- me.data('touch', false);
- });
- sr.on('touchmove', function (event) {
- var me = $(this);
- var touch = event.originalEvent.targetTouches[0];
- point_two.x = touch.pageX;
- point_two.y = touch.pageY;
- if (me.data('touch')) {
- //华为手机测试,没有滑动也会触发touchmove,所以加测滑动距离,来判断是否滑动
- var distane = getPointsDistance(point_one, point_two);
- console.log(distane);
- $('#log').text(distane);
- if (distane > 4) {
- me.data('touch', false);
- clearTimeout(timeID);
- me.removeClass('touchInside')
- }
- }
- // alert(2);
- });
- }
- //计算两点之间距离
- function getPointsDistance(p1, p2) {
- var xDistance = Math.abs(p1.x - p2.x);
- var yDistance = Math.abs(p1.y - p2.y);
- var distance = Math.sqrt(xDistance * xDistance + yDistance * yDistance);
- return distance;
- }
3.index.css
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- * {
- -webkit-tap-highlight-color: rgba(0,0,0,0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */
- }
- .head{
- position:absolute;
- left:0;
- right:0;
- top:0;
- height:2rem;
- line-height: 2rem;
- background-color:orange;
- padding-left:.3rem;
- color: white;
- text-align: center;
- }
- .content{
- position:absolute;
- top:2rem;
- bottom:0;
- left:0;
- right:0
- }
- .content .nav{
- position: absolute;
- top: 0;
- height: 1.5rem;
- left: 0;
- right: 0;
- background: #efefef;
- }
- .content .list{
- position: absolute;
- top: 1.5rem;
- left: 0;
- right: 0;
- bottom: 0;
- background: #adadad;
- overflow: hidden;
- }
- .content ul{
- list-style: none;
- padding: 0;
- margin: 0;
- }
- .content ul li{
- position: relative;
- height: 4rem;
- line-height: 2rem;
- padding-left: .5rem ;
- border-bottom: 1px solid gray;
- background: white;
- }
- .content ul li.touchInside{
- background: #efefef;
- }
- .content ul li .file-icon{
- position: absolute;
- background: url("../img/file.png") no-repeat center;
- background-size: 1.2rem 1.2rem;
- width: 1.2rem;
- height: 1.2rem;
- left: 1rem;
- top:.3rem;
- }
- .content ul li .file-name{
- position: absolute;
- line-height: 2rem;
- left: 3.2rem;
- }
- .t{
- display: none;
- }
- #upper{
- position: absolute;
- right: 15px;
- top: 5px;
- color: blue;
- }
- #log{
- width: 200px;
- height: 30px;
- color: red;
- float: right;
- }
在html编写之前,先在终端下载好cordova-file插件,同时注意开起真机设备的或模拟器的 文件存储权限,具体路径为:设置-->应用管理->高级 ->应用权限->存储, 这个要千万注意,不然打不开sdk内容。
移动混合开发之android文件管理demo的更多相关文章
- 移动混合开发之android文件管理新建文件和删除文件
今天经过一天超过8小时的实践,有很多CSS上的细节需要注意: 1, /*注意是对before的操作*/ .content ul li .icon-check-empty:before{ display ...
- 移动混合开发之android文件管理-->flexbox,webFont。
增加操作栏,使用felxbox居中,felx相关参考网址:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 使用webFont添加图标, ...
- Android混合开发之WebView与Javascript交互
前言: 最近公司的App为了加快开发效率选择了一部分功能采用H5开发,从目前市面的大部分App来讲,大致分成Native App.Web App.Hybrid App三种方式,个人觉得目前以Hybri ...
- Android混合开发之WebViewJavascriptBridge实现JS与java安全交互
前言: 为了加快开发效率,目前公司一些功能使用H5开发,这里难免会用到Js与Java函数互相调用的问题,这个Android是提供了原生支持的,不过存在安全隐患,今天我们来学习一种安全方式来满足Js与j ...
- Android混合开发之WebView使用总结
前言: 今天修改项目中一个有关WebView使用的bug,激起了我总结WebView的动机,今天抽空做个总结. 混合开发相关博客: Android混合开发之WebView使用总结 Android混合开 ...
- 基于xmpp openfire smack开发之Android客户端开发[3]
在上两篇文章中,我们依次介绍openfire部署以及smack常用API的使用,这一节中我们着力介绍如何基于asmack开发一个Android的客户端,本篇的重点在实践,讲解和原理环节,大家可以参考前 ...
- Android开发之AsyncTask示例Demo
今天做了一个AsyncTask的小Demo,内含注释,通过此Demo,可以对AsyncTask有一个详细的了解 已经将项目上传到了GitHub上(程序有一个小bug,在第一次提交有说明,有解决方法请留 ...
- 混合开发之DSBridge(同时支持Android和iOS)
什么是 Javascript bridge 随着h5的不断普及及优化,以及移动端对动态化的需求越来越大,开发者经常需要在app中嵌入一些网页,然后会在web和native之间进行交互,如传递数据,调用 ...
- Android 开发之Android 应用程序如何调用支付宝接口
1.到支付宝官网,下载支付宝集成开发包 由于android设备一般用的都是无线支付,所以我们申请的就是支付宝无线快捷支付接口.下面是申请的地址以及下载接口开发包的网址:https://b.alipay ...
随机推荐
- 推荐两本学习linux的经典书籍
- Rdseed与SAC的安装
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- iOS —— 字典遍历排序
字典NSDictionary一般的遍历方法都是: NSArray* arr = [yourdictonary allKeys]; for(NSString* str in arr) { NSLog(& ...
- TortoiseGit编辑全局变量支持https
在windows,右键,进入tortoisegit的设置窗口,在左边树形菜单选Git,然后店"编辑全局.gid/config"按钮 输入以下文字 [http] sslVerify ...
- java 简单数组元素的增删改查
public class Test { static int[] a = new int[20]; static int n; public static void main(String[] arg ...
- WinForm 公共控件
一.窗体属性: 1.AcceptButton - 窗体的“接受”按钮.如果设置该属性,每次用户按“Enter”键都相当于“单击”了该按钮. 需要设置哪个键,就在后面选择. 2.CancelButton ...
- Windows 10系统更换Windows 7系统磁盘分区注意事项一
新买的电脑预装系统是WIN10,考虑到兼容性问题,打算更换为WIN7,但在新机上不能直接装WIN7系统,需要在BIOS启动中做一点小改动. 原因分析:由于Windows 8采用的是UEFI引导和GPT ...
- category - junit用例分组执行
一.category 和 testSuite的比较 (1)testSuite是类级分组(xx.class) (2)category是用例级分组(@Test) (3)category是testSuite ...
- github上面建立分支
首先是有一个github的账户,然后随便开个项目. 好了,现在把git命令行打开,输入下面几行代码. git clone https://github.com/user/repository.git ...
- 1476. Lunar Code
http://acm.timus.ru/problem.aspx?space=1&num=1476 由于前一列对后一列有影响,所以需要保持前一列的状态, 但无需用状态压缩来保存(也保存不了) ...