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;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(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() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
lis.getChoice(arg2);
}
});
}
ViewGroup parent=(ViewGroup) view.getParent();
if(parent!=null) {
parent.removeView(view);
}
return view;
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
if(!(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;
};
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
str=getArguments().getString("params");
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slidingpanellayout=(SlidingPaneLayout) findViewById(R.id.slidingpanellayout);
changeFragment(0);
}
@Override
public 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;
}
@Override
public 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);
}
@Override
public void getChoice(int position) {
// TODO Auto-generated method stub
changeTextView(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有所了解了。
效果图就不贴了,大家一目十行随意看看就行了,知道这么个东西就可以了