ActionBar详解(六)--->利用ActionBar结合Fragment实现下拉式导航

时间:2021-11-15 04:40:59

MainActivity如下:

package cc.testsimpleactionbar5;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.app.FragmentTransaction;
/**
* Demo描述:
* 利用ActionBar结合Fragment实现下拉式导航.
* 且根据不同的导航利用Fragment显示不同的内容
*
* 备注说明:
* 该示例和TestSimpleActionBar4示例的布局文件一致.
* 代码实现也类似,只是导航方式不同罢了
*
* 参考资料:
* 1 Android疯狂讲义(第二版)
* Thank you very much
*
*/
public class MainActivity extends Activity {
private ActionBar mActionBar;
private final static String SELECTED_PAGE_INDEX="selected_page";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initActionBar();
}

private void initActionBar(){
mActionBar=getActionBar();
//设置是否显示ActionBar的标题
mActionBar.setDisplayShowTitleEnabled(true);
//设置导航方式为LIST
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(
MainActivity.this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
new String[]{"第一页","第二页","第三页" });
//为ActionBar设置ArrayAdapter和导航监听
mActionBar.setListNavigationCallbacks(arrayAdapter,new NavigationListenerImpl());

}

private class NavigationListenerImpl implements OnNavigationListener{
@Override
public boolean onNavigationItemSelected(int position, long id) {
System.out.println(" onNavigationItemSelected ");
TestFragment testFragment=new TestFragment();
Bundle bundle=new Bundle();
bundle.putString(TestFragment.KEY, "这是第"+(position+1)+"页");
testFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.relativeLayoutContainer, testFragment);
fragmentTransaction.commit();
return false;
}

}


// 以下处理横竖屏的切换

// 将当前选中页的索引保存到Bundle中
@Override
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putInt(SELECTED_PAGE_INDEX,mActionBar.getSelectedNavigationIndex());
}

// 获取已经保存的被选中页的索引,且设置ActionBar.
// setSelectedNavigationItem()方法会调用onNavigationItemSelected()
@Override
protected void onRestoreInstanceState(Bundle bundle) {
super.onRestoreInstanceState(bundle);
mActionBar.setSelectedNavigationItem(bundle.getInt(SELECTED_PAGE_INDEX));
}


}


TestFragment如下:

package cc.testsimpleactionbar5;

import android.app.Fragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TestFragment extends Fragment {
public static final String KEY="key";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
TextView textView=new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle bundle=this.getArguments();
String content=bundle.getString(KEY);
textView.setText(content);
textView.setTextSize(30);
return textView;
}
}


main.mxl如下:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayoutContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

</RelativeLayout>