mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 13:38:41 +00:00
Add pagination in Search Fragment
This commit is contained in:
@@ -31,11 +31,14 @@ public interface FoodApi {
|
|||||||
Call<List<Food>> drinksFoodResponse();
|
Call<List<Food>> drinksFoodResponse();
|
||||||
|
|
||||||
@GET("listFood")
|
@GET("listFood")
|
||||||
Call<List<Food>> listFood(@Query("search") String search);
|
Call<List<Food>> listFood(@Query("search") String search, @Query("page") int page, @Query("limit") int limit);
|
||||||
|
|
||||||
@GET("Category")
|
@GET("Category")
|
||||||
Call<List<Category>> listCategory();
|
Call<List<Category>> listCategory();
|
||||||
|
|
||||||
@GET("Category/{id}/food")
|
@GET("Category/{id}/food")
|
||||||
Call<List<Food>> listFoodByCategory(@Path("id") String id);
|
Call<List<Food>> listFoodByCategory(@Path("id") String id);
|
||||||
|
|
||||||
|
@GET("listFood")
|
||||||
|
Call<List<Food>> listFoodByPagination(@Query("page") int page, @Query("limit") int limit);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,18 @@ import static com.capstone.foodify.Fragment.HomeFragment.listFood;
|
|||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
import android.view.inputmethod.InputMethodManager;
|
||||||
import android.widget.CompoundButton;
|
import android.widget.CompoundButton;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.core.widget.NestedScrollView;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.recyclerview.widget.GridLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
@@ -37,8 +42,18 @@ import retrofit2.Response;
|
|||||||
|
|
||||||
public class SearchFragment extends Fragment {
|
public class SearchFragment extends Fragment {
|
||||||
|
|
||||||
|
private static int CURRENT_PAGE = 1;
|
||||||
|
private static final int LIMIT = 18;
|
||||||
|
private static int TOTAL_PAGE = 5;
|
||||||
|
private static int ACTION_CODE = -1;
|
||||||
|
private static final int LIST_FOOD = -1;
|
||||||
|
private static final int SEARCH_FOOD = 0;
|
||||||
|
private String searchQuery = "";
|
||||||
private RecyclerView recycler_view_search;
|
private RecyclerView recycler_view_search;
|
||||||
|
private NestedScrollView nestedScrollView;
|
||||||
|
private TextView end_of_list_text_view;
|
||||||
private FoodSearchAdapter foodAdapter;
|
private FoodSearchAdapter foodAdapter;
|
||||||
|
private ProgressBar progressBar;
|
||||||
private static final List<Food> listFoodSearch = new ArrayList<>();
|
private static final List<Food> listFoodSearch = new ArrayList<>();
|
||||||
private ChipGroup list_Category;
|
private ChipGroup list_Category;
|
||||||
private final List<String> statusCategoryChecked = new ArrayList<>();
|
private final List<String> statusCategoryChecked = new ArrayList<>();
|
||||||
@@ -52,17 +67,31 @@ public class SearchFragment extends Fragment {
|
|||||||
PersistentSearchView persistentSearchView = view.findViewById(R.id.search_view);
|
PersistentSearchView persistentSearchView = view.findViewById(R.id.search_view);
|
||||||
recycler_view_search = view.findViewById(R.id.recycler_view_search);
|
recycler_view_search = view.findViewById(R.id.recycler_view_search);
|
||||||
list_Category = view.findViewById(R.id.list_category);
|
list_Category = view.findViewById(R.id.list_category);
|
||||||
|
nestedScrollView = view.findViewById(R.id.search_fragment);
|
||||||
foodAdapter = new FoodSearchAdapter(getContext());
|
foodAdapter = new FoodSearchAdapter(getContext());
|
||||||
|
progressBar = view.findViewById(R.id.progress_bar);
|
||||||
|
end_of_list_text_view = view.findViewById(R.id.end_of_list_text);
|
||||||
|
|
||||||
getListCategory();
|
getListCategory();
|
||||||
|
|
||||||
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
|
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
|
||||||
recycler_view_search.setLayoutManager(gridLayoutManager);
|
recycler_view_search.setLayoutManager(gridLayoutManager);
|
||||||
|
recycler_view_search.setNestedScrollingEnabled(false);
|
||||||
foodAdapter.setData(listFood);
|
|
||||||
recycler_view_search.setAdapter(foodAdapter);
|
recycler_view_search.setAdapter(foodAdapter);
|
||||||
|
|
||||||
|
getListFoodByPagination(1);
|
||||||
|
if(nestedScrollView != null){
|
||||||
|
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onScrollChange(@NonNull NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
|
||||||
|
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
|
||||||
|
//When user scroll to bottom, load more data
|
||||||
|
dataLoadMore();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
persistentSearchView.setLeftButtonDrawable(R.drawable.ic_search);
|
persistentSearchView.setLeftButtonDrawable(R.drawable.ic_search);
|
||||||
persistentSearchView.setOnSearchConfirmedListener(new OnSearchConfirmedListener() {
|
persistentSearchView.setOnSearchConfirmedListener(new OnSearchConfirmedListener() {
|
||||||
|
|
||||||
@@ -70,20 +99,13 @@ public class SearchFragment extends Fragment {
|
|||||||
public void onSearchConfirmed(PersistentSearchView searchView, String query) {
|
public void onSearchConfirmed(PersistentSearchView searchView, String query) {
|
||||||
// Handle a search confirmation. This is the place where you'd
|
// Handle a search confirmation. This is the place where you'd
|
||||||
// want to perform a search against your data provider.
|
// want to perform a search against your data provider.
|
||||||
|
ACTION_CODE = SEARCH_FOOD;
|
||||||
|
CURRENT_PAGE = 1;
|
||||||
|
showProgressBarAndHideEndOfListText();
|
||||||
|
listFoodSearch.clear();
|
||||||
if(!query.isEmpty()){
|
if(!query.isEmpty()){
|
||||||
FoodApi.apiService.listFood(query).enqueue(new Callback<List<Food>>() {
|
searchQuery = query;
|
||||||
@Override
|
getListFoodBySearch(searchQuery, CURRENT_PAGE);
|
||||||
public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
|
|
||||||
loadFoodAdapter(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(Call<List<Food>> call, Throwable t) {
|
|
||||||
Toast.makeText(getContext(), "Lỗi" + t, Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Toast.makeText(getContext(), "Category checked: " + statusCategoryChecked.toString(), Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
searchView.collapse();
|
searchView.collapse();
|
||||||
@@ -95,8 +117,11 @@ public class SearchFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onSearchQueryChanged(PersistentSearchView searchView, String oldQuery, String newQuery) {
|
public void onSearchQueryChanged(PersistentSearchView searchView, String oldQuery, String newQuery) {
|
||||||
if(searchView.isInputQueryEmpty()){
|
if(searchView.isInputQueryEmpty()){
|
||||||
|
CURRENT_PAGE = 1;
|
||||||
|
ACTION_CODE = LIST_FOOD;
|
||||||
|
showProgressBarAndHideEndOfListText();
|
||||||
foodAdapter.setData(listFood);
|
foodAdapter.setData(listFood);
|
||||||
recycler_view_search.setAdapter(foodAdapter);
|
foodAdapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -104,14 +129,37 @@ public class SearchFragment extends Fragment {
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void showProgressBarAndHideEndOfListText(){
|
||||||
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
|
end_of_list_text_view.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void hideProgressBarAndShowEndOfListText(){
|
||||||
|
progressBar.setVisibility(View.GONE);
|
||||||
|
end_of_list_text_view.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void getListFoodBySearch(String query, int page) {
|
||||||
|
FoodApi.apiService.listFood(query, page, LIMIT).enqueue(new Callback<List<Food>>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
|
||||||
|
loadFoodAdapter(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<List<Food>> call, Throwable t) {
|
||||||
|
Toast.makeText(getContext(), "Lỗi" + t, Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void loadFoodAdapter(Response<List<Food>> response) {
|
private void loadFoodAdapter(Response<List<Food>> response) {
|
||||||
List<Food> foodData = response.body();
|
List<Food> foodData = response.body();
|
||||||
|
|
||||||
if(foodData != null) {
|
if(foodData != null) {
|
||||||
listFoodSearch.clear();
|
|
||||||
listFoodSearch.addAll(foodData);
|
listFoodSearch.addAll(foodData);
|
||||||
foodAdapter.setData(listFoodSearch);
|
foodAdapter.setData(listFoodSearch);
|
||||||
recycler_view_search.setAdapter(foodAdapter);
|
foodAdapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,6 +207,7 @@ public class SearchFragment extends Fragment {
|
|||||||
FoodApi.apiService.listFoodByCategory(id).enqueue(new Callback<List<Food>>() {
|
FoodApi.apiService.listFoodByCategory(id).enqueue(new Callback<List<Food>>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
|
public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
|
||||||
|
listFoodSearch.clear();
|
||||||
loadFoodAdapter(response);
|
loadFoodAdapter(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,4 +218,39 @@ public class SearchFragment extends Fragment {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void getListFoodByPagination(int page){
|
||||||
|
FoodApi.apiService.listFoodByPagination(page, LIMIT).enqueue(new Callback<List<Food>>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
|
||||||
|
loadFoodAdapter(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<List<Food>> call, Throwable t) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dataLoadMore() {
|
||||||
|
switch (ACTION_CODE){
|
||||||
|
case LIST_FOOD:{
|
||||||
|
if(CURRENT_PAGE < TOTAL_PAGE){
|
||||||
|
getListFoodByPagination(++CURRENT_PAGE);
|
||||||
|
} else {
|
||||||
|
hideProgressBarAndShowEndOfListText();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SEARCH_FOOD:{
|
||||||
|
if(CURRENT_PAGE < TOTAL_PAGE){
|
||||||
|
getListFoodBySearch(searchQuery, ++CURRENT_PAGE);
|
||||||
|
} else {
|
||||||
|
hideProgressBarAndShowEndOfListText();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,9 @@ public class Food {
|
|||||||
private String name;
|
private String name;
|
||||||
private String price;
|
private String price;
|
||||||
|
|
||||||
|
public Food() {
|
||||||
|
}
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".Fragment.SearchFragment"
|
tools:context=".Fragment.SearchFragment"
|
||||||
|
android:id="@+id/search_fragment"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
>
|
>
|
||||||
|
|
||||||
@@ -37,6 +38,28 @@
|
|||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progress_bar"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/recycler_view_search"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/end_of_list_text"
|
||||||
|
android:text="End of list"
|
||||||
|
android:fontFamily="@font/opensans"
|
||||||
|
android:textColor="@color/graylight"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/progress_bar"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"/>
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user