Skip to content

Instantly share code, notes, and snippets.

@gvr23
Created January 13, 2018 02:08
Show Gist options
  • Save gvr23/968ba1f32fa2caafdcfb1335bb7856f3 to your computer and use it in GitHub Desktop.
Save gvr23/968ba1f32fa2caafdcfb1335bb7856f3 to your computer and use it in GitHub Desktop.
setear tabs en un activity
ANDROID
===================================================================================================================================
viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), getBaseContext()));
slidingTabs.setupWithViewPager(viewpager);
===================================================================================================================================
xml
===================================================================================================================================
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
style="@style/MyCustomTabLayout"
app:tabMode="fixed"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="10"
android:background="@android:color/white" />
==================================================================================================================================
ADAPTER
==================================================================================================================================
public class FragmentPagerAdapter extends FragmentStatePagerAdapter {
private String[] liqTabList;
private Context context;
final int PAGE_COUNT = 3;
public FragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
liqTabList = context.getResources().getStringArray(R.array.liqTabArray);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return LiquidationFragment.newInstance(position + 1);
case 1:
return ClientFragment.newInstance(position + 1);
case 2:
return TotalFragment.newInstance(position + 1);
}
return LiquidationFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return liqTabList[position];
}
===============================================================================================================================
fragment
===============================================================================================================================
public class ClientFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
public static final int CLIENT_CODE = 2;
Unbinder unbinder;
@BindView(R.id.lnRowNewClient)
LinearLayout lnRowNewClient;
@BindView(R.id.btnSave)
Button btnSave;
@BindView(R.id.btnAdd)
Button btnAdd;
private int mPage;
public static ClientFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
ClientFragment fragment = new ClientFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
preCargar();
eventos();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.client_fragment_layout, container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
private void preCargar() {
}
private void eventos() {
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CLIENT_CODE && resultCode == RESULT_OK){
ClienteBean clienteBean = (ClienteBean) data.getSerializableExtra("client");
LiquidacionController.getInstance().getClientList().add(clienteBean);
insertarRowCliente(clienteBean);
}
}
@OnClick({R.id.btnSave, R.id.btnAdd})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btnSave:
break;
case R.id.btnAdd:
sendTipoActivity(SearchCustomerActivity.class);
break;
}
}
private void sendTipoActivity(Class clase){
Intent intent = new Intent(getContext(), clase);
intent.putExtra("tipoActivity", "Liq");
startActivityForResult(intent, CLIENT_CODE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment