3 つのタブを持つ ActionBar を使用しています。ActionBar にも項目があります。選択したタブに応じて、これらのアイテムのクリックに反応したいと思います。これどうやってするの?
私の活動からのコード:
public class CreateProjectManually extends FragmentActivity implements ActionBar.TabListener {
private static ArrayList<String> buildingList;
private static ArrayList<String> roomList;
private static ArrayList<String> deviceList;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
* sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
* keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
* to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_project_manually);
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the action bar.
final ActionBar actionBar = getActionBar();
// set the app icon as an action to go home
actionBar.setDisplayHomeAsUpEnabled(true);
//enable tabs in actionbar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have a reference to the
// Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter.
// Also specify this Activity object, which implements the TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
/**
* Actionbar
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
//TODO Speichern implementieren
Toast.makeText(getBaseContext(), "Speichern",Toast.LENGTH_LONG).show();
break;
case R.id.menu_add:
//TODO Eintrag hinzufügen implementieren
if(getItem()==0){
}
break;
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, CreateProject.class);
intent.putExtra("Uniqid","From_CreateProjectManually_Activity");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return true;
default:
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_create_project_manually, menu);
return true;
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
* sections of the app.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment building_fragment = new BuildingFragment();
Fragment room_fragment = new RoomFragment();
Fragment device_fragment = new DeviceFragment();
Bundle args = new Bundle();
switch(i){
case 0:
args.putInt(BuildingFragment.ARG_SECTION_NUMBER, i);
building_fragment.setArguments(args);
return building_fragment;
case 1:
args.putInt(RoomFragment.ARG_SECTION_NUMBER, i);
room_fragment.setArguments(args);
return room_fragment;
case 2:
args.putInt(DeviceFragment.ARG_SECTION_NUMBER, i);
device_fragment.setArguments(args);
return device_fragment;
default: return null;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.building).toUpperCase();
case 1: return getString(R.string.room).toUpperCase();
case 2: return getString(R.string.devices).toUpperCase();
}
return null;
}
}
/**
* A fragment representing building structure
*/
public static class BuildingFragment extends Fragment {
public BuildingFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listviews, container, false);
ListView listView = (ListView) view.findViewById(R.id.listviewfragment);
buildingList = new ArrayList<String>();
ListAdapter listenAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1, buildingList);
// Assign adapter to ListView
listView.setAdapter(listenAdapter);
return view;
}
}
/**
* A fragment representing room structure
*/
public static class RoomFragment extends Fragment {
public RoomFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listviews, container, false);
ListView listView = (ListView) view.findViewById(R.id.listviewfragment);
roomList = new ArrayList<String>();
ListAdapter listenAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1, roomList);
// Assign adapter to ListView
listView.setAdapter(listenAdapter);
return view;
}
}
/**
* A fragment representing device structure
*/
public static class DeviceFragment extends Fragment {
public DeviceFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listviews, container, false);
ListView listView = (ListView) view.findViewById(R.id.listviewfragment);
deviceList = new ArrayList<String>();
ListAdapter listenAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1, deviceList);
// Assign adapter to ListView
listView.setAdapter(listenAdapter);
return view;
}
}
}