Route works and is clickable

This commit is contained in:
RemoMeijer
2020-12-17 14:35:05 +01:00
parent aba26f3211
commit 1f7cd49a90
6 changed files with 105 additions and 19 deletions

View File

@@ -1,5 +1,8 @@
package com.a1.nextlocation.data;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import org.jetbrains.annotations.NotNull;
@@ -7,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class Route {
public class Route implements Parcelable {
@NonNull
@@ -29,6 +32,25 @@ public class Route {
}
protected Route(Parcel in) {
this.name = in.readString();
this.locations = in.createTypedArrayList(Location.CREATOR);
this.totalDistance = in.readFloat();
this.totalTime = in.readInt();
}
public static final Creator<Route> CREATOR = new Creator<Route>() {
@Override
public Route createFromParcel(Parcel in) {
return new Route(in);
}
@Override
public Route[] newArray(int size) {
return new Route[size];
}
};
public void addLocation(Location location) {
this.locations.add(location);
}
@@ -69,4 +91,16 @@ public class Route {
this.totalTime = totalTime;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeTypedList(locations);
parcel.writeFloat(totalDistance);
parcel.writeInt(totalTime);
}
}

View File

@@ -36,21 +36,24 @@ public class LocationFragment extends Fragment {
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 -> {
this.locationRecyclerView = view.findViewById(R.id.locationRecyclerView);
this.locationRecyclerView.setHasFixedSize(true);
this.layoutManager = new LinearLayoutManager(this.getContext());
LocationListManager.INSTANCE.setContext(this.getContext());
LocationListManager.INSTANCE.load();
this.locationList = LocationListManager.INSTANCE.getLocationList();
this.locationAdapter = new LocationAdapter(this.getContext(), this.locationList, clickedPosition -> {
LocationDetailFragment locationDetailFragment = new LocationDetailFragment();
Bundle locationBundle = new Bundle();
locationBundle.putParcelable("location", locationList.get(clickedPosition));
locationBundle.putParcelable("location", this.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);
this.locationRecyclerView.setLayoutManager(this.layoutManager);
this.locationRecyclerView.setAdapter(this.locationAdapter);
return view;
}
}

View File

@@ -3,15 +3,29 @@ 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.Route;
import com.a1.nextlocation.recyclerview.RouteAdapter;
import com.a1.nextlocation.recyclerview.RouteListManager;
import java.util.ArrayList;
import java.util.List;
public class RouteFragment extends Fragment {
private RecyclerView routeRecyclerView;
private RecyclerView.LayoutManager layoutManager;
private List<Route> routeList;
private RouteAdapter routeAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -19,9 +33,27 @@ public class RouteFragment extends Fragment {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_route, container, false);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_route, container, false);
this.routeRecyclerView = view.findViewById(R.id.routeRecyclerView);
this.routeRecyclerView.setHasFixedSize(true);
this.layoutManager = new LinearLayoutManager(this.getContext());
RouteListManager.INSTANCE.setContext(this.getContext());
RouteListManager.INSTANCE.load();
this.routeList = RouteListManager.INSTANCE.getRouteList();
this.routeAdapter = new RouteAdapter(this.getContext(), this.routeList, clickedPosition -> {
RouteDetailFragment routeDetailFragment = new RouteDetailFragment();
Bundle routeBundle = new Bundle();
routeBundle.putParcelable("route", this.routeList.get(clickedPosition));
routeDetailFragment.setArguments(routeBundle);
((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, routeDetailFragment).addToBackStack(null).commit();
});
this.routeRecyclerView.setLayoutManager(this.layoutManager);
this.routeRecyclerView.setAdapter(this.routeAdapter);
return view;
}
}

View File

@@ -1,12 +1,17 @@
package com.a1.nextlocation.recyclerview;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.a1.nextlocation.R;
import com.a1.nextlocation.data.Location;
import com.a1.nextlocation.data.Route;
import java.util.List;
@@ -23,14 +28,23 @@ public class RouteAdapter extends RecyclerView.Adapter<RouteAdapter.RouteViewHol
class RouteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView routeName;
public RouteViewHolder(@NonNull View itemView) {
super(itemView);
this.routeName = itemView.findViewById(R.id.route_name);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
clickListener.onItemClick(getAdapterPosition());
}
public void setTextViewText(String text){
this.routeName = itemView.findViewById(R.id.route_name);
this.routeName.setText(text);
}
}
public RouteAdapter(Context context, List<Route> route, CouponAdapter.OnItemClickListener listener){
@@ -42,12 +56,15 @@ public class RouteAdapter extends RecyclerView.Adapter<RouteAdapter.RouteViewHol
@NonNull
@Override
public RouteAdapter.RouteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.route_item, parent, false);
RouteAdapter.RouteViewHolder viewHolder = new RouteAdapter.RouteViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RouteAdapter.RouteViewHolder holder, int position) {
Route route = routeList.get(position);
holder.setTextViewText(route.getName());
}
@Override

View File

@@ -28,7 +28,7 @@
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/routeListRV"
android:id="@+id/routeRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/primaryColour"

View File

@@ -17,7 +17,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/routeName"
android:id="@+id/route_name"
android:text="test text"
android:gravity="center"
android:textSize="20dp"