我正在构建一个带有标签活动的Android应用程序。 选项卡将是用户触发长按事件时的列表 在列表中的确定项目中,上下文菜单将 打开,用户将能够选择该选项 编辑该项目。
为此,我创建了一个扩展FragmentActivity并实现ActionBar.TabListener的actitivy。
我需要做的是,当用户选择编辑项目的选项时,表单会在此列表的选项卡(片段列表)中打开,更新当前选项卡的片段。 我尝试在List活动片段中使用FragmentTransaction,但它没有用。
我尝试过的代码如下(在ListFragment中)
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(((ViewGroup) getView().getParent()).getId(),
newFragment);
transaction.addToBackStack(null);
transaction .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); //
// Commit the transaction
transaction.commit();
有人可以帮助我吗?谢谢
编辑: FragmentActivity的布局如下:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
FragmentActivity的代码如下:
public class FiltroActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Filtro", "Diário", "Semanal" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filtro_activity);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
ListFragment的代码如下:
public class ListagemFragment extends ListFragment {
public class MyListAdapter extends ArrayAdapter<Compromisso> {
Context myContext;
View v;
public MyListAdapter(Context context, int textViewResourceId,
List<Compromisso> objects) {
super(context, textViewResourceId, objects);
myContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// return super.getView(position, convertView, parent);
v = convertView;
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.listagem_item_activity,
parent, false);
TextView label = (TextView) row.findViewById(R.id.txtTitulo);
TextView label2 = (TextView) row.findViewById(R.id.txtData);
if (compromissos.size() > 0) {
label.setText(compromissos.get(position).getTitulo());
label2.setText("Data: "
+ Formatter.GetShortDate(compromissos.get(position)
.getDataInicio(), "dd/MM/yyyy HH:mm"));
}
return row;
}
}
List<Compromisso> compromissos;
MyListAdapter myListAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getListaCompromisso();
myListAdapter = new MyListAdapter(getActivity(),
R.layout.listagem_item_activity, compromissos);
setListAdapter(myListAdapter);
}
private void getListaCompromisso() {
compromissos = new ArrayList<Compromisso>();
CompromissoNegocio negocio = new CompromissoNegocio();
compromissos = negocio.getListaCompromisso(getActivity()
.getApplicationContext());
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
registerForContextMenu(getListView());
super.onActivityCreated(savedInstanceState);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = this.getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_listagem, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
int position;
Intent i;
switch (item.getItemId()) {
case R.id.delete_item:
info = (AdapterContextMenuInfo) item.getMenuInfo();
position = (int) info.position;
CompromissoNegocio compNeg = new CompromissoNegocio();
compNeg.Deletar(compromissos.get(position).getCompromissoID(),
getActivity());
compromissos.remove(position);
myListAdapter.notifyDataSetChanged();
return true;
case R.id.edit_item:
info = (AdapterContextMenuInfo) item.getMenuInfo();
position = (int) info.position;
EditActivity newFragment = new EditActivity();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(((ViewGroup) getView().getParent()).getId(),
newFragment);
transaction.addToBackStack(null);
transaction
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); //
// Commit the transaction
transaction.commit();
Log.i("haha", "haha");
return true;
default:
return super.onContextItemSelected(item);
}
}
public void onResume() {
super.onResume();
getListaCompromisso();
myListAdapter.notifyDataSetChanged();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.listagem_activity, container, false);
}
}