//主布局 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侧边栏的全部代码,测试成功的图片由于我是真机调试,就不贴了...