SlidingPaneLayout的基本使用

其他论坛看到的,为了让更多人看到,所以选择了原创

SlidingPaneLayout是V4包中新添加的组件,可以实现两列面板的切换。说先来看看API文档的说明:

SlidingPanelLayout为在UI最上层的使用提供了一个水平的,多个面板的布局。左边的面板可以看作是一个内容列表或者是浏览,右边的面板的任务是显示详细的内容。

SlidingPaneLayout类也是直接继承于ViewGroup类,所以这个类也是当作容器类使用,在使用时通常可以和Fragement组件一起使用。下面是一个xiaoDemo,左边是网站url,右边显示网站。

首先是各个布局文件:

主布局文件actvity_main.xml:

<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/slidingpanellayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" > 

    <fragment
        android:id="@+id/leftfragment"
        android:name="com.example.android_slidingpanellayout1.BookMarkerFragment"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_gravity="left" /> 

    <fragment
        android:id="@+id/rightfragment"
        android:name="com.example.android_slidingpanellayout1.ShowFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_weight="1" /> 

</android.support.v4.widget.SlidingPaneLayout>

标签页使用的布局文件(bookmarker.xml)

<?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"
    android:orientation="vertical" > 

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/> 

</LinearLayout>

内容页使用的布局文件(show.xml)

<?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"

android:orientation="vertical" >

<WebView

android:id="@+id/webview"

android:layout_width="match_parent"

android:layout_height="match_parent" />

</LinearLayout>

标签页中listview使用的布局文件(mytext.xml):

<?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"
    android:orientation="vertical" > 

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:textAlignment="center" /> 

</LinearLayout>

标签页的Fragment(BookMarkerFragement.java)

package com.example.android_slidingpanellayout1;
 import java.util.ArrayList; import java.util.List;
 import android.app.Activity; import android.app.Fragment; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView;
 public class BookMarkerFragment extends Fragment { 

    public interface BookmarkListener {
        public void onChangeBookmark(String bookmark);
    } 

    public BookmarkListener myActionObject = null; 

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bookmarker, container, false);
        initListView(view);
        return view;
    } 

    @Override
    public void onAttach(Activity activity) { 

        if (!(activity instanceof BookmarkListener)) {
            throw new ClassCastException();
        }
        myActionObject = (BookmarkListener) activity;
        super.onAttach(activity);
    } 

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        MenuItem item1 = menu.add(1, 1, 1, "分享");
        item1.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT
                | MenuItem.SHOW_AS_ACTION_ALWAYS);
        ImageView imageView = new ImageView(
                BookMarkerFragment.this.getActivity());
        imageView.setBackgroundResource(R.drawable.share);
        imageView.setLayoutParams(new LayoutParams(50, 50));
        item1.setActionView(imageView); 

    } 

    // 初始化listview     public void initListView(View view) {
        ListView lv = (ListView) view.findViewById(R.id.listview);
        List<String> list = new ArrayList<String>();
        list.add("网易");
        list.add("腾讯");
        list.add("新浪");
        list.add("搜狐");
        ArrayAdapter adapter = new ArrayAdapter(
                BookMarkerFragment.this.getActivity(), R.layout.mytextview,
                R.id.text, list);
        lv.setAdapter(adapter); 

        lv.setOnItemClickListener(new OnItemClickListener() { 

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                switch (position) {
                case 0:
                    myActionObject.onChangeBookmark("http://www.163.com");
                    break;
                case 1:
                    myActionObject.onChangeBookmark("http://www.qq.com");
                    break;
                case 2:
                    myActionObject.onChangeBookmark("http://www.sina.com");
                    break;
                case 3:
                    myActionObject.onChangeBookmark("http://www.sohu.com");
                    break; 

                }
            } 

        }); 

    }
}

内容页面(ShowFragment.java)

package com.example.android_slidingpanellayout1;

import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView;

public class ShowFragment extends Fragment {

WebView webview=null;

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.show, container, false);

webview=(WebView) view.findViewById(R.id.webview);

return view;

}

@Override

public void onAttach(Activity activity) {

super.onAttach(activity);

}

public WebView getWebView()

{

return webview;

}

}

主界面(MainActivity.java)

package com.example.android_slidingpanellayout1;

/*

*

* 实现一个书签的小例子

*

*

*/ import com.example.android_slidingpanellayout1.BookMarkerFragment.BookmarkListener;

import android.os.Bundle; import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
import android.view.Menu; import android.view.View; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast;

public class MainActivity extends Activity implements BookmarkListener {

Fragment bookmarkerFragment;

Fragment showFragment;

SlidingPaneLayout spl = null;

ActionBar actionBar = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

actionBar = this.getActionBar();

actionBar.setDisplayShowTitleEnabled(false);

actionBar.setDisplayHomeAsUpEnabled(true);

spl = (SlidingPaneLayout) this.findViewById(R.id.slidingpanellayout);

spl.setPanelSlideListener(new PanelSlideListener() {

@Override

public void onPanelClosed(View view) {

MainActivity.this.getFragmentManager()

.findFragmentById(R.id.leftfragment)

.setHasOptionsMenu(false);

}

@Override

public void onPanelOpened(View viw) {

MainActivity.this.getFragmentManager()

.findFragmentById(R.id.leftfragment)

.setHasOptionsMenu(true);

}

@Override

public void onPanelSlide(View arg0, float arg1) {

}

});

}

@Override

public void onChangeBookmark(String bookmark) {

ShowFragment sf = (ShowFragment) MainActivity.this.getFragmentManager()

.findFragmentById(R.id.rightfragment);

WebView webView = sf.getWebView();

WebSettings settings = webView.getSettings();

settings.setJavaScriptEnabled(true);

WebViewClient client = new WebViewClient();

webView.setWebViewClient(client);

webView.loadUrl(bookmark);

}

}

SlidingPaneLayout的基本使用,布布扣,bubuko.com

时间: 2024-08-09 23:43:59

SlidingPaneLayout的基本使用的相关文章

SlidingPaneLayout

1. layout xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" too

Android-通过SlidingPaneLayout高仿微信6.2最新版手势滑动返回(一)

最近更新了微信版本到6.2,发现里面有个非常好的体验,就是在第二个页面Activity能手势向右滑动返回,在手势滑动的过程中能看到第一个页面,这种体验非常赞,这里高仿了一下.这里使用的是v4包里面的SlidingPaneLayout来手势滑动,在下一篇博文中将采用SlidingMenu来高仿,下面是SlidingPaneLayout高仿后的效果,效果还是蛮不错的. 最重要的是,每一个页面都是Activity,而非Fragment哦,使用Activity和正常的Activity一样 这里给出dem

Android ViewPager和SlidingPaneLayout的滑动事件冲突处理方法(转载)

最近在做一个项目需要用到ViewPager加载广告图,布局中需要侧滑,用了android V4包里的SlidingPaneLayout控件(该控件在旧的v4包里面没有,需要更新v4包),项目中使用的时候,发现在滑动中ViewPager和SlidingPaneLayout滑动冲突了,当手指从左向右滑动时,ViewPager的滑动事件被SlidingPaneLayout屏蔽了,只能执行SlidingPaneLayout的事件,而从右往左滑时,则正常. 国内找了一些资料,发现不是特别好,最后还是靠VP

Android实现侧边栏SlidingPaneLayout

//主布局 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://sc

淘宝(阿里百川)手机客户端开发日记第三篇 SlidingPaneLayout实现侧滑菜单

需要的三个布局文件: activity_main.xml :主窗体布局 left.xml : 左侧栏目分类布局 right.xml : 右侧内容详情 需要的组件: android.support.v4.widget.SlidingPaneLayout 布局代码 activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="ht

Android android.support.v4.widget.SlidingPaneLayout 侧滑示例

SlidingPaneLayout 用于水平滚动两个view, 第一个view是左侧边,第二个view是content view slding_pane_layout.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res

侧菜单栏的实现SlidingPaneLayout

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_heig

使用SlidingPaneLayout 实现仿微信的滑动返回

上周,公司的项目改版要求加上一个右滑返回上一个界面,于是就在网上找了一些开源库打算实现.但是在使用的时候遇见了许多的问题.试了两天用过 https://github.com/ikew0ng/SwipeBackLayout ,https://github.com/r0adkll/Slidr等库都没成功. 然后在https://www.jianshu.com/p/c0a15bdc2690 看见了使用SlidingPaneLayout 来实现的一个滑动返回案例然后就看了看发现不错于是就使用了这个. 虽

Android FoldingLayout 折叠布局 原理及实现(一)

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/44278417.本文出自:[张鸿洋的博客] 1.概述 无意中翻到的FoldingLayout的介绍的博客,以及github地址.感觉非常nice呀,于是花了点时间研究以及编写,本篇博客将带大家从最主要的原理分析,一步一步的实现我们的FoldingLayout.当然了.假设你能力过硬,能够直接下载github上的代码进行学习. 博客基本分为以下几个部分: 1.Matrix的setP