Connected locationFragment with recyclerview and added functionality
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package com.a1.nextlocation.data;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class Location {
|
||||
public class Location implements Parcelable {
|
||||
|
||||
@NonNull
|
||||
private String name;
|
||||
@@ -31,6 +34,25 @@ public class Location {
|
||||
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
|
||||
public String getName() {
|
||||
return name;
|
||||
@@ -85,4 +107,16 @@ public class Location {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,25 +3,54 @@ package com.a1.nextlocation.fragments;
|
||||
import android.os.Bundle;
|
||||
|
||||
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.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
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 {
|
||||
private RecyclerView locationRecyclerView;
|
||||
private LocationAdapter locationAdapter;
|
||||
private RecyclerView.LayoutManager layoutManager;
|
||||
private List<Location> locationList;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
return inflater.inflate(R.layout.fragment_location, container, false);
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
|
||||
View view = 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;
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.Locati
|
||||
|
||||
private Context appContext;
|
||||
private List<Location> locationList;
|
||||
private CouponAdapter.OnItemClickListener clickListener;
|
||||
private OnItemClickListener clickListener;
|
||||
|
||||
public interface OnItemClickListener {
|
||||
void onItemClick(int clickedPosition);
|
||||
@@ -31,6 +31,7 @@ public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.Locati
|
||||
public LocationViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
this.locationName = itemView.findViewById(R.id.location_name);
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@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.locationList = location;
|
||||
this.clickListener = listener;
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
android:id="@+id/locationRecyclerView"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
|
||||
Reference in New Issue
Block a user