在ViewPager中将一个片段replace为另一个片段
当我尝试用ViewPager
另一个Fragment
replace一个Fragment
时,我遇到了一些问题。
现在的情况
我有一个3页的ViewPager
,每一个都是一个Fragment
。 在第一页,我有一个ListView
内的ListFragment
(“FacturasFragment”)。 当我点击该列表中的一个项目时,我使用onListItemClick
方法来处理该事件。
我想要的是
- 单击列表项目时,我想用另一个
Fragment
(“DetallesFacturaFragment”,包含发票的详细信息)replaceListFragment
(包含发票清单)。 - 当我在“DetallesFacturaFragment”,然后按返回
Button
,应该返回到ListFragment
。 - 页面之间的滚动不应该改变第一个显示的
Fragment
。 这是,如果我在第一页“DetallesFacturaFragment”和滚动到第二页,当返回到第一个应该继续显示“DetallesFacturaFragment”。
码
FragmentActivity
public class TabsFacturasActivity extends SherlockFragmentActivity { private MyAdapter mAdapter; private ViewPager mPager; private PageIndicator mIndicator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_pager); mAdapter = new MyAdapter(getSupportFragmentManager()); mPager = (ViewPager)findViewById(R.id.pager); mPager.setAdapter(mAdapter); mIndicator = (TitlePageIndicator)findViewById(R.id.indicator); mIndicator.setViewPager(mPager); } private static class MyAdapter extends FragmentPagerAdapter { private String[] titles = { "VER FACTURAS", "VER CONSUMO", "INTRODUCIR LECTURA" }; private final FragmentManager mFragmentManager; private Fragment mFragmentAtPos0; private Context context; public MyAdapter(FragmentManager fragmentManager) { super(fragmentManager); mFragmentManager = fragmentManager; } @Override public CharSequence getPageTitle(int position) { return titles[position]; } @Override public Fragment getItem(int position) { switch (position) { case 0: // Fragment # 0 return new FacturasFragment(); case 1: // Fragment # 1 return new ConsumoFragment(); case 2:// Fragment # 2 return new LecturaFragment(); } return null; } @Override public int getCount() { return titles.length; } @Override public int getItemPosition(Object object) { if (object instanceof FacturasFragment && mFragmentAtPos0 instanceof DetallesFacturaFragment) return POSITION_NONE; return POSITION_UNCHANGED; } } }
ListFragment
public class FacturasFragment extends ListFragment { private ListView lista; private ArrayList<TuplaFacturaWS> facturas; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_facturas, container, false); facturas = myApplication.getFacturas(); lista = (ListView) view.findViewById(id.list); MyAdapter myAdaptador = new MyAdapter(this, facturas); setListAdapter(myAdaptador); return view; } public void onListItemClick (ListView l, View v, int position, long id) { myApplication.setFacturaActual(position); mostrarDatosFactura(); } private void mostrarDatosFactura() { final DetallesFacturaFragment fragment = new DetallesFacturaFragment(); FragmentTransaction transaction = null; transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.pager, fragment); //id of ViewPager transaction.addToBackStack(null); transaction.commit(); } private class MyAdapter extends BaseAdapter { private final FacturasFragment actividad; private final ArrayList<TuplaFacturaWS> facturas; public MyAdapter(FacturasFragment facturasActivity, ArrayList<TuplaFacturaWS> facturas) { super(); this.actividad = facturasActivity; this.facturas = facturas; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = actividad.getLayoutInflater(null); View view = inflater.inflate(R.layout.list_row, null, true); //Set data to view return view; } @Override public int getCount() { return facturas.size(); } @Override public Object getItem(int pos) { return facturas.get(pos); } @Override public long getItemId(int position) { return position; } private OnClickListener checkListener = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }; } }
分段
public class DetallesFacturaFragment extends SherlockFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_factura, container, false); //Set data to view return view; } }
目前,当我点击列表项时,白色视图出现在第一页。 我已经validation并执行了“DetallesFacturaFragment”的onCreateView
方法,但没有任何内容出现在该页面上。
第一次点击列表项时,显示白屏。 但回到列表后,我必须点击两次显示白屏的列表项。
我一直在谷歌search,看着一些很多的问题,但找不到任何人用完成的代码解决。
经过这么多的时间,我发现正确的解决scheme修改代码中的答案 。
它用另一个ViewPager
第一页replaceFragment
,如果从第二个Fragment
返回,则第一个Fragment
被正确显示。 首页中显示Fragment
无关紧要,如果您从一个页面滑动到另一个页面,则不会更改其Fragment
。
这是我的代码:
FragmentActivity
public class TabsFacturasActivity extends SherlockFragmentActivity { public void onBackPressed() { if(mPager.getCurrentItem() == 0) { if (mAdapter.getItem(0) instanceof DetallesFacturaFragment) { ((DetallesFacturaFragment) mAdapter.getItem(0)).backPressed(); } else if (mAdapter.getItem(0) instanceof FacturasFragment) { finish(); } } } private static class MyAdapter extends FragmentPagerAdapter { private final class FirstPageListener implements FirstPageFragmentListener { public void onSwitchToNextFragment() { mFragmentManager.beginTransaction().remove(mFragmentAtPos0) .commit(); if (mFragmentAtPos0 instanceof FacturasFragment){ mFragmentAtPos0 = new DetallesFacturaFragment(listener); }else{ // Instance of NextFragment mFragmentAtPos0 = new FacturasFragment(listener); } notifyDataSetChanged(); } } private String[] titles = { "VER FACTURAS", "VER CONSUMO", "INTRODUCIR LECTURA" }; private final FragmentManager mFragmentManager; public Fragment mFragmentAtPos0; private Context context; FirstPageListener listener = new FirstPageListener(); public MyAdapter(FragmentManager fragmentManager) { super(fragmentManager); mFragmentManager = fragmentManager; } @Override public CharSequence getPageTitle(int position) { return titles[position]; } @Override public Fragment getItem(int position) { switch (position) { case 0: // Fragment # 0 if (mFragmentAtPos0 == null) { mFragmentAtPos0 = new FacturasFragment(listener); } return mFragmentAtPos0; case 1: // Fragment # 1 return new ConsumoFragment(); case 2:// Fragment # 2 return new LecturaFragment(); } return null; } @Override public int getCount() { return titles.length; } @Override public int getItemPosition(Object object) { if (object instanceof FacturasFragment && mFragmentAtPos0 instanceof DetallesFacturaFragment) { return POSITION_NONE; } if (object instanceof DetallesFacturaFragment && mFragmentAtPos0 instanceof FacturasFragment) { return POSITION_NONE; } return POSITION_UNCHANGED; } } }
FirstPageFragmentListener
public interface FirstPageFragmentListener { void onSwitchToNextFragment(); }
FacturasFragment (FirstFragment)
public class FacturasFragment extends ListFragment implements FirstPageFragmentListener { static FirstPageFragmentListener firstPageListener; public FacturasFragment() { } public FacturasFragment(FirstPageFragmentListener listener) { firstPageListener = listener; } public void onListItemClick (ListView l, View v, int position, long id) { firstPageListener.onSwitchToNextFragment(); } }
DetallesFacturaFragment (SecondFragment)
public class DetallesFacturaFragment extends SherlockFragment { static FirstPageFragmentListener firstPageListener; public DetallesFacturaFragment() { } public DetallesFacturaFragment(FirstPageFragmentListener listener) { firstPageListener = listener; } public void backPressed() { firstPageListener.onSwitchToNextFragment(); } }
解决Androidbuild议不要使用非默认构造函数的小问题。 你应该改变这样的构造函数:
在FacturasFragment:
public static FacturasFragment createInstance(FirstPageFragmentListener listener) { FacturasFragment facturasFragment = new FacturasFragment(); facturasFragment.firstPageListener = listener; return facturasFragment; }
然后你可以像这样从getItem函数调用它:
mFragmentAtPos0 = FacturasFragment.createInstance(listener);
在DetallesFacturaFragment:
public static DetallesFacturaFragment createInstance(FirstPageFragmentListener listener) { DetallesFacturaFragment detallesFacturaFragment= new DetallesFacturaFragment(); detallesFacturaFragment.firstPageListener = listener; return detallesFacturaFragment; }
然后你可以像这样从FirstPageListener.onSwitchToNextFragment()函数调用它:
mFragmentAtPos0 = DetallesFacturaFragment.createInstance(listener);
- adb不显示nexus 5设备
- android studio,其中是proguard映射文件
- 查看可见性为View.GONE时,使用RelativeLayout进行发布
- 如何在右侧显示androidcheckbox?
- 有没有什么办法从Java类更改android:windowSoftInputMode值?
- Android中的Looper,Handler和MessageQueue之间有什么关系?
- 获取存储在drawable中的图像的URI
- Android未知命令“紧缩”
- 在更新到新的Firebase之后获取Exception java.lang.NoClassDefFoundError:com.google.firebase.FirebaseOptions