Home > Enterprise >  Why my page is lagging while click on "Daily Meal"
Why my page is lagging while click on "Daily Meal"

Time:04-01

I am working on food delivery application. I have four tab in menu bar Home, Daily Meal, Favourite and My Cart. My all tab is work well but when i click on Daily Meal my page is rendering. I am googling for them but not found any solution. I am stuck from 2 days to this problem. Please look at my problem and solve it. Thanks in advance.

Here is my code:

daily_meal_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.DailyMeal.DailyMealFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/daily_meal_rec"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

daily_meal_ver.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp">

    <ImageView
        android:id="@ id/imgview"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/breakfast"
        android:foreground="@drawable/blur"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@ id/names"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/amita_bold"
        android:text="Dinner"
        android:textColor="@color/white"
        android:textSize="40sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@ id/imgview"
        app:layout_constraintEnd_toEndOf="@ id/imgview"
        app:layout_constraintStart_toStartOf="@ id/imgview"
        app:layout_constraintTop_toTopOf="@ id/imgview"
        app:layout_constraintVertical_bias="0.3" />

    <TextView
        android:id="@ id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/amita"
        android:text="Choose your best dinner"
        android:textColor="@color/white"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/names" />

    <TextView
        android:id="@ id/discounts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/off_back"
        android:padding="2sp"
        android:text=" 20% OFF  "
        android:textColor="#FFFFFF"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@ id/imgview"
        app:layout_constraintStart_toStartOf="@ id/imgview"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.821" />
</androidx.constraintlayout.widget.ConstraintLayout>

DailyMealModel.java

public class DailyMealModel {
    int image;
    String name;
    String discount;
    String description;
    
    // getter setter and constructor
}

DailyMealFragment.java

public class DailyMealFragment extends Fragment {
    RecyclerView recyclerView;
    List<DailyMealModel> dailyMealModels;
    DailyMealAdapter dailyMealAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.daily_meal_fragment,container,false);

        recyclerView = root.findViewById(R.id.daily_meal_rec);

        dailyMealModels = new ArrayList<>();
        dailyMealModels.add(new DailyMealModel(R.drawable.breakfast,"Breakfast","30% OFF","Description Description"));
        dailyMealModels.add(new DailyMealModel(R.drawable.lunch,"Lunch","15% OFF","Description Description"));

        dailyMealAdapter = new DailyMealAdapter(getContext(),dailyMealModels);
        recyclerView.setAdapter(dailyMealAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        return root;
    }
}

DailyMealAdapter.java

public class DailyMealAdapter extends RecyclerView.Adapter<DailyMealAdapter.viewholder> {
    Context context;
    List<DailyMealModel> list;

    public DailyMealAdapter(Context context, List<DailyMealModel> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new viewholder(LayoutInflater.from(parent.getContext()).inflate(R.layout.daily_meal_ver,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull viewholder holder, int position) {
        holder.img.setImageResource(list.get(position).getImage());
        holder.name.setText(list.get(position).getName());
        holder.discount.setText(list.get(position).getDiscount());
        holder.description.setText(list.get(position).getDescription());

    }

    @Override
    public int getItemCount() {

        return list.size();
    }

    public class viewholder extends RecyclerView.ViewHolder {
        ImageView img;
        TextView name,discount,description;
        public viewholder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.imgview);
            name = itemView.findViewById(R.id.names);
            description = itemView.findViewById(R.id.desc);
            discount = itemView.findViewById(R.id.discounts);

        }
    }
}

Snapshot

enter image description here

CodePudding user response:

As your attached screenshot, please make sure all images and icons used in the app are lower sizes even loading dynamic images for that you can use tinypng.com or tinyjpg.com also, using pagination in RecyclerView will improve your app performance. and make sure JSON/XML list response is minimal.

  • Related