RestaurantTabFragment.java :
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.cbrecodingtest.R;
import com.example.cbrecodingtest.adapter.RestaurantsAdapter;
import com.example.cbrecodingtest.model.RestaurantsModel;
import com.example.cbrecodingtest.utils.Utils;
import java.util.ArrayList;
public class RestaurantTabFragment extends Fragment {
private RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
private RecyclerView.Adapter mRecyclerAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayList<RestaurantsModel> restaurantsList = (ArrayList<RestaurantsModel>) getArguments().getSerializable(Utils.RESTAURANT_LIST_KEY);
mRecyclerAdapter = new RestaurantsAdapter(restaurantsList, getActivity());
View rootView = inflater.inflate(R.layout.restaurant_tab_fragment, container, false);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView = rootView.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mRecyclerAdapter);
return rootView;
}
}
restaurant_tab_fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/restTabView">
<!-- A RecyclerView with some commonly used attributes -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
RestaurantsAdapter.java :
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.cbrecodingtest.R;
import com.example.cbrecodingtest.model.RestaurantsModel;
import com.example.cbrecodingtest.utils.Utils;
import com.example.cbrecodingtest.view.RestaurantDetailsScreen;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class RestaurantsAdapter extends RecyclerView.Adapter<RestaurantsAdapter.ViewHolder> {
private ArrayList<RestaurantsModel> mRestaurantList;
private Context context;
public RestaurantsAdapter(ArrayList<RestaurantsModel> mRestaurantList, Context context) {
this.mRestaurantList = mRestaurantList;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.restaurant_row, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
RestaurantsModel restaurantsModel = mRestaurantList.get(position);
holder.restName.setText(restaurantsModel.getName());
holder.restCategory.setText(restaurantsModel.getCategory());
holder.restGoodFor.setText(restaurantsModel.getGoodFor());
String openTime = restaurantsModel.getOpenTime();
String closeTime = restaurantsModel.getCloseTime();
if(Utils.isRestaurantOpen(openTime , closeTime)) {
holder.restOpenClose.setText("Open until "+closeTime);
holder.restOpenClose.setTextColor(context.getResources().getColor(R.color.green));
} else {
holder.restOpenClose.setText("Closed until tomorrow at "+openTime);
holder.restOpenClose.setTextColor(context.getResources().getColor(R.color.red));
}
Picasso.get().load(restaurantsModel.getImageUrl()).into(holder.imageDataView);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//listener for row click
/*FragmentTransaction ft = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
RestaurantDetailsScreen fragment2 = new RestaurantDetailsScreen();
Bundle bundle = new Bundle();
bundle.putSerializable(Utils.RESTAURANT_KEY , mRestaurantList.get(position));
fragment2.setArguments(bundle);
ft.replace(android.R.id.content, fragment2);
//ft.addToBackStack(null);
ft.commit();*/
RestaurantDetailsScreen fragment2 = new RestaurantDetailsScreen();
FragmentTransaction ft = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putSerializable(Utils.RESTAURANT_KEY , mRestaurantList.get(position));
fragment2.setArguments(bundle);
ft.replace(R.id.restTabView, fragment2);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
}
@Override
public int getItemCount() {
if (mRestaurantList != null) {
return mRestaurantList.size();
} else {
return 0;
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public final View view;
public final TextView restName;
public final TextView restCategory;
public final TextView restGoodFor;
public final TextView restOpenClose;
public final ImageView imageDataView;
public ViewHolder(View view) {
super(view);
this.view = view;
restName = view.findViewById(R.id.restName);
restCategory = view.findViewById(R.id.restCategory);
restGoodFor = view.findViewById(R.id.restGoodFor);
restOpenClose = view.findViewById(R.id.restOpenClose);
imageDataView = view.findViewById(R.id.mImageView);
}
}
}
restaurant_row.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:id="@+id/mViewRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:clickable="true"
android:focusable="true">
<ImageView
android:id="@+id/mImageView"
android:layout_width="120dp"
android:layout_height="90dp"
android:src="@mipmap/ic_launcher"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_margin="6dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="visible" />
<TextView
android:id="@+id/restName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="restName"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textSize="@dimen/nameTextSize"
android:textStyle="bold"
android:layout_marginLeft="4dp"
app:layout_constraintBottom_toBottomOf="@+id/mImageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/mImageView"
app:layout_constraintTop_toTopOf="@+id/mImageView"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/restCategory"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="@dimen/categoryTextSize"
android:textColor="@color/category"
android:text="restState "
app:layout_constraintLeft_toLeftOf="@+id/restName"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/restName" />
<TextView
android:id="@+id/restGoodFor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="@dimen/goodForTextSize"
android:textColor="@color/category"
android:text="restGoodFor"
app:layout_constraintLeft_toLeftOf="@+id/restCategory"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/restCategory" />
<TextView
android:id="@+id/restOpenClose"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="@dimen/openCloseTextSize"
android:text="restOpenClose "
app:layout_constraintLeft_toLeftOf="@+id/restGoodFor"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/restGoodFor" />
<View
android:id="@+id/lineView"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@color/black"
android:layout_marginTop="6dp"
app:layout_constraintLeft_toLeftOf="@+id/mImageView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mImageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

No comments:
Post a Comment