//主布局 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的更多相关文章

  1. android DrawerLayout 侧边栏实现

    现在实现侧边栏比较简单了,官方提供的DrawerLayout可以很方便实现. 主要实现方法是:用DrawerLayout 作为界面根控件.在DrawerLayout里面第一个View为当前界面主内容: ...

  2. Android中禁止SlidingPaneLayout的侧滑功能

    Android中使用android.support.v4.widget.SlidingPaneLayout实现侧滑功能的时候,可能出现滑动屏幕时与SlidingPaneLayout的侧滑发生冲突,查看 ...

  3. Android App 侧边栏菜单的简单实现

    效果图 Layout 注意事项 想要实现侧边栏,需要配合使用DrawerLayout.因为会用到嵌套布局,所以根布局不能是 ConstraintLayout,最好使用 LinearLayout 布局. ...

  4. 利用SlidingPaneLayout实现侧滑

    利用SlidingPaneLayout实验仿QQ侧滑效果 1.效果图            2.布局文件 <?xml version="1.0" encoding=" ...

  5. android侧滑菜单笔记

    一.SlidingPaneLayout v4包下的控件,使用简单,功能简洁.官方文档明确说明该控件只能左侧滑动.使用如下: <android.support.v4.widget.SlidingP ...

  6. 我的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 ...

  7. React Native项目组织结构介绍

    代码组织: 目录结构: . ├── components //组成应用的各个组件 │   ├── Routers.android.js //每个组件若实现不一样,分为android的实现和ios的实现 ...

  8. Android UI开发第三十四篇——SlidingPaneLayout

    SlidingPaneLayout也是系统支持的高级控件,是Android团对在2013 google IO大会期间更新的Support库(Version 13)中新加入的重要的功能.它支持左右滑动菜 ...

  9. Android 侧边栏(使用Support Library 4提供的扩展组件)

    本文转自:http://www.apkbus.com/android-117148-1-1.html 写在前面的话:接触Android已经有一段时间了,自己积累的东西也算蛮多的.总结以往的经验,凡是关 ...

随机推荐

  1. 学习Cassandra的开源电子书(中英文版)

    学习Cassandra的开源电子书(中英文版)发布啦:http://teddymaef.github.io/learncassandra/ 之前发布了英文版,现在包含中文版了. 学习Cassandra ...

  2. 在JavaScript中,利用三元运算符生成当前日期yyyy-MM-dd

    <script type="text/javascript"> //得到当前时间yyyy-MM-dd var myDate = new Date(); var nowD ...

  3. spring4mvc返回json(bean,list,map)

    因为spring3和spring4的mvc在前端返回json所需要的jar包不一样,所以索性写一篇关于spring4mvc在前端返回json的博文. 首先,新建一个web项目,项目格式如图所示: co ...

  4. uwp 图片切换动画 使用帧动画

    上一篇博客使用了Timer来实现图片的切换,@lindexi_gd讨论了一下性能,我本人其实对性能这一方面不太熟,但我觉得还是有必要考虑一下,那么今天我们使用帧动画开实现以下 新建项目,添加一个But ...

  5. 开源地图SharpMap源码解析-(1)简介

    1.简介 SharpMap最新版基于.NET Framework 4,采用C#开发的地图渲染引擎,非常易于使用.我这次研究的是比较稳定发布的V1.1版本.可以在GitHub下载该源码,地址:https ...

  6. SQL Server中In-Flight日志究竟是多少

        在SQL Server中,利用日志的WAL来保证关系数据库的持久性,但由于硬盘的特性,不可能使得每生成一条日志,就直接向磁盘写一次,因此日志会被缓存起来,到一定数据量才会写入磁盘.这部分已经生 ...

  7. 实战MEF(4):搜索范围

    在前面的文章中,几乎每个示例我们都会接触到扩展类的搜索位置,我们也不妨想一下,既然是自动扩展,它肯定会有一个或者多人可供查找的位置,不然MEF框架怎么知道哪里有扩展组件呢? 就像我们用导航系统去查找某 ...

  8. javascript运动系列第四篇——抖动

    × 目录 [1]原理介绍 [2]代码实现 [3]实例应用 前面的话 在运动系列中,前面分别介绍了匀速运动.变速运动和曲线运动.下面介绍一种特殊的运动形式——抖动 原理介绍 抖动其实是往复运动的一种特殊 ...

  9. Rehosting the Workflow Designer

    官方文档:https://msdn.microsoft.com/en-us/library/dd489451(v=vs.110).aspx The Windows Workflow Designer ...

  10. Java学习笔记——回调函数

    转载:http://wangyang0311.iteye.com/blog/368031 一般来说分为以下几步: 声明回调函数的统一接口interface A,包含方法callback(); 在调用类 ...