I'm making an app for a restaurant in Android Studio. I have a MainActivity with a Navigation Drawer and I change between different Fragments so I can keep the Navigation Menu the same but switch content.
So, I want to add a horizontal menu bar on the top side of one of those Fragments, so users can filter different type of dishes (dessert, meat, fish, pasta ...). The fragment also has a ListView to show the dishes taken from a DB
Here is the question, what's the best approach I can take to keep it as simple as possible? I thought of adding a LinearLayout with buttons to the Fragment's layout but since I have several of them they are too small, so a sliding bar would be nice but I have no idea how to start with it.
Thanks for reading, I'll keep looking
-- EDIT -- Here is the code I'm refering to
public class MenuFragment extends ListFragment {
static AdaptadorMenu adaptador;
static ArrayList<ElementoMenu> arrayList = new ArrayList();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_menu, container, false);
//((MainActivity) getActivity()).getSupportActionBar().setTitle("Fragment Inbox");
cargaDatos();
//consultaBD();
return view;
}
private void cargaDatos(){
JSONArray jsonArray = MainActivity.jsonMenu;
arrayList.clear();
if (jsonArray!= null) {
JSONObject jo;
for (int i = 0; i < jsonArray.length(); i++) {
try {
jo = jsonArray.getJSONObject(i);
int idPromocion = Integer.parseInt(jo.getString("idMenu")); //en mysql id empieza por 1, por eso el -1 abajo
String nombre = jo.getString("nombre");
String descripcion = jo.getString("descripcion");
float precio = Float.parseFloat(""+jo.getDouble("precio"));
String direcImagen = jo.getString("direcImagen");
//Log.e("JSON", idPromocion + " " + nombre + " " + descripcion + " " + precio + " " + direcImagen);
arrayList.add(new ElementoMenu(idPromocion-1, nombre, descripcion, precio, direcImagen));
} catch (JSONException e) {
e.printStackTrace();
}
}
aplicaAdaptador();
}
}
public void aplicaAdaptador(){
adaptador = new AdaptadorMenu(getActivity(), arrayList);
adaptador.notifyDataSetChanged();
setListAdapter(adaptador);
}
}
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBar actionBar;
private final int MENU = 0, PROMOCIONES = 1, MAPA = 2, RESERVAS = 3;
public static JSONArray jsonDatos, jsonMenu, jsonPromociones;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
if (navigationView != null) {
setupNavigationDrawerContent(navigationView);
}
setupNavigationDrawerContent(navigationView);
consultaBD();
//setUltimaActualizacion(DateTime.now().toString());
//Log.e("HORA", ""+getHoraActual()/1000);
//getPreferencias(this);
}
@Override
public void onResume(){
super.onResume();
onResumeConsultaBD();
Toast.makeText(getBaseContext(), "onResume", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
//aqui se elige que sucedera cuando se pulse cada uno de los componentes del NavigationDrawer menu
private void setupNavigationDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.item_carta:
menuItem.setChecked(true);
setFragment(MENU);
Toast.makeText(getBaseContext(), "CARTAPULSADA", Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_promociones:
menuItem.setChecked(true);
setFragment(PROMOCIONES);
Toast.makeText(getBaseContext(), "PROMOSPULSADA", Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_como_llegar:
menuItem.setChecked(true);
setFragment(MAPA);
Toast.makeText(getBaseContext(), "MAPAPULSADO", Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_reservas:
menuItem.setChecked(true);
setFragment(RESERVAS);
Toast.makeText(getBaseContext(), "RESERVASPULSADO", Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_noticias:
menuItem.setChecked(true);
//setFragment(4);
Toast.makeText(getBaseContext(), "NOTICIASPULSADO", Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_sobre_nosotros:
menuItem.setChecked(true);
//setFragment(5);
Toast.makeText(getBaseContext(), "SOBRENOSOTROSPULSADO", Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
return true;
}
});
}
//aqui se llama a los fragments. MenuFragment, PromocionesFragment, MapFragment....
public void setFragment(int position) {
switch (position) {
case MENU:
cambiaFragment(new MenuFragment());
break;
case PROMOCIONES:
cambiaFragment(new PromocionesFragment());
break;
case MAPA:
cambiaFragment(new MapFragment());
break;
case RESERVAS:
cambiaFragment(new ReservasFragment());
break;
}
}
private void cambiaFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment, fragment);
fragmentTransaction.commit();
}
Fragment's layout is just a RelativeLayout with a ListView in it
Using HorizontalScrollView element was exactly what I was looking for, didn't knew android had that. In this tutorial is easily done https://www.youtube.com/watch?v=0PAsR5sIi6E
Thanks for answering, I should have looked a little more (Brain stops working after several hours :p)