Commit 9d8f582b authored by Inès EL HADRI's avatar Inès EL HADRI 💤

Merge branch 'listview_home'

# Conflicts:
#	app/src/main/java/com/example/tpleboncoin/ui/home/HomeFragment.java
parents 9d66263d 0ea13bb9
package com.example.tpleboncoin.ui.home;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.example.tpleboncoin.R;
import com.example.tpleboncoin.models.Annonce;
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
private static final String TAG = "CustomAdapter";
private Annonce[] mDataSet;
private boolean mIsGrid;
/**
* Provide a reference to the type of views that you are using (custom ViewHolder)
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView titreTextView;
private final TextView adresseTextView;
public ViewHolder(View v) {
super(v);
// Define click listener for the ViewHolder's View.
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO : mettre la page de Donia
Log.d(TAG, "Element " + getAdapterPosition() + " clicked.");
}
});
titreTextView = (TextView) v.findViewById(R.id.titre_annonce);
adresseTextView = (TextView) v.findViewById(R.id.adresse_annonce);
}
public TextView getTitreTextView() {
return titreTextView;
}
public TextView getAdresseTextView() {
return adresseTextView;
}
}
/**
* Initialize the dataset of the Adapter.
*
* @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) {
mDataSet = dataSet;
mIsGrid = isGrid;
}
// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// Create a new view. - Layout en fonction de la sélection
View v;
if (mIsGrid) {
v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.annonces_grid, viewGroup, false);
}
else {
v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.annonces_liste, viewGroup, false);
}
return new ViewHolder(v);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
Log.d(TAG, "Element " + position + " set.");
// Get element from dataset at this position and replace the contents of the view
// with that element
viewHolder.getTitreTextView().setText(mDataSet[position].getTitre());
viewHolder.getAdresseTextView().setText(mDataSet[position].getAdresse());
}
// Return the size of dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataSet.length;
}
}
......@@ -7,29 +7,91 @@ import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Button;
import android.widget.RadioButton;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.tpleboncoin.databinding.FragmentHomeBinding;
import com.example.tpleboncoin.R;
import com.example.tpleboncoin.models.Annonce;
import com.example.tpleboncoin.databinding.FragmentHomeBinding;
public class HomeFragment extends Fragment {
private FragmentHomeBinding binding;
private static final String TAG = "RecyclerViewFragment";
private static final String KEY_LAYOUT_MANAGER = "layoutManager";
private static final int SPAN_COUNT = 2;
private static final int DATASET_COUNT = 60;
private enum LayoutManagerType {
GRID_LAYOUT_MANAGER,
LINEAR_LAYOUT_MANAGER
}
protected LayoutManagerType mCurrentLayoutManagerType;
protected RadioButton mLinearLayoutRadioButton;
protected RadioButton mGridLayoutRadioButton;
protected RecyclerView mRecyclerView;
protected HomeAdapter mAdapter;
protected RecyclerView.LayoutManager mLayoutManager;
protected Annonce[] mDataset;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize dataset, this data would usually come from a local content provider or
// remote server.
initDataset();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// vue de base :
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
rootView.setTag(TAG);
// Vue qui gère les éléments (annonces)
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
// pour gérer le layout (grille ou liste linéaire)
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
if (savedInstanceState != null) {
// Restore saved layout manager type.
mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
.getSerializable(KEY_LAYOUT_MANAGER);
}
setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
// adapter pour gérer le visu de l'annonce
mAdapter = new HomeAdapter(mDataset, false);
mRecyclerView.setAdapter(mAdapter);
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
// bouton pour changer de layout
mLinearLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.linear_layout_rb);
mLinearLayoutRadioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setRecyclerViewLayoutManager(LayoutManagerType.LINEAR_LAYOUT_MANAGER);
}
});
final TextView textView = binding.textHome;
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
mGridLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.grid_layout_rb);
mGridLayoutRadioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setRecyclerViewLayoutManager(LayoutManagerType.GRID_LAYOUT_MANAGER);
}
});
// On récupère l'annonce qui vient d'être créée s'il y en a une
if(getArguments() != null && getArguments().getParcelable("nouvelleAnnonce") != null){
......@@ -37,12 +99,66 @@ public class HomeFragment extends Fragment {
Log.d("HomeFragment", newAnnonce.getTitre());
}
return root;
return rootView;
}
/**
* Set RecyclerView's LayoutManager to the one given.
*
* @param layoutManagerType Type of layout manager to switch to.
*/
public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
int scrollPosition = 0;
// If a layout manager has already been set, get current scroll position.
if (mRecyclerView.getLayoutManager() != null) {
scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
.findFirstCompletelyVisibleItemPosition();
}
// on change de layout
switch (layoutManagerType) {
case GRID_LAYOUT_MANAGER:
mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
// redéfinition de l'adapter
mAdapter = new HomeAdapter(mDataset, true);
mRecyclerView.setAdapter(mAdapter);
break;
case LINEAR_LAYOUT_MANAGER:
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
// redéfinition de l'adapter
mAdapter = new HomeAdapter(mDataset, false);
mRecyclerView.setAdapter(mAdapter);
break;
default:
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
}
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.scrollToPosition(scrollPosition);
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save currently selected layout manager.
savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType);
super.onSaveInstanceState(savedInstanceState);
}
/**
* Generates Strings for RecyclerView's adapter. This data would usually come
* from a local content provider or remote server.
*/
private void initDataset() {
mDataset = new Annonce[DATASET_COUNT];
for (int i = 0; i < DATASET_COUNT; i++) {
Annonce annonce = new Annonce("Titre " + i, "Adresse " + i, 0, 12.99);
mDataset[i] = annonce;
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="50dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/adresse_annonce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/prix_annonce"
android:text="Adresse, 59000 Douai" />
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/titre_annonce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:fontFamily="sans-serif-medium"
android:text="Titre annonce de test"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<TextView
android:id="@+id/prix_annonce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/titre_annonce"
android:layout_weight="1"
android:fontFamily="sans-serif-black"
android:text="12.99€"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</RelativeLayout>
</FrameLayout>
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:padding="8dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/ic_launcher_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/titre_annonce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginBottom="4dp"
android:fontFamily="sans-serif-medium"
android:text="Titre annonce de test"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintBottom_toTopOf="@+id/adresse_annonce"
app:layout_constraintStart_toEndOf="@+id/imageView" />
<TextView
android:id="@+id/adresse_annonce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adresse, 59000 Douai"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/titre_annonce" />
<TextView
android:id="@+id/prix_annonce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:fontFamily="sans-serif-black"
android:text="12.99€"
android:textAlignment="viewEnd"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?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"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
android:orientation="vertical">
<TextView
android:id="@+id/text_home"
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
android:checkedButton="@+id/linear_layout_rb"
android:gravity="center_horizontal"
android:orientation="horizontal">
<RadioButton
android:id="@+id/linear_layout_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/linear_layout_manager" />
<RadioButton
android:id="@+id/grid_layout_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grid_layout_manager" />
</RadioGroup>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
\ No newline at end of file
......@@ -3,4 +3,7 @@
<string name="title_home">Accueil</string>
<string name="title_add">Ajout</string>
<string name="title_profile">Profil</string>
<string name="text_annonce">test</string>
<string name="linear_layout_manager">text</string>
<string name="grid_layout_manager">text</string>
</resources>
\ No newline at end of file
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