SlidingPaneLayout的基本使用

来源:互联网 发布:北京科技大学网络 编辑:程序博客网 时间:2024/05/22 14:56

SlidingPaneLayout是谷歌为了适应侧滑菜单,专门在v4包种加入的一个新布局。

一般我们采用第三方SlidingMenu作为首选,因为这个库可以左右分别侧滑,官方提供得这个行不行,我还没有研究过

首先我们看下代码,我是用2个fragment去分别做菜单栏跟主页面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"><android.support.v4.widget.SlidingPaneLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/slidingpanellayout">    <fragment         android:name="com.renyu.slidingpanelayoutdemo.ListFragment"        android:id="@+id/main_list"    android:layout_width="100dip"    android:layout_height="match_parent"/>    <FrameLayout         android:id="@+id/main_content"    android:layout_width="match_parent"    android:layout_height="match_parent"/></android.support.v4.widget.SlidingPaneLayout></RelativeLayout>

左边得菜单栏标明侧边滑动100dip的宽度

2个fragment我写的相当简单,菜单栏就是一个listview,主页面就是一个textview

<?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/fragment_list_list"    android:layout_width="match_parent"    android:layout_height="match_parent">            </ListView></LinearLayout>

<?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/fragment_main_text"    android:layout_width="match_parent"    android:layout_height="match_parent"        /></LinearLayout>

在菜单栏fragment,我写了一个接口,便于切换页签使用

package com.renyu.slidingpanelayoutdemo;import java.util.ArrayList;import android.app.Activity;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.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;public class ListFragment extends Fragment {View view=null;ListView fragment_list_list=null;ArrayAdapter<String> adapter=null;OnListChoiceListener lis=null;@Overridepublic View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {// TODO Auto-generated method stubif(view==null) {ArrayList<String> strArray=new ArrayList<String>();strArray.add("百度");strArray.add("阿里");strArray.add("腾讯");view=inflater.inflate(R.layout.list_fragment, container, false);fragment_list_list=(ListView) view.findViewById(R.id.fragment_list_list);adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, strArray);fragment_list_list.setAdapter(adapter);fragment_list_list.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stublis.getChoice(arg2);}});}ViewGroup parent=(ViewGroup) view.getParent();if(parent!=null) {parent.removeView(view);}return view;}@Overridepublic void onAttach(Activity activity) {// TODO Auto-generated method stubif(!(activity instanceof OnListChoiceListener)) {throw new ClassCastException();}lis=(OnListChoiceListener) activity;super.onAttach(activity);}public interface OnListChoiceListener {public void getChoice(int position);}}

主页面也很简单,就是将不同页签区分显示一下

package com.renyu.slidingpanelayoutdemo;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.TextView;public class MainFragment extends Fragment {View view=null;TextView fragment_main_text=null;String str="";public static MainFragment getInstance(String str) {MainFragment fragment=new MainFragment();Bundle bundle=new Bundle();bundle.putString("params", str);fragment.setArguments(bundle);return fragment;};@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);str=getArguments().getString("params");}@Overridepublic View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {// TODO Auto-generated method stubif(view==null) {view=inflater.inflate(R.layout.main_fragment, container, false);fragment_main_text=(TextView) view.findViewById(R.id.fragment_main_text);fragment_main_text.setText(str);}ViewGroup parent=(ViewGroup) view.getParent();if(parent!=null) {parent.removeView(view);}return view;}}

最后一个是主页面,这个页面是通过菜单栏的选择,去显示相应得fragment到主页面上,这里主要是用的show/hide去显示一个fragment,就不需要每次去新建fragment了

package com.renyu.slidingpanelayoutdemo;import com.renyu.slidingpanelayoutdemo.ListFragment.OnListChoiceListener;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v4.widget.SlidingPaneLayout;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends FragmentActivity implements OnListChoiceListener {SlidingPaneLayout slidingpanellayout=null;Fragment mCurFragment=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);slidingpanellayout=(SlidingPaneLayout) findViewById(R.id.slidingpanellayout);changeFragment(0);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}@Overridepublic void getChoice(int position) {// TODO Auto-generated method stubchangeTextView(position);if(slidingpanellayout.isOpen()) {slidingpanellayout.closePane();}else {slidingpanellayout.openPane();}}public void changeTextView(int position) {changeFragment(position);};private void changeFragment(int position) {FragmentManager manager=getSupportFragmentManager();FragmentTransaction tran=manager.beginTransaction();Fragment fragment=getSupportFragmentManager().findFragmentByTag(""+position);if(fragment==null) {switch(position) {case 0:fragment=MainFragment.getInstance("百度");break;case 1:fragment=MainFragment.getInstance("阿里");break;case 2:fragment=MainFragment.getInstance("腾讯");break;}}if(mCurFragment!=null&&mCurFragment!=fragment) {tran.hide(mCurFragment);}if(fragment.isAdded()) {tran.show(fragment);}else {tran.add(R.id.main_content, fragment, ""+position);}tran.commitAllowingStateLoss();mCurFragment=fragment;}}

到这里,大家应该就对SlidingPaneLayout有所了解了。

效果图就不贴了,大家一目十行随意看看就行了,知道这么个东西就可以了

0 0
原创粉丝点击