SlidingPaneLayout的基本使用


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有所瞭解了。

效果圖就不貼了,大家一目十行隨意看看就行了,知道這麼個東西就可以了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章