本文介绍了在 ViewPager 中将一个 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.

我想要的

  • 当单击列表项时,我想用另一个 Fragment(DetallesFacturaFragment",包含发票的详细信息)替换 ListFragment(包含发票列表).
  • 当我在DetallesFacturaFragment"并按下返回Button时,应该返回到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".

代码

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

目前,当我点击列表项时,第一页出现白色视图.我已经验证并执行了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.

这是我的代码:

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)

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 替换为另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:21