我创建了一个FragmentActivity,用Eclipse显示带有Tabs的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我想上传第二个标签片段..是否可能?
答案 0 :(得分:0)
您可以使用片段管理器跟踪正在添加到TabPagerAdapter的片段。
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager(), context);
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
if(position == 1)
{
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
**MYACTIVITY fragment = (MYACTIVITY) fragmentManager.findFragmentByTag("YOUR TAG 1");**
fragment.get_recent();
Toast.makeText(context, "im visible", Toast.LENGTH_LONG).show();
}
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("TAB NAME 1").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("TAB NAME 2").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("TAB NAME 3").setTabListener(tabListener));
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(TabAdapter.getItem(0), "YOUR TAG 1").commit();
fragmentManager.beginTransaction().add(TabAdapter.getItem(1), "YOUR TAG 2").commit();
fragmentManager.beginTransaction().add(TabAdapter.getItem(2), "YOUR TAG 3").commit();
}
public class TabPagerAdapter extends FragmentStatePagerAdapter {
private Context context;
public TabPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new YOURACTIVITY1();
case 1:
return new YOURACTIVITY2();
case 2:
return new YOURACTIVITY3();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3; //No of Tabs
}
}
如果您正在使用ActionBarSherlock,请小心,因为您需要明确声明使用android.support.v4.app.FragmentManager。