Android实现侧边栏SlidingPaneLayout
//主布局 1 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spl"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yzj.android_8_2.MainActivity"> <fragment
android:id="@+id/fragment_left"
android:name="com.example.yzj.android_8_2.LeftFragment"
android:layout_width="100dp"
android:layout_height="match_parent"/> <fragment
android:id="@+id/fragment_right"
android:name="com.example.yzj.android_8_2.RightFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </android.support.v4.widget.SlidingPaneLayout>
//左边的侧边栏布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ListView
android:id="@+id/lv"
android:entries="@array/webSite"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView> </LinearLayout>
//右边的webview布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <WebView
android:id="@+id/wv"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</LinearLayout>
//主类
package com.example.yzj.android_8_2; import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity implements LeftFragment.setWebsite{
SlidingPaneLayout spl ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
} private void init() {
spl=(SlidingPaneLayout)findViewById(R.id.spl);
spl.closePane();
changeWebsite("http://www.baidu.com");//设置初始的webview界面为baidu
}
//重写方法设置webview显示界面
@Override
public void changeWebsite(String url) {
RightFragment rf = (RightFragment) MainActivity.this.getSupportFragmentManager().findFragmentById(R.id.fragment_right);
WebView webView = rf.getView();
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
WebViewClient client = new WebViewClient();
webView.setWebViewClient(client);
webView.loadUrl(url);
}
}
package com.example.yzj.android_8_2; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView; /**
* Created by YZJ on 2016/8/2.
*/
public class LeftFragment extends Fragment{
private setWebsite website;
private ListView lv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root=inflater.inflate(R.layout.layout_left,null);
init(root);
return root;
} private void init(View root) {
lv=(ListView)root.findViewById(R.id.lv);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> paren, View view, int position, long id) {
switch(position){
case 0:
website.changeWebsite("http://www.sina.com");
break;
case 1:
website.changeWebsite("http://www.qq.com");
break;
case 2:
website.changeWebsite("http://www.163.com");
break;
case 3:
website.changeWebsite("http://www.taobao.com");
break;
}
}
});
} @Override
public void onAttach(Context context) {
super.onAttach(context);
website=(setWebsite)context;//把activity向下转型成我们定义的接口,注意这里要强转
}
//创建回调接口,来回调mainactivity
public interface setWebsite{
public void changeWebsite(String url);
}
}
package com.example.yzj.android_8_2; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView; /**
* Created by YZJ on 2016/8/2.
*/
public class RightFragment extends Fragment{
private WebView wv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root=inflater.inflate(R.layout.layout_right,null);
init(root);
return root;
} private void init(View root) {
wv=(WebView)root.findViewById(R.id.wv);
} public WebView getView(){//返回rightfragment的webview
return wv;
}
}
以上就是android侧边栏的全部代码,测试成功的图片由于我是真机调试,就不贴了...
Android实现侧边栏SlidingPaneLayout的更多相关文章
- android DrawerLayout 侧边栏实现
现在实现侧边栏比较简单了,官方提供的DrawerLayout可以很方便实现. 主要实现方法是:用DrawerLayout 作为界面根控件.在DrawerLayout里面第一个View为当前界面主内容: ...
- Android中禁止SlidingPaneLayout的侧滑功能
Android中使用android.support.v4.widget.SlidingPaneLayout实现侧滑功能的时候,可能出现滑动屏幕时与SlidingPaneLayout的侧滑发生冲突,查看 ...
- Android App 侧边栏菜单的简单实现
效果图 Layout 注意事项 想要实现侧边栏,需要配合使用DrawerLayout.因为会用到嵌套布局,所以根布局不能是 ConstraintLayout,最好使用 LinearLayout 布局. ...
- 利用SlidingPaneLayout实现侧滑
利用SlidingPaneLayout实验仿QQ侧滑效果 1.效果图 2.布局文件 <?xml version="1.0" encoding=" ...
- android侧滑菜单笔记
一.SlidingPaneLayout v4包下的控件,使用简单,功能简洁.官方文档明确说明该控件只能左侧滑动.使用如下: <android.support.v4.widget.SlidingP ...
- 我的Android进阶之旅------>解决Android Studio编译后安装apk报错:The APK file does not exist on disk
1.错误描述 今天用Android Studio编译应用后安装APK的时候,报错了,错误如下所示: The APK file build\outputs\apk\OYP_2.3.4_I2Base_64 ...
- React Native项目组织结构介绍
代码组织: 目录结构: . ├── components //组成应用的各个组件 │ ├── Routers.android.js //每个组件若实现不一样,分为android的实现和ios的实现 ...
- Android UI开发第三十四篇——SlidingPaneLayout
SlidingPaneLayout也是系统支持的高级控件,是Android团对在2013 google IO大会期间更新的Support库(Version 13)中新加入的重要的功能.它支持左右滑动菜 ...
- Android 侧边栏(使用Support Library 4提供的扩展组件)
本文转自:http://www.apkbus.com/android-117148-1-1.html 写在前面的话:接触Android已经有一段时间了,自己积累的东西也算蛮多的.总结以往的经验,凡是关 ...
随机推荐
- 模糊测试(fuzz testing)介绍(一)
模糊测试(fuzz testing)是一类安全性测试的方法.说起安全性测试,大部分人头脑中浮现出的可能是一个标准的“黑客”场景:某个不修边幅.脸色苍白的年轻人,坐在黑暗的房间中,正在熟练地使用各种工具 ...
- javascript的快速排序法
在排序方式中,快速是比较普遍使用的,因为其速度快. 因为其是不断的递归,而且是根据基准点的左右两边开始递归,直到数组只有一个值的时候才返回. 这个基准点是自己定的. 一般取中间,比较好理解. < ...
- linux创建进程fork的方法步骤
fork创建进程 函数原型如下 #include// 必须引入头文件,使用fork函数的时候,必须包含这个头文件,否则,系统找不到fork函数 pid_t fork(void); //void代表没有 ...
- AngularJs入门之表单开发
本文和大家分享的主要是前端开发中必备的AngularJs框架表单开发相关基础知识,希望对大家使用和学习AngularJs有所帮助. 1.简单的表单提交: 2.更多的表单元素: 3.初始化表单: 代码 ...
- SQL Server中使用Check约束提升性能
在SQL Server中,SQL语句的执行是依赖查询优化器生成的执行计划,而执行计划的好坏直接关乎执行性能. 在查询优化器生成执行计划过程中,需要参考元数据来尽可能生成高效的执行计划, ...
- 前端MVC框架Backbone 1.1.0源码分析系列
Backbone.js 是一个在JavaScript环境下的 模型-视图-控制器 (MVC) 框架.任何接触较大规模项目的开发人员一定会苦恼于各种琐碎的事件回调逻辑.以及金字塔般的代码.而且,在传统的 ...
- 前端MVVM框架avalon揭秘 - HTML编译器
MVVM试图更加清晰的讲用户界面(UI)开发从应用程序的业务逻辑与行为中心分离,因为,很多这样的模式的实现都需要利用声明式数据绑定来实现讲View(视图)工作从其他层分离 所以出现了一大堆自定义的声明 ...
- DOM-Document类型
Document类型 JavaScript通过Document类型表示文档.在浏览器中,document对象是HTMLDocument(继承自Document类型)的一个实例,document对象是w ...
- .Net处理Oracle中Clob类型字段总结
最近在做项目中用到Clob这个字段,Clob是存储无限长字符的Oracle字段,用的时候网上找资料找了好久,内容不是很多,大部分都不能用,当然也有可以用的,测试了不同版本,整理了一下,给大家在做项目的 ...
- ZOJ Problem Set - 1331 Perfect Cubes 判断一个double是否为整数
zju对时间要求比较高,这就要求我们不能简单地暴力求解(三个循环搞定),就要换个思路:因为在循环时,已知a,确定b,c,d,在外重两层循环中已经给定了b和c,我们就不用遍历d,我们可以利用d^3=a^ ...