【发布时间】:2016-12-13 19:45:50
【问题描述】:
我有几天的问题,决定直接问,期待一些帮助。对不起,如果我的英语不好。
我有什么: 使用 ViewPager (FragmentStatePagerAdapter) 的活动有 3 个片段,并且使用每个片段中定义的 newInstance 方法发生参数 (Bundle)。
我想要什么: 在 Fragment A 通过一个按钮向 Fragment B 发送一些数据。 Fragment B 立即切换到使用: ACTIVITY.setCurrentItem(项目); -----> 项目 = 在这种情况下的片段编号 1。 并控制从 Fragment A 发送的数据。
问题: 我也使用 newInstance 来发送数据: 1.活动->片段(A,B,C) 2. 片段 A -> 片段 B 但是从 Fragment A 发送到 Fragment B 的数据是空的。我只能通过 Activity 发送。
使用选项卡在片段之间移动。
一些代码:
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;
}
}
【问题讨论】:
标签: android android-fragments android-tablayout