From ab0d7eff30c930b82c0a91d18edcee1229b92bc0 Mon Sep 17 00:00:00 2001 From: phanhuuloi27 <73208452+Nezumi2711@users.noreply.github.com> Date: Thu, 16 Jun 2022 19:02:18 +0700 Subject: [PATCH] Integrated Swipe Layout --- .../java/com/waterbase/foodify/FoodList.java | 59 +++++++++++++++---- .../main/java/com/waterbase/foodify/Home.java | 44 ++++++++++---- .../main/res/layout/activity_food_list.xml | 39 +++++++----- .../app/src/main/res/layout/content_home.xml | 5 +- 4 files changed, 106 insertions(+), 41 deletions(-) diff --git a/Foodify/app/src/main/java/com/waterbase/foodify/FoodList.java b/Foodify/app/src/main/java/com/waterbase/foodify/FoodList.java index 132a476..e96cdf8 100644 --- a/Foodify/app/src/main/java/com/waterbase/foodify/FoodList.java +++ b/Foodify/app/src/main/java/com/waterbase/foodify/FoodList.java @@ -5,6 +5,7 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import android.content.Intent; import android.os.Bundle; @@ -51,6 +52,8 @@ public class FoodList extends AppCompatActivity { //Favorites Database localDB; + SwipeRefreshLayout swipeRefreshLayout; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -63,24 +66,53 @@ public class FoodList extends AppCompatActivity { //Local DB localDB = new Database(this); + swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout); + swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, + android.R.color.holo_green_dark, + android.R.color.holo_orange_dark, + android.R.color.holo_blue_dark); + + swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + //Get Intent here + if(getIntent() != null) + categoryId = getIntent().getStringExtra("CategoryId"); + categoryName = getIntent().getStringExtra("CategoryName"); + if(!categoryId.isEmpty() && categoryId != null) { + if(Common.isConnectedToInternet(getBaseContext())) + loadListFood(categoryId); + else { + Toast.makeText(FoodList.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show(); + return; + } + } + } + }); + + swipeRefreshLayout.post(new Runnable() { + @Override + public void run() { + //Get Intent here + if(getIntent() != null) + categoryId = getIntent().getStringExtra("CategoryId"); + categoryName = getIntent().getStringExtra("CategoryName"); + if(!categoryId.isEmpty() && categoryId != null) { + if(Common.isConnectedToInternet(getBaseContext())) + loadListFood(categoryId); + else { + Toast.makeText(FoodList.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show(); + return; + } + } + } + }); + recyclerView = (RecyclerView) findViewById(R.id.recyler_food); recyclerView.setHasFixedSize(true); layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); - //Get Intent here - if(getIntent() != null) - categoryId = getIntent().getStringExtra("CategoryId"); - categoryName = getIntent().getStringExtra("CategoryName"); - if(!categoryId.isEmpty() && categoryId != null) { - if(Common.isConnectedToInternet(getBaseContext())) - loadListFood(categoryId); - else { - Toast.makeText(FoodList.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show(); - return; - } - } - //Search materialSearchBar = (MaterialSearchBar) findViewById(R.id.searchBar); materialSearchBar.setHint("Enter your food"); @@ -228,6 +260,7 @@ public class FoodList extends AppCompatActivity { //Set Adapter recyclerView.setAdapter(adapter); + swipeRefreshLayout.setRefreshing(false); } @Override diff --git a/Foodify/app/src/main/java/com/waterbase/foodify/Home.java b/Foodify/app/src/main/java/com/waterbase/foodify/Home.java index 29add57..35128f6 100644 --- a/Foodify/app/src/main/java/com/waterbase/foodify/Home.java +++ b/Foodify/app/src/main/java/com/waterbase/foodify/Home.java @@ -20,6 +20,7 @@ import androidx.drawerlayout.widget.DrawerLayout; import androidx.navigation.ui.AppBarConfiguration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.firebase.ui.database.FirebaseRecyclerAdapter; import com.google.android.gms.tasks.OnCompleteListener; @@ -53,7 +54,7 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati RecyclerView.LayoutManager layoutManager; FirebaseRecyclerAdapter adapter; - private AppBarConfiguration mAppBarConfiguration; + SwipeRefreshLayout swipeRefreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { @@ -65,6 +66,37 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati toolbar.setTitle("Menu"); setSupportActionBar(toolbar); + //View + swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout); + swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, + android.R.color.holo_green_dark, + android.R.color.holo_orange_dark, + android.R.color.holo_blue_dark); + swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + if (Common.isConnectedToInternet(getBaseContext())) + loadMenu(); + else { + Toast.makeText(getBaseContext(), "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show(); + return; + } + } + }); + + //Default, load for first time + swipeRefreshLayout.post(new Runnable() { + @Override + public void run() { + if (Common.isConnectedToInternet(getBaseContext())) + loadMenu(); + else { + Toast.makeText(getBaseContext(), "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show(); + return; + } + } + }); + DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.app_name, R.string.app_name); drawer.setDrawerListener(toggle); @@ -86,14 +118,6 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati layoutManager = new LinearLayoutManager(this); recyler_menu.setLayoutManager(layoutManager); - if (Common.isConnectedToInternet(getBaseContext())) - loadMenu(); - else { - Toast.makeText(this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show(); - return; - } - - //Set name for user View headerView = navigationView.getHeaderView(0); TextView navUserName = (TextView) headerView.findViewById(R.id.txtFullName); @@ -131,7 +155,7 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati } }; recyler_menu.setAdapter(adapter); - + swipeRefreshLayout.setRefreshing(false); } @Override diff --git a/Foodify/app/src/main/res/layout/activity_food_list.xml b/Foodify/app/src/main/res/layout/activity_food_list.xml index 91dd2a1..a9bf13b 100644 --- a/Foodify/app/src/main/res/layout/activity_food_list.xml +++ b/Foodify/app/src/main/res/layout/activity_food_list.xml @@ -1,26 +1,33 @@ - - + android:layout_height="match_parent"> - + - \ No newline at end of file + + + + + \ No newline at end of file diff --git a/Foodify/app/src/main/res/layout/content_home.xml b/Foodify/app/src/main/res/layout/content_home.xml index 8efdd6c..78b747e 100644 --- a/Foodify/app/src/main/res/layout/content_home.xml +++ b/Foodify/app/src/main/res/layout/content_home.xml @@ -1,5 +1,6 @@ - - \ No newline at end of file + \ No newline at end of file