我在Android Studio中为一家餐馆制作应用程序。我有一个带有导航抽屉的MainActivity,我在不同的片段之间进行切换,因此我可以保持导航菜单相同但切换内容。
所以,我想在其中一个片段的顶部添加一个水平菜单栏,这样用户就可以过滤不同类型的菜肴(甜点,肉类,鱼类,面食......)。该片段还有一个ListView来显示从DB中获取的菜肴
这是一个问题,我可以采取哪种最佳方法来保持尽可能简单?我想在Fragment的布局中添加带有按钮的LinearLayout,但由于我有几个它们太小了,所以滑动条会很好但我不知道如何从它开始。
感谢阅读,我一直在寻找
- 编辑 - 这是我参考的代码
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的布局只是一个包含ListView的RelativeLayout
答案 0 :(得分:0)
使用HorizontalScrollView元素正是我所寻找的,并不知道android有那个。在本教程中,您可以轻松完成https://www.youtube.com/watch?v=0PAsR5sIi6E
感谢您的回答,我应该多看一点(大脑在几个小时后停止工作:p)