我正在创建一个简单的应用程序管理器来训练自己开发Android应用程序。 我希望我的操作栏(actionbarcompat)有下拉导航+标题+副标题,我设法通过创建自定义适配器来实现,但我不知道如何让我的操作栏使用该微调器适配器。所以我做了一个简单的研究,我终于设法通过“setListNavigationCallbacks”实现我的目标,将我的适配器作为第一个参数传递,并将“OnNavigationListener”作为第二个参数。 但我有两个问题:
我不熟悉听众及其工作方式,我想知道是否可以将适配器设置为我的操作栏(getSupportActionBar)并稍后捕获回调?
即使这段代码有效,每次切换布局时都会崩溃(即当我从纵向切换到横向,反之亦然)时,我知道问题出在setListNavigationCallbacks方法,因为当我删除它时在纵向或横向中运行没有问题,但我的样式没有得到应用。
这是我的代码(我使用片段来显示已安装的应用程序):
MainActivity.java
public class MainActivity extends ActionBarActivity {
private FragmentTransaction fragmentTransaction = getSupportFragmentManager ( ).beginTransaction ( );
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
getSupportActionBar ( ).setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar ( ).setTitle("");
if ( findViewById ( R.id.activity_application_manager_framelayout ) != null && savedInstanceState == null ) {
ApplicationsListFragment applicationsListFragment = new ApplicationsListFragment ( );
applicationsListFragment.setArguments ( getIntent ( ).getExtras ( ) );
fragmentTransaction.add ( R.id.activity_application_manager_framelayout , applicationsListFragment ).commit ( );
}
}
@Override
public boolean onCreateOptionsMenu ( Menu menu ) {
getMenuInflater ( ).inflate ( R.menu.main , menu );
return true;
}
}
ApplicationListFragment.java
public class ApplicationsListFragment extends ListFragment {
private ListView listView;
private ApplicationManager applicationManager;
private ActionBar actionBar;
private CustomAdapter customAdapter;
private CustomAdapter.ActionBarDropDownCustomAdapter actionBarDropDownCustomAdapter;
private CustomAdapter.ApplicationsListCustomAdapter applicationsListCustomAdapter;
private List < ApplicationInfo > applications = new ArrayList < ApplicationInfo > ( );
private List < String > actionBarDropDownItems = new ArrayList < String > ( );
@Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
View view = inflater.inflate ( R.layout.fragment_applications_list , container , false );
this.listView = ( ListView ) view.findViewById ( android.R.id.list );
this.actionBarDropDownItems.add ( "All Apps" );
this.actionBarDropDownItems.add ( "User Apps" );
this.actionBarDropDownItems.add ( "System Apps" );
customAdapter = new CustomAdapter ( );
actionBarDropDownCustomAdapter = customAdapter.new ActionBarDropDownCustomAdapter ( getActivity ( ) , R.layout.spinner_activity_main_actionbar , R.layout.list_item_activity_main_actionbar , this.actionBarDropDownItems );
actionBar.setListNavigationCallbacks ( actionBarDropDownCustomAdapter , new OnNavigationListener( ) {
@Override
public boolean onNavigationItemSelected ( int arg0 , long arg1 ) {
if ( actionBarDropDownItems.get ( arg0 ).equals ( actionBarDropDownItems.get ( 0 ) ) ) {
refreshDisplay ( 0 );
} else if ( actionBarDropDownItems.get ( arg0 ).equals ( actionBarDropDownItems.get ( 1 ) ) ) {
refreshDisplay ( 1 );
} else if ( actionBarDropDownItems.get ( arg0 ).equals ( actionBarDropDownItems.get ( 2 ) ) ) {
refreshDisplay ( 2 );
}
return false;
}
} );
return view;
}
@Override
public void onActivityCreated ( Bundle savedInstanceState ) {
super.onActivityCreated ( savedInstanceState );
this.applicationManager = new ApplicationManager ( getActivity() );
refreshDisplay ( 0 );
}
@Override
public void onAttach ( Activity activity ) {
super.onAttach ( activity );
actionBar = ( ( ActionBarActivity ) activity ).getSupportActionBar ( );
}
public boolean refreshDisplay ( int flag ) {
if ( flag == 0 ) {
this.applications = this.applicationManager.getAllAppInfos ( );
this.applicationsListCustomAdapter = customAdapter.new ApplicationsListCustomAdapter ( getActivity() , R.layout.list_item_fragment_applications_list , this.applications );
this.listView.setAdapter ( applicationsListCustomAdapter );
return true;
} else if ( flag == 1 ) {
this.applications = this.applicationManager.getAllUserAppInfos ( );
this.applicationsListCustomAdapter = customAdapter.new ApplicationsListCustomAdapter ( getActivity() , R.layout.list_item_fragment_applications_list , this.applications );
this.listView.setAdapter ( applicationsListCustomAdapter );
return true;
} else if ( flag == 2 ) {
this.applications = this.applicationManager.getAllSystemAppInfos ( );
this.applicationsListCustomAdapter = customAdapter.new ApplicationsListCustomAdapter ( getActivity() , R.layout.list_item_fragment_applications_list , this.applications );
this.listView.setAdapter ( applicationsListCustomAdapter );
return true;
} else {
return false;
}
}
}
答案 0 :(得分:0)
我找到了解决这两个问题的方法,所以我想我和你们分享了这些问题:
1 - 在类中实现“ActionBar.OnNavigationListener”,然后覆盖类中的“onNavigationItemSelected”方法,最后将类作为“setListNavigationCallbacks”的第二个参数传入(使用“this”关键字)当然)
2 - 你必须覆盖你的活动的“onSaveInstanceState”方法(而不是片段)并在“savedInstanceState”中放入一个整数(getSupportActionBar()。getSelectedNavigationIndex()),你覆盖你的“onRestoreInstanceState”方法activity并将“getSupportActionBar()。setSelectedNavigationItem()”设置为您在“onSaveInstanceState”中保存的值。