我一直在尝试在操作栏中开发 ListNavigation 下拉菜单。操作栏中有 2 个下拉菜单。
现在我想为每个微调器选择显示一个唯一的 TextView。就像如果用户Fragment1
从左下拉菜单中选择并从右下拉菜单中选择“一个”,项目的项目符号列表将进入 TextView。如果用户Fragment 1
从左侧下拉列表中选择,从右侧下拉列表中选择“两个”,则另一个项目符号列表将显示在 TextView 中。
基本上我使用“左下拉”作为主菜单和“右下拉”作为子菜单来显示主要是项目符号列表的文本。
现在我能够获得 GUI,但我对如何实现这一点有点困惑。
这是主要活动的代码:
package com.temp.basicactionbar;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity
{
private MenuItem mSpinnerItem = null;
private MenuItem mSearchItem = null;
private EditText mSearch = null;
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled( false );
setListNavigation( ab );
}
@Override
protected void onSaveInstanceState( Bundle outState ) {
outState.putInt( "mode", getActionBar().getNavigationMode() );
super.onSaveInstanceState( outState );
}
@Override
public boolean onCreateOptionsMenu( Menu menu ) {
getMenuInflater().inflate( R.menu.main, menu );
mSpinnerItem = menu.findItem( R.id.menu_spinner );
setupSpinner( mSpinnerItem );
mSearchItem = menu.findItem( R.id.menu_search );
mSearchItem.setVisible(getActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS );
mSearch = (EditText) mSearchItem.getActionView().findViewById(R.id.search );
return true;
}
private void setListNavigation( ActionBar actionBar )
{
actionBar.setNavigationMode( ActionBar.NAVIGATION_MODE_LIST );
final List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put( "title", getString( R.string.frag1 ) );
map.put( "fragment",
Fragment.instantiate( this, Fragment1.class.getName() ) );
data.add( map );
map = new HashMap<String, Object>();
map.put( "title", getString( R.string.frag2 ) );
map.put( "fragment",
Fragment.instantiate( this, Fragment2.class.getName() ) );
data.add( map );
map = new HashMap<String, Object>();
map.put( "title", getString( R.string.frag3 ) );
map.put( "fragment",
Fragment.instantiate( this, Fragment3.class.getName() ) );
data.add( map );
map = new HashMap<String, Object>();
map.put( "title", getString( R.string.frag4 ) );
map.put( "fragment", Fragment.instantiate( this, Fragment4.class.getName() ) );
data.add( map );
SimpleAdapter adapter = new SimpleAdapter( this, data,
android.R.layout.simple_spinner_dropdown_item,
new String[] { "title" }, new int[] { android.R.id.text1 } );
actionBar.setListNavigationCallbacks( adapter,
new OnNavigationListener()
{
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Map<String, Object> map = data.get( itemPosition );
Object o = map.get( "fragment" );
if (o instanceof Fragment)
{
FragmentTransaction tx = getFragmentManager()
.beginTransaction();
tx.replace( android.R.id.content, (Fragment) o );
tx.commit();
}
return true;
}
} );
if (mSpinnerItem != null)
{
setupSpinner( mSpinnerItem );
}
}
private void setupSpinner( MenuItem item )
{
item.setVisible( getActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST );
View view = item.getActionView();
if (view instanceof Spinner)
{
Spinner spinner = (Spinner) view;
spinner.setAdapter( ArrayAdapter.createFromResource( this,
R.array.spinner_data,
android.R.layout.simple_spinner_dropdown_item ) );
}
}
public void delete( View v )
{
if (mSearch != null)
{
mSearch.setText( "" );
}
}
}
这是片段的代码
package com.stylingandroid.basicactionbar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment
{
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState )
{
return inflater.inflate( R.layout.frag2, container, false );
}
}
这是每个片段的 XML 布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/frag3"/>
</RelativeLayout>
如果您想要完整的项目 zip,请告诉我,我会为您提供。谢谢