在Android 4中,点击带有标签的ActionBar中的项目
我正在使用一个带有三个选项卡的操作栏。我在ActionBar中还有一个项目。我想根据所选选项卡的不同,在单击这些项目时做出不同的反应。我该怎么做 “我的活动”中的代码:在Android 4中,点击带有标签的ActionBar中的项目,android,tabs,android-actionbar,Android,Tabs,Android Actionbar,我正在使用一个带有三个选项卡的操作栏。我在ActionBar中还有一个项目。我想根据所选选项卡的不同,在单击这些项目时做出不同的反应。我该怎么做 “我的活动”中的代码: public class CreateProjectManually extends FragmentActivity implements ActionBar.TabListener { private static ArrayList<String> buildingList; private
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;
}
}
}
公共类CreateProjectManually扩展FragmentActivity实现ActionBar.TabListener{
私有静态ArrayList buildingList;
私有静态ArrayList roomList;
私有静态ArrayList deviceList;
/**
*{@link android.support.v4.view.PagerAdapter},它将为每个
*我们使用{@link android.support.v4.app.FragmentPagerAdapter}派生,它将
*将每个加载的片段都保存在内存中。如果这变得太占用内存,可能是最好的
*切换到{@link android.support.v4.app.FragmentStatePagerAdapter}。
*/
分段SPAGERADAPTER mSectionsPagerAdapter;
/**
*将承载节内容的{@link ViewPager}。
*/
ViewPager mViewPager;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u手动创建\u项目);
//创建适配器,该适配器将为三个主要部分中的每个部分返回一个片段
//应用程序的名称。
mSectionsPagerAdapter=newsectionspageradapter(getSupportFragmentManager());
//设置操作栏。
最终ActionBar ActionBar=getActionBar();
//将应用程序图标设置为“回家”操作
actionBar.setDisplayHomeAsUpEnabled(true);
//在actionbar中启用选项卡
actionBar.setNavigationMode(actionBar.NAVIGATION\u MODE\u选项卡);
//使用分区适配器设置ViewPager。
mViewPager=(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
//在不同区段之间滑动时,选择相应的选项卡。
//如果有对
//标签。
mViewPager.setOnPageChangeListener(新的ViewPager.SimpleOnPageChangeListener(){
@凌驾
已选择页面上的公共无效(内部位置){
actionBar.setSelectedNavigationItem(位置);
}
});
//对于应用程序中的每个部分,在操作栏中添加一个选项卡。
对于(int i=0;i@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}