Android TabLayout with Fragments,使用newInstance

时间:2015-10-14 23:44:25

标签: android android-fragments android-tablayout

我有几天遇到问题并决定直接询问,希望得到一些帮助。对不起,如果我的英语不好。

我有什么: 使用ViewPager(FragmentStatePagerAdapter)的活动有3个片段,并使用每个片段中定义的newInstance方法发生参数(Bundle)。

我想要的是什么: 在Fragment A中用一个按钮将一些数据发送到片段B.片段B立即切换到使用: ACTIVITY.setCurrentItem(item); -----> Item =此情况下的片段编号1。 并控制从片段A发送的数据。

问题: 我同时使用newInstance来发送数据: 1.活动 - >碎片(A,B,C) 2.片段A - >片段B 但是从片段A发送到片段B的数据为空。我只能通过活动发送。

使用标签在片段之间移动。

一些代码:

ViewPager

public class PagerAdapterMainCliente extends FragmentStatePagerAdapter {
int gNumsTabsCliente;
Bundle arguments;
TabFragmentCotizacion tfCotizacionCliente;

public PagerAdapterMainCliente(FragmentManager fragmentManager, int numTabsCliente, Bundle arguments)
{
    super(fragmentManager);
    this.gNumsTabsCliente = numTabsCliente;
    this.arguments = arguments;
}

@Override
public Fragment getItem(int position)
{
    switch (position)
    {
        case 0:
            TabFragmentProducto tfProductoCliente = TabFragmentProducto.newInstance(arguments);
            return tfProductoCliente;
        case 1:
            tfCotizacionCliente = TabFragmentCotizacion.newInstance(arguments);
            return tfCotizacionCliente;
        case 2:
            TabFragmentPedido tfPedidoCliente = new TabFragmentPedido();
            return tfPedidoCliente;
        default:
            return null;
    }
}

@Override
public int getCount() { return gNumsTabsCliente; }

}

活动

public class MainCliente extends AppCompatActivity {

private Activity thisActivity;

ViewPager vpagerMainClienteA;
PagerAdapterMainCliente pAdapterMainCliente;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_cliente);
    this.thisActivity = this;

    Bundle bundleCliente = new Bundle();
    bundleCliente.putString("CODIGO_CLIENTE", getIntent().getStringExtra("CODIGO_CLIENTE"));

    TabLayout tlayoutMainClienteA = (TabLayout) findViewById(R.id.tlayoutMainCliente);


    tlayoutMainClienteA.addTab(tlayoutMainClienteA.newTab().setText("Productos"));
    tlayoutMainClienteA.addTab(tlayoutMainClienteA.newTab().setText("Cotización"));
    tlayoutMainClienteA.addTab(tlayoutMainClienteA.newTab().setText("Pedidos"));
    tlayoutMainClienteA.setTabGravity(TabLayout.OVER_SCROLL_ALWAYS);

    vpagerMainClienteA = (ViewPager) findViewById(R.id.vpagerMainCliente);
    pAdapterMainCliente = new PagerAdapterMainCliente(getSupportFragmentManager(), tlayoutMainClienteA.getTabCount(), bundleCliente);

    vpagerMainClienteA.setAdapter(pAdapterMainCliente);
    vpagerMainClienteA.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tlayoutMainClienteA));
    vpagerMainClienteA.setOffscreenPageLimit(1);

    tlayoutMainClienteA.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            vpagerMainClienteA.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {}

        @Override
        public void onTabReselected(TabLayout.Tab tab) {}
    });
}

public void setCurrenttPagerItem(int item) {
    vpagerMainClienteA.setCurrentItem(item);
}

片段A

public class TabFragmentProducto extends Fragment {
ProgressDialog pdialog;boolean resp;

private EditText etBuscarProductoA;
private ListView lvListaProductoA;
private Button bSeleccionarProductoA;

ArrayList<ArrayDatosProducto> arrayDatosProducto = new ArrayList<>();
ArrayDatosProducto arrayDatosP;

public TabFragmentProducto() {}

public static TabFragmentProducto newInstance(Bundle arguments) {
    TabFragmentProducto tabFragmentProducto = new TabFragmentProducto();
    if (arguments != null) {
        tabFragmentProducto.setArguments(arguments);
    }
    return tabFragmentProducto;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.tab_fragment_producto, container, false);

    etBuscarProductoA = (EditText) rootView.findViewById(R.id.etBuscarProducto);
    lvListaProductoA = (ListView) rootView.findViewById(R.id.lvListaProducto);
    bSeleccionarProductoA = (Button) rootView.findViewById(R.id.bSeleccionarProducto);




    bSeleccionarProductoA.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

              //Send data to Fragment B
                  Bundle bundleCotizacion = new Bundle();

            bundleCotizacion.putString("hola", "hola");
            TabFragmentCotizacion tabFragmentCotizacion = TabFragmentCotizacion.newInstance("hola");
            ((MainCliente) getActivity()).setCurrenttPagerItem(1);

            }
    });

片段B

public class TabFragmentCotizacion extends Fragment {

private JSONObject jsonobject;
private ArrayDatosCotizacion arrayDatosCotizacionBD;
private ArrayList<ArrayDatosCotizacion> arrayDatosCotizacions = new ArrayList<>();


public TabFragmentCotizacion() {}

public static TabFragmentCotizacion newInstance(Bundle arguments) {
    TabFragmentCotizacion tabFragmentCotizacion = new TabFragmentCotizacion();
    if (arguments != null) {
        tabFragmentCotizacion.setArguments(arguments);
    }
    return tabFragmentCotizacion;
 }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,       Bundle savedInstanceState) {
     final View rootView = inflater.inflate(R.layout.tab_fragment_cotizacion,     container, false);

return rootView;
}

}

2 个答案:

答案 0 :(得分:1)

你已经putString(“hola”,“hola”); 但不是那样,你继续使用单个字符串“hola”

TabFragmentCotizacion tabFragmentCotizacion = TabFragmentCotizacion.newInstance(bundleCotizaction);

答案 1 :(得分:0)

而不是

TabFragmentCotizacion tabFragmentCotizacion = TabFragmentCotizacion.newInstance("hola");

使用

TabFragmentCotizacion tabFragmentCotizacion = TabFragmentCotizacion.newInstance(bundleCotizacion);