تطبيق restaurant بأندرويد و firebase الجزء الثاني
نظرة سريعة بالفيديو
1- الملف Item.java
package com.example.belasri.restaurantapp.model;
public class Item {
private String title;
private String description;
private String imageUrl;
public Item(String title, String description, String imageUrl) {
this.title = title;
this.description = description;
this.imageUrl = imageUrl;
}
public Item(){
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
2-bottom_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:title="Accueil"
android:orderInCategory="1"
android:icon="@drawable/ic_home_black_24dp"></item>
<item
android:id="@+id/cmd"
android:title="Commandes"
android:icon="@drawable/card"
android:orderInCategory="2"></item>
<item
android:id="@+id/add"
android:title="Inserer"
android:icon="@drawable/pencil"
android:orderInCategory="3"></item>
</menu>
3- BottomNavigationViewHelper.java
package com.example.belasri.restaurantapp.helpers;
import android.annotation.SuppressLint;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.util.Log;
import java.lang.reflect.Field;
/**
* Created by belasri on 29/11/2017.
*/
public class BottomNavigationViewHelper {
@SuppressLint("RestrictedApi")
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}
4- الملف MainActivity.java
فالمجلد java
كاين الملف MainActivity.java فيه الكود لي كيمكن باش ن référencer la
base منين غادي نجبد المعلومات ولي هي Menu من بعد كنسترجع ال RecyclerView و BottomNavigationView من
الملف activity_main.xml ومنبعد عندي ل fonction showMenu فيها كنخدم ب FirebaseRecyclerAdapter لي كيمكنا باش نزيدو المعلومات لي استرجعنا فبلايصهم فكنعطيه ل class Item و ل list_item.xml
ول MenuViewHolder لي غادي تمكني باش نmodifier المعلومات لي فال
ImageView و TextView منبعد مكنسترجعهم وكنعطيه ايضا référence لقاعدة
البيانات وكنزيد لRecyclerView
ل onclick لي كتمكني باش ملي نضغط على شي plat كنصيفط ل id
ديالو ل MenuDetailsActivity لي غادي تمكنا من مشاهدة تفاصيل ل plat ولي غادي نزيدوها منبعد ثم كاين onNavigationItemSelected لي ملي كيضغط المستخدم على شي item من Bottom Menu كتديه لل Activity لي اختار ا الكود ديال الملف هو:
package com.example.belasri.restaurantapp;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.belasri.restaurantapp.helpers.BottomNavigationViewHelper;
import com.example.belasri.restaurantapp.model.Item;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{
private BottomNavigationView bottomNavigationView;
private BottomNavigationViewHelper bottomNavigationViewHelper;
private DatabaseReference menuDb;
private TextView title;
private RecyclerView recyclerview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNav);
recyclerview = (RecyclerView) findViewById(R.id.menu);
menuDb = FirebaseDatabase.getInstance().getReference().child("Menu");
bottomNavigationView.setOnNavigationItemSelectedListener(this);
bottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
LinearLayoutManager linearManager = new LinearLayoutManager(this);
linearManager.setOrientation(linearManager.VERTICAL);
recyclerview.setLayoutManager(new GridLayoutManager(this,2));
recyclerview.setItemAnimator(new DefaultItemAnimator());
showMenu();
}
private void showMenu(){
FirebaseRecyclerAdapter<Item, MenuViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Item, MenuViewHolder>(
Item.class,
R.layout.list_item,
MenuViewHolder.class,
menuDb
) {
@Override
protected void populateViewHolder(MenuViewHolder viewHolder, Item model, int position) {
//get selected post id
final String menu_id = getRef(position).getKey().toString();
viewHolder.setTitle(model.getTitle());
viewHolder.setImage(model.getImageUrl(),getApplicationContext());
viewHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent singleItem = new Intent(MainActivity.this,MenuDetailsActivity.class);
singleItem.putExtra("menu_id",menu_id);
startActivity(singleItem);
}
});
}
};
recyclerview.setAdapter(firebaseRecyclerAdapter);
}
public static class MenuViewHolder extends RecyclerView.ViewHolder{
View view;
public MenuViewHolder(View itemView) {
super(itemView);
view = itemView;
}
public void setTitle(String title){
TextView mTitle = (TextView) view.findViewById(R.id.title);
mTitle.setText(title);
}
public void setImage(String image,Context ctx){
ImageView imageView = (ImageView) view.findViewById(R.id.image);
Picasso.with(ctx).load(image).into(imageView);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.home :
Intent homeIntent = new Intent(MainActivity.this,MainActivity.class);
startActivity(homeIntent);
break;
case R.id.add :
Intent favsIntent = new Intent(MainActivity.this,AddToMenu.class);
startActivity(favsIntent);
break;
case R.id.cmd:
Intent commandIntent = new Intent(MainActivity.this,CommandsActivity.class);
startActivity(commandIntent);
break;
}
return true;
}
}