Connected locationFragment with recyclerview and added functionality

This commit is contained in:
Bart
2020-12-17 13:46:41 +01:00
parent dbc10014f9
commit 6c923d27b2
4 changed files with 73 additions and 8 deletions

View File

@@ -1,11 +1,14 @@
package com.a1.nextlocation.data; package com.a1.nextlocation.data;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class Location { public class Location implements Parcelable {
@NonNull @NonNull
private String name; private String name;
@@ -31,6 +34,25 @@ public class Location {
this(name,getStringFromCoordinates(latCoord,longCoord),description,imageUrl); this(name,getStringFromCoordinates(latCoord,longCoord),description,imageUrl);
} }
protected Location(Parcel in) {
name = in.readString();
coordinates = in.readString();
description = in.readString();
imageUrl = in.readString();
}
public static final Creator<Location> CREATOR = new Creator<Location>() {
@Override
public Location createFromParcel(Parcel in) {
return new Location(in);
}
@Override
public Location[] newArray(int size) {
return new Location[size];
}
};
@NotNull @NotNull
public String getName() { public String getName() {
return name; return name;
@@ -85,4 +107,16 @@ public class Location {
return lat1 + "," + long1; return lat1 + "," + long1;
} }
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(coordinates);
dest.writeString(description);
dest.writeString(imageUrl);
}
} }

View File

@@ -3,25 +3,54 @@ package com.a1.nextlocation.fragments;
import android.os.Bundle; import android.os.Bundle;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
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 com.a1.nextlocation.R; import com.a1.nextlocation.R;
import com.a1.nextlocation.data.Location;
import com.a1.nextlocation.recyclerview.CouponAdapter;
import com.a1.nextlocation.recyclerview.LocationAdapter;
import com.a1.nextlocation.recyclerview.LocationListManager;
import java.util.ArrayList;
import java.util.List;
public class LocationFragment extends Fragment { public class LocationFragment extends Fragment {
private RecyclerView locationRecyclerView;
private LocationAdapter locationAdapter;
private RecyclerView.LayoutManager layoutManager;
private List<Location> locationList;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle savedInstanceState) {
// Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_location, container, false);
return inflater.inflate(R.layout.fragment_location, container, false);
locationRecyclerView = view.findViewById(R.id.locationRecyclerView);
locationRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this.getContext());
LocationListManager locationListManager = new LocationListManager(this.getContext());
locationListManager.load();
locationList = locationListManager.getLocationList();
locationAdapter = new LocationAdapter(this.getContext(), locationList, clickedPosition -> {
LocationDetailFragment locationDetailFragment = new LocationDetailFragment();
Bundle locationBundle = new Bundle();
locationBundle.putParcelable("location", locationList.get(clickedPosition));
locationDetailFragment.setArguments(locationBundle);
((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, locationDetailFragment).addToBackStack(null).commit();
});
locationRecyclerView.setLayoutManager(layoutManager);
locationRecyclerView.setAdapter(locationAdapter);
return view;
} }
} }

View File

@@ -18,7 +18,7 @@ public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.Locati
private Context appContext; private Context appContext;
private List<Location> locationList; private List<Location> locationList;
private CouponAdapter.OnItemClickListener clickListener; private OnItemClickListener clickListener;
public interface OnItemClickListener { public interface OnItemClickListener {
void onItemClick(int clickedPosition); void onItemClick(int clickedPosition);
@@ -31,6 +31,7 @@ public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.Locati
public LocationViewHolder(@NonNull View itemView) { public LocationViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
this.locationName = itemView.findViewById(R.id.location_name); this.locationName = itemView.findViewById(R.id.location_name);
itemView.setOnClickListener(this);
} }
@Override @Override
@@ -43,7 +44,7 @@ public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.Locati
} }
} }
public LocationAdapter(Context context, List<Location> location, CouponAdapter.OnItemClickListener listener){ public LocationAdapter(Context context, List<Location> location, OnItemClickListener listener){
this.appContext = context; this.appContext = context;
this.locationList = location; this.locationList = location;
this.clickListener = listener; this.clickListener = listener;

View File

@@ -31,6 +31,7 @@
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:id="@+id/locationRecyclerView"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"