<tfoot id='o1ZT0'></tfoot>

    1. <legend id='o1ZT0'><style id='o1ZT0'><dir id='o1ZT0'><q id='o1ZT0'></q></dir></style></legend>
        <bdo id='o1ZT0'></bdo><ul id='o1ZT0'></ul>

      <small id='o1ZT0'></small><noframes id='o1ZT0'>

    2. <i id='o1ZT0'><tr id='o1ZT0'><dt id='o1ZT0'><q id='o1ZT0'><span id='o1ZT0'><b id='o1ZT0'><form id='o1ZT0'><ins id='o1ZT0'></ins><ul id='o1ZT0'></ul><sub id='o1ZT0'></sub></form><legend id='o1ZT0'></legend><bdo id='o1ZT0'><pre id='o1ZT0'><center id='o1ZT0'></center></pre></bdo></b><th id='o1ZT0'></th></span></q></dt></tr></i><div id='o1ZT0'><tfoot id='o1ZT0'></tfoot><dl id='o1ZT0'><fieldset id='o1ZT0'></fieldset></dl></div>

    3. 在 ViewPager 中用另一个 Fragment 替换一个 Fragment

      时间:2023-10-04
        <bdo id='Ni2wS'></bdo><ul id='Ni2wS'></ul>
        <legend id='Ni2wS'><style id='Ni2wS'><dir id='Ni2wS'><q id='Ni2wS'></q></dir></style></legend>

        1. <tfoot id='Ni2wS'></tfoot>
            <i id='Ni2wS'><tr id='Ni2wS'><dt id='Ni2wS'><q id='Ni2wS'><span id='Ni2wS'><b id='Ni2wS'><form id='Ni2wS'><ins id='Ni2wS'></ins><ul id='Ni2wS'></ul><sub id='Ni2wS'></sub></form><legend id='Ni2wS'></legend><bdo id='Ni2wS'><pre id='Ni2wS'><center id='Ni2wS'></center></pre></bdo></b><th id='Ni2wS'></th></span></q></dt></tr></i><div id='Ni2wS'><tfoot id='Ni2wS'></tfoot><dl id='Ni2wS'><fieldset id='Ni2wS'></fieldset></dl></div>
                  <tbody id='Ni2wS'></tbody>
              • <small id='Ni2wS'></small><noframes id='Ni2wS'>

                本文介绍了在 ViewPager 中用另一个 Fragment 替换一个 Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                当我尝试用 ViewPager 中的另一个 Fragment 替换一个 Fragment 时遇到了一些问题.

                I'm having some problems when I try to replace one Fragment with another one in a ViewPager.

                现状

                我有一个 ViewPager 有 3 页,每一页都是一个 Fragment.在第一页,我在 ListFragment (FacturasFragment")中有一个 ListView.当我单击该列表中的一个项目时,我使用 onListItemClick 方法来处理该事件.

                I have a ViewPager with 3 pages, each one is a Fragment. In first page, I have a ListView inside a ListFragment ("FacturasFragment"). When I click on an item of that list, I use onListItemClick method for handle that event.

                我想要什么

                • 单击列表项时,我想将 ListFragment(包含发票列表)替换为另一个 Fragment(DetallesFacturaFragment",包含发票详细信息).
                • 当我在DetallesFacturaFragment"并按下返回按钮时,应该返回到ListFragment.
                • 页面之间的滚动不应改变第一个页面中显示的Fragment.如果我在第一页显示DetallesFacturaFragment"并滚动到第二页,则返回第一页时应继续显示DetallesFacturaFragment".
                • When list item is clicked, I want to replace ListFragment (contains a list of invoices) with another Fragment ("DetallesFacturaFragment", contains details of invoice).
                • When I'm in "DetallesFacturaFragment" and press Back Button, should return to ListFragment.
                • Scrolling between pages should not change Fragment displayed in first one. It is, if I'm in first page with "DetallesFacturaFragment" and scroll to second page, when return to first one should continue displaying "DetallesFacturaFragment".

                代码

                片段活动

                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;
                        }
                    }
                
                }
                

                列表片段

                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;
                    }
                }
                

                目前,当我单击列表项时,第一页出现白色视图.我已经验证并执行了DetallesFacturaFragment"的 onCreateView 方法,但该页面上没有任何内容.

                At the moment, when I click on list item, white view appears in first page. I've verified and onCreateView method of "DetallesFacturaFragment" is executed, but nothing appears on that page.

                当我第一次点击列表项时,它会显示那个白屏.但是回到列表后,我必须单击两次才能显示白屏.

                And first time I click on list item, it shows that white screen. But after coming back to list, I have to click twice to a list item for showing white screen.

                我一直在谷歌上搜索并查看了很多问题,但找不到任何人用完整的代码解决了问题.

                I've been googling and looking at some many questions but couldn't find anyone solved with completed code.

                推荐答案

                经过这么多小时,我在 那个答案.

                After so many hours spent, I've found correct solution modifying code in that answer.

                它将 ViewPager 第一页中的 Fragment 替换为另一个,如果您从第二个 Fragment 返回,则第一个 Fragment 正确显示.Fragment 显示在第一页无关紧要,如果您从一个页面滑动到另一个页面,它不会更改其 Fragment.

                It replaces Fragment in first page of ViewPager with another one, and if you return back from second Fragment, first Fragment is correctly displayed. Doesn't matter Fragment displayed in first page, if you swipe from one page to another, it doesn't change its Fragment.

                这是我的代码:

                片段活动

                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)

                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)

                DetallesFacturaFragment (SecondFragment)

                public class DetallesFacturaFragment extends SherlockFragment {
                
                    static FirstPageFragmentListener firstPageListener;
                
                    public DetallesFacturaFragment() {
                    }
                
                    public DetallesFacturaFragment(FirstPageFragmentListener listener) {
                        firstPageListener = listener;
                    }
                
                    public void backPressed() {
                        firstPageListener.onSwitchToNextFragment();
                    }
                }
                

                这篇关于在 ViewPager 中用另一个 Fragment 替换一个 Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Android.app Fragments 与 android.support.v4.app 使用 ViewPager? 下一篇:ScrollView 中的 ViewPager 无法正确滚动

                相关文章

                    <bdo id='hDbxg'></bdo><ul id='hDbxg'></ul>
                1. <legend id='hDbxg'><style id='hDbxg'><dir id='hDbxg'><q id='hDbxg'></q></dir></style></legend>
                  1. <i id='hDbxg'><tr id='hDbxg'><dt id='hDbxg'><q id='hDbxg'><span id='hDbxg'><b id='hDbxg'><form id='hDbxg'><ins id='hDbxg'></ins><ul id='hDbxg'></ul><sub id='hDbxg'></sub></form><legend id='hDbxg'></legend><bdo id='hDbxg'><pre id='hDbxg'><center id='hDbxg'></center></pre></bdo></b><th id='hDbxg'></th></span></q></dt></tr></i><div id='hDbxg'><tfoot id='hDbxg'></tfoot><dl id='hDbxg'><fieldset id='hDbxg'></fieldset></dl></div>

                    <small id='hDbxg'></small><noframes id='hDbxg'>

                    <tfoot id='hDbxg'></tfoot>