Integrated Swipe Layout

This commit is contained in:
phanhuuloi27
2022-06-16 19:02:18 +07:00
parent 4f6deaf160
commit ab0d7eff30
4 changed files with 106 additions and 41 deletions
@@ -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
@@ -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<Category, MenuViewHolder> 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
@@ -1,26 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FoodList">
<com.mancj.materialsearchbar.MaterialSearchBar
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/searchBar"
app:mt_speechMode="false"
app:mt_hint="Enter your food"
/>
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_below="@+id/searchBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyler_food"
android:scrollbars="vertical"
/>
<com.mancj.materialsearchbar.MaterialSearchBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/searchBar"
app:mt_speechMode="false"
app:mt_hint="Enter your food"
/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_below="@+id/searchBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyler_food"
android:scrollbars="vertical"
/>
</RelativeLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
@@ -14,4 +15,4 @@
android:layout_height="match_parent"
android:scrollbars="vertical" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>