我创建了一个带有 Eclipse 的 FragmentActivity,它显示了一个带有选项卡的 ActionBar。在第一个选项卡中,我显示了一个带有列表的片段,在下一个选项卡中显示了一个带有文本的片段。然后我希望,当我选择第一个选项卡的一个项目时,将第二个片段更改为另一个列表片段。这是我的代码:
Eclipse 和我创建的 TabActivity:
public class TabActivity extends FragmentActivity implements ActionBar.TabListener {
static final int NUM_TABS = MyTab.values().length;
public enum MyTab{
Categorias(0), Productos(1);
private final int index;
MyTab(int i) {
this.index = i;
}
}
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedido);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// 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 (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.pedido, menu);
return true;
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(position);
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private FragmentManager mFragmentManager;
private TextoFragment mTextoFragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
}
@Override
public Fragment getItem(int position) {
return TabFragment.nuevoFragment(position);
}
@Override
public int getCount() {
return NUM_TABS;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (MyTab.values()[position]) {
case Categorias:
return getString(R.string.title_categorias).toUpperCase(l);
case Productos:
return getString(R.string.title_productos).toUpperCase(l);
}
return "";
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class TabFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
static Fragment nuevoFragment(int position) {
Fragment f;
Bundle args = new Bundle();
switch (Pestaña.values()[position]) {
case Categorias:
f = new ListaFragment();
args.putInt("tipoObjeto", TipoObjeto.Categoria.ordinal());
break;
case Productos:
if (EstadoHT.categoriaActual == null) {
f = new TextoFragment();
args.putString(TextoFragment.ARG_MENSAJE,
"Elija primero una categoría");
} else {
f = new ListaFragment();
args.putInt(ListaFragment.ARG_TIPO_OBJETO,
TipoObjeto.Producto.ordinal());
}
break;
default:
f = new TabFragment();
args = new Bundle();
args.putInt(TabFragment.ARG_SECTION_NUMBER, position);
}
f.setArguments(args);
return f;
}
private View lanzarEstadoPedido(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_estado_pedido,
container, false); // Muestra el layout
TextView lblMesa = (TextView) rootView.findViewById(R.id.lblMesa);
lblMesa.setText("Nada");
return rootView;
}
public TabFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int valorPestaña = getArguments().getInt(ARG_SECTION_NUMBER, 0);
switch (MyTab.values()[valorPestaña]) {
case Pedido:
return lanzarEstadoPedido(inflater, container,
savedInstanceState);
default:
return null;
}
}
}
}
在 ListaFragment 中:
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Intent dato = new Intent();
IObjetoHT o = (IObjetoHT) lvlista.getItemAtPosition(position);
switch (tipo) {
case Categoria:
App.categoriaActual = (Categoria) o;
TabActivity actPedido = (TabActivity) getActivity();
ActionBar actionBar = actPedido.getActionBar();
actPedido.mViewPager.setCurrentItem(TabActivity.MyTab.Productos.ordinal());
break;
case Producto:
break;
default:
dato.putExtra(ARG_OBJETO_A_DEVOLVER, o);
act.setResult(act.RESULT_OK, dato);
act.finish();
}
然后从 ListaFragment 我想上传第二个标签片段..有可能吗?