Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uv-amse-android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Lucas NAURY
uv-amse-android
Commits
e3c50500
Commit
e3c50500
authored
Feb 16, 2024
by
Inès EL HADRI
💤
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add list of annonces
parent
d577b0a3
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
256 additions
and
33 deletions
+256
-33
HomeAdapter.java
...ain/java/com/example/tpleboncoin/ui/home/HomeAdapter.java
+88
-0
HomeFragment.java
...in/java/com/example/tpleboncoin/ui/home/HomeFragment.java
+126
-17
annonces_liste.xml
app/src/main/res/layout/annonces_liste.xml
+11
-0
fragment_home.xml
app/src/main/res/layout/fragment_home.xml
+28
-16
strings.xml
app/src/main/res/values/strings.xml
+3
-0
No files found.
app/src/main/java/com/example/tpleboncoin/ui/home/HomeAdapter.java
0 → 100644
View file @
e3c50500
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
;
// BEGIN_INCLUDE(recyclerViewSampleViewHolder)
/**
* Provide a reference to the type of views that you are using (custom ViewHolder)
*/
public
static
class
ViewHolder
extends
RecyclerView
.
ViewHolder
{
private
final
TextView
textView
;
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
)
{
Log
.
d
(
TAG
,
"Element "
+
getAdapterPosition
()
+
" clicked."
);
}
});
textView
=
(
TextView
)
v
.
findViewById
(
R
.
id
.
text_annonce
);
}
public
TextView
getTextView
()
{
return
textView
;
}
}
// END_INCLUDE(recyclerViewSampleViewHolder)
/**
* Initialize the dataset of the Adapter.
*
* @param dataSet String[] containing the data to populate views to be used by RecyclerView.
*/
public
HomeAdapter
(
Annonce
[]
dataSet
)
{
mDataSet
=
dataSet
;
}
// BEGIN_INCLUDE(recyclerViewOnCreateViewHolder)
// Create new views (invoked by the layout manager)
@Override
public
ViewHolder
onCreateViewHolder
(
ViewGroup
viewGroup
,
int
viewType
)
{
// Create a new view.
View
v
=
LayoutInflater
.
from
(
viewGroup
.
getContext
())
.
inflate
(
R
.
layout
.
annonces_liste
,
viewGroup
,
false
);
return
new
ViewHolder
(
v
);
}
// END_INCLUDE(recyclerViewOnCreateViewHolder)
// BEGIN_INCLUDE(recyclerViewOnBindViewHolder)
// 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 your dataset at this position and replace the contents of the view
// with that element
viewHolder
.
getTextView
().
setText
(
mDataSet
[
position
].
getTitre
());
}
// END_INCLUDE(recyclerViewOnBindViewHolder)
// Return the size of your dataset (invoked by the layout manager)
@Override
public
int
getItemCount
()
{
return
mDataSet
.
length
;
}
}
app/src/main/java/com/example/tpleboncoin/ui/home/HomeFragment.java
View file @
e3c50500
...
...
@@ -4,34 +4,143 @@ import android.os.Bundle;
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
;
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
;
public
View
onCreateView
(
@NonNull
LayoutInflater
inflater
,
ViewGroup
container
,
Bundle
savedInstanceState
)
{
HomeViewModel
homeViewModel
=
new
ViewModelProvider
(
this
).
get
(
HomeViewModel
.
class
);
private
enum
LayoutManagerType
{
GRID_LAYOUT_MANAGER
,
LINEAR_LAYOUT_MANAGER
}
protected
LayoutManagerType
mCurrentLayoutManagerType
;
protected
RadioButton
mLinearLayoutRadioButton
;
protected
RadioButton
mGridLayoutRadioButton
;
binding
=
FragmentHomeBinding
.
inflate
(
inflater
,
container
,
false
);
View
root
=
binding
.
getRoot
();
protected
RecyclerView
mRecyclerView
;
protected
HomeAdapter
mAdapter
;
protected
RecyclerView
.
LayoutManager
mLayoutManager
;
protected
Annonce
[]
mDataset
;
final
TextView
textView
=
binding
.
textHome
;
homeViewModel
.
getText
().
observe
(
getViewLifecycleOwner
(),
textView:
:
setText
);
return
root
;
@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
void
onDestroyView
()
{
super
.
onDestroyView
();
binding
=
null
;
public
View
onCreateView
(
LayoutInflater
inflater
,
ViewGroup
container
,
Bundle
savedInstanceState
)
{
View
rootView
=
inflater
.
inflate
(
R
.
layout
.
fragment_home
,
container
,
false
);
rootView
.
setTag
(
TAG
);
// BEGIN_INCLUDE(initializeRecyclerView)
mRecyclerView
=
(
RecyclerView
)
rootView
.
findViewById
(
R
.
id
.
recycler_view
);
// LinearLayoutManager is used here, this will layout the elements in a similar fashion
// to the way ListView would layout elements. The RecyclerView.LayoutManager defines how
// elements are laid out.
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
);
mAdapter
=
new
HomeAdapter
(
mDataset
);
// Set CustomAdapter as the adapter for RecyclerView.
mRecyclerView
.
setAdapter
(
mAdapter
);
// END_INCLUDE(initializeRecyclerView)
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
);
}
});
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
);
}
});
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
();
}
switch
(
layoutManagerType
)
{
case
GRID_LAYOUT_MANAGER:
mLayoutManager
=
new
GridLayoutManager
(
getActivity
(),
SPAN_COUNT
);
mCurrentLayoutManagerType
=
LayoutManagerType
.
GRID_LAYOUT_MANAGER
;
break
;
case
LINEAR_LAYOUT_MANAGER:
mLayoutManager
=
new
LinearLayoutManager
(
getActivity
());
mCurrentLayoutManagerType
=
LayoutManagerType
.
LINEAR_LAYOUT_MANAGER
;
break
;
default
:
mLayoutManager
=
new
LinearLayoutManager
(
getActivity
());
mCurrentLayoutManagerType
=
LayoutManagerType
.
LINEAR_LAYOUT_MANAGER
;
}
mRecyclerView
.
setLayoutManager
(
mLayoutManager
);
mRecyclerView
.
scrollToPosition
(
scrollPosition
);
}
@Override
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"
,
"Adresse"
,
0
);
mDataset
[
i
]
=
annonce
;
}
}
}
\ No newline at end of file
app/src/main/res/layout/annonces_liste.xml
0 → 100644
View file @
e3c50500
<FrameLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:gravity=
"center_vertical"
>
<TextView
android:id=
"@+id/text_annonce"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"@string/text_annonce"
/>
</FrameLayout>
app/src/main/res/layout/fragment_home.xml
View file @
e3c50500
<?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
app/src/main/res/values/strings.xml
View file @
e3c50500
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment