ActionBar中的下拉選單列表導航
阿新 • • 發佈:2019-02-11
主要有四個步驟
1、建立一個spinnerAdapter,tigonneirong
2、實現ActionBar.OnNavigationListener介面,處理對錶單選擇
3、建立ActionBar,並設定為下拉列表的導航模式
4、給這個下拉列表設定處理使用者選擇的回撥處理
//自定義Framgmentpackage com.example.actionbar_drop_down_list; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.ActionBar; import android.app.ActionBar.OnNavigationListener; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Build; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.SpinnerAdapter; @SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.HONEYCOMB) public class Main extends Activity{ @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SpinnerAdapter adapter=ArrayAdapter.createFromResource(this, R.array.song, android.R.layout.simple_spinner_dropdown_item); ActionBar bar=getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); bar.setListNavigationCallbacks(adapter, new Down_to_Up()); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) class Down_to_Up implements OnNavigationListener{ String[] listNames = getResources().getStringArray(R.array.song); /* 當選擇下拉選單項的時候,將Activity中的內容置換為對應的Fragment */ public boolean onNavigationItemSelected(int itemPosition, long itemId) { // 生成自定的Fragment SongInfo student = new SongInfo(); FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); // 將Activity中的內容替換成對應選擇的Fragment transaction.replace(R.id.context, student, listNames[itemPosition]); transaction.commit(); return true; } } }
package com.example.actionbar_drop_down_list; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.app.Fragment; import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @SuppressLint("NewApi") public class SongInfo extends Fragment{ private String tag; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") public void onAttach(Activity activity) { super.onAttach(activity); tag=getTag(); } @SuppressLint("NewApi") @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView tv=new TextView(getActivity()); tv.setText(tag); return tv; } }
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".Main" > <LinearLayout android:id="@+id/context" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"></LinearLayout> </RelativeLayout>
//定義一個數組
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ActionBar_Drop_Down_List</string>
<string name="action_settings">設定</string>
<string name="hello_world">Hello world!</string>
<string-array name="song">
<item > 杯中影 </item>
<item >愛情一陣風 </item>
<item >浪子的心情 </item>
<item >人生的第一步 </item>
<item >初戀 </item>
<item >為你唱一首歌 </item>
<item >奮鬥的歌手 </item>
</string-array>
</resources>