Commit f1704608 authored by Inès EL HADRI's avatar Inès EL HADRI 💤

clean code

parent 7a6069e4
...@@ -18,7 +18,6 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> { ...@@ -18,7 +18,6 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
private Annonce[] mDataSet; private Annonce[] mDataSet;
private boolean mIsGrid; private boolean mIsGrid;
// BEGIN_INCLUDE(recyclerViewSampleViewHolder)
/** /**
* Provide a reference to the type of views that you are using (custom ViewHolder) * Provide a reference to the type of views that you are using (custom ViewHolder)
*/ */
...@@ -32,6 +31,7 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> { ...@@ -32,6 +31,7 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
v.setOnClickListener(new View.OnClickListener() { v.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// TODO : mettre la page de Donia
Log.d(TAG, "Element " + getAdapterPosition() + " clicked."); Log.d(TAG, "Element " + getAdapterPosition() + " clicked.");
} }
}); });
...@@ -46,23 +46,22 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> { ...@@ -46,23 +46,22 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
return adresseTextView; return adresseTextView;
} }
} }
// END_INCLUDE(recyclerViewSampleViewHolder)
/** /**
* Initialize the dataset of the Adapter. * Initialize the dataset of the Adapter.
* *
* @param dataSet String[] containing the data to populate views to be used by RecyclerView. * @param dataSet String[] containing the data to populate views to be used by RecyclerView.
* @param isGrid boolean = le layout actuel (grille ou linéaire)
*/ */
public HomeAdapter(Annonce[] dataSet, boolean isGrid) { public HomeAdapter(Annonce[] dataSet, boolean isGrid) {
mDataSet = dataSet; mDataSet = dataSet;
mIsGrid = isGrid; mIsGrid = isGrid;
} }
// BEGIN_INCLUDE(recyclerViewOnCreateViewHolder)
// Create new views (invoked by the layout manager) // Create new views (invoked by the layout manager)
@Override @Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// Create a new view. // Create a new view. - Layout en fonction de la sélection
View v; View v;
if (mIsGrid) { if (mIsGrid) {
v = LayoutInflater.from(viewGroup.getContext()) v = LayoutInflater.from(viewGroup.getContext())
...@@ -75,22 +74,19 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> { ...@@ -75,22 +74,19 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
return new ViewHolder(v); return new ViewHolder(v);
} }
// END_INCLUDE(recyclerViewOnCreateViewHolder)
// BEGIN_INCLUDE(recyclerViewOnBindViewHolder)
// Replace the contents of a view (invoked by the layout manager) // Replace the contents of a view (invoked by the layout manager)
@Override @Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) { public void onBindViewHolder(ViewHolder viewHolder, final int position) {
Log.d(TAG, "Element " + position + " set."); Log.d(TAG, "Element " + position + " set.");
// Get element from your dataset at this position and replace the contents of the view // Get element from dataset at this position and replace the contents of the view
// with that element // with that element
viewHolder.getTitreTextView().setText(mDataSet[position].getTitre()); viewHolder.getTitreTextView().setText(mDataSet[position].getTitre());
viewHolder.getAdresseTextView().setText(mDataSet[position].getAdresse()); viewHolder.getAdresseTextView().setText(mDataSet[position].getAdresse());
} }
// END_INCLUDE(recyclerViewOnBindViewHolder)
// Return the size of your dataset (invoked by the layout manager) // Return the size of dataset (invoked by the layout manager)
@Override @Override
public int getItemCount() { public int getItemCount() {
return mDataSet.length; return mDataSet.length;
......
...@@ -49,15 +49,14 @@ public class HomeFragment extends Fragment { ...@@ -49,15 +49,14 @@ public class HomeFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
// vue de base :
View rootView = inflater.inflate(R.layout.fragment_home, container, false); View rootView = inflater.inflate(R.layout.fragment_home, container, false);
rootView.setTag(TAG); rootView.setTag(TAG);
// BEGIN_INCLUDE(initializeRecyclerView) // Vue qui gère les éléments (annonces)
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view); mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
// LinearLayoutManager is used here, this will layout the elements in a similar fashion // pour gérer le layout (grille ou liste linéaire)
// to the way ListView would layout elements. The RecyclerView.LayoutManager defines how
// elements are laid out.
mLayoutManager = new LinearLayoutManager(getActivity()); mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
...@@ -69,11 +68,11 @@ public class HomeFragment extends Fragment { ...@@ -69,11 +68,11 @@ public class HomeFragment extends Fragment {
} }
setRecyclerViewLayoutManager(mCurrentLayoutManagerType); setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
// adapter pour gérer le visu de l'annonce
mAdapter = new HomeAdapter(mDataset, false); mAdapter = new HomeAdapter(mDataset, false);
// Set CustomAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter); mRecyclerView.setAdapter(mAdapter);
// END_INCLUDE(initializeRecyclerView)
// bouton pour changer de layout
mLinearLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.linear_layout_rb); mLinearLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.linear_layout_rb);
mLinearLayoutRadioButton.setOnClickListener(new View.OnClickListener() { mLinearLayoutRadioButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
...@@ -107,19 +106,22 @@ public class HomeFragment extends Fragment { ...@@ -107,19 +106,22 @@ public class HomeFragment extends Fragment {
.findFirstCompletelyVisibleItemPosition(); .findFirstCompletelyVisibleItemPosition();
} }
// on change de layout
switch (layoutManagerType) { switch (layoutManagerType) {
case GRID_LAYOUT_MANAGER: case GRID_LAYOUT_MANAGER:
mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT); mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER; mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
// redéfinition de l'adapter
mAdapter = new HomeAdapter(mDataset, true); mAdapter = new HomeAdapter(mDataset, true);
// Set CustomAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter); mRecyclerView.setAdapter(mAdapter);
break; break;
case LINEAR_LAYOUT_MANAGER: case LINEAR_LAYOUT_MANAGER:
mLayoutManager = new LinearLayoutManager(getActivity()); mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
// redéfinition de l'adapter
mAdapter = new HomeAdapter(mDataset, false); mAdapter = new HomeAdapter(mDataset, false);
// Set CustomAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter); mRecyclerView.setAdapter(mAdapter);
break; break;
default: default:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment