Android實現側邊欄SlidingPaneLayout

來源:互聯網
上載者:User

標籤:


//主布局 1

<?xml version="1.0" encoding="utf-8"?> 2 <android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/spl" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.yzj.android_8_2.MainActivity"> 8 9 <fragment10 android:id="@+id/fragment_left"11 android:name="com.example.yzj.android_8_2.LeftFragment"12 android:layout_width="100dp"13 android:layout_height="match_parent"/>14 15 <fragment16 android:id="@+id/fragment_right"17 android:name="com.example.yzj.android_8_2.RightFragment"18 android:layout_width="match_parent"19 android:layout_height="match_parent"/>20 21 22 </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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.