The most efficient way to show details in Android
up vote
2
down vote
favorite
What is the most efficient way to show model field information in an activity? Just like below picture for example, do they manually make each fields using View groups(LinearLayout, RelativeLayout, ConstraintLayout), or do they produce this using a RecyclerView? If this was made using a RecyclerView, how is this achievable?
UPDATE(If we're using RecyclerView):
-Thank you for the replies, I think you guys are right,I should use LinearLayout if I want to achieve just like in picture sample. But in my case, I have a lot of data, 50 fields, and I dont want to manually make 1 ViewGroup for each field. With my example, I would only be using 4 fields for my User Model, and these items are purely for viewing, no other viewtypes and no specific functions.
User Model fields:
-Name
-Age
-Gender
-Address
I want each user field as an item in a recyclerview, but since my UserModel is not a List, I wouldnt be able to fed it to the RecyclerView. Below picture is what I want to achieve. Sorry If my problem is a bit ambiguous, I'm having a hard time explaining my problem.
android android-studio layout android-recyclerview
New contributor
|
show 9 more comments
up vote
2
down vote
favorite
What is the most efficient way to show model field information in an activity? Just like below picture for example, do they manually make each fields using View groups(LinearLayout, RelativeLayout, ConstraintLayout), or do they produce this using a RecyclerView? If this was made using a RecyclerView, how is this achievable?
UPDATE(If we're using RecyclerView):
-Thank you for the replies, I think you guys are right,I should use LinearLayout if I want to achieve just like in picture sample. But in my case, I have a lot of data, 50 fields, and I dont want to manually make 1 ViewGroup for each field. With my example, I would only be using 4 fields for my User Model, and these items are purely for viewing, no other viewtypes and no specific functions.
User Model fields:
-Name
-Age
-Gender
-Address
I want each user field as an item in a recyclerview, but since my UserModel is not a List, I wouldnt be able to fed it to the RecyclerView. Below picture is what I want to achieve. Sorry If my problem is a bit ambiguous, I'm having a hard time explaining my problem.
android android-studio layout android-recyclerview
New contributor
imho RecyclerView
– Vadim Eksler
yesterday
It is possible, usingRecyclerView
with differentviewType
but i would recommend doing it as nested views.
– jake
yesterday
If you want to create it using RecyclerView with multiple ViewType. Here is the relevant link stackoverflow.com/questions/26245139/…
– Rohit Singh
yesterday
@RohitSingh Thank you for the response, that would solve partially my problem, the problem is what kind of List<Object> would I feed the recyclerview? Can I loop inside a model with each fields? For example, I have 3 fields in a model [Name, Age, Address], how to make 1 model field for 1 recyclerview item? Thank you.
– Boomiyaah
yesterday
1
Welcome to StackOverflow! In my opinion a RecyclerView with n different viewTypes would be overkill. It's not like you'll have more than 20 items even when your model increases a LOT and each will have a different function (call a number, start navigation, etc.). I'd go with a manual implementation of each element inside a LinearLayout.
– Oliver Metz
yesterday
|
show 9 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
What is the most efficient way to show model field information in an activity? Just like below picture for example, do they manually make each fields using View groups(LinearLayout, RelativeLayout, ConstraintLayout), or do they produce this using a RecyclerView? If this was made using a RecyclerView, how is this achievable?
UPDATE(If we're using RecyclerView):
-Thank you for the replies, I think you guys are right,I should use LinearLayout if I want to achieve just like in picture sample. But in my case, I have a lot of data, 50 fields, and I dont want to manually make 1 ViewGroup for each field. With my example, I would only be using 4 fields for my User Model, and these items are purely for viewing, no other viewtypes and no specific functions.
User Model fields:
-Name
-Age
-Gender
-Address
I want each user field as an item in a recyclerview, but since my UserModel is not a List, I wouldnt be able to fed it to the RecyclerView. Below picture is what I want to achieve. Sorry If my problem is a bit ambiguous, I'm having a hard time explaining my problem.
android android-studio layout android-recyclerview
New contributor
What is the most efficient way to show model field information in an activity? Just like below picture for example, do they manually make each fields using View groups(LinearLayout, RelativeLayout, ConstraintLayout), or do they produce this using a RecyclerView? If this was made using a RecyclerView, how is this achievable?
UPDATE(If we're using RecyclerView):
-Thank you for the replies, I think you guys are right,I should use LinearLayout if I want to achieve just like in picture sample. But in my case, I have a lot of data, 50 fields, and I dont want to manually make 1 ViewGroup for each field. With my example, I would only be using 4 fields for my User Model, and these items are purely for viewing, no other viewtypes and no specific functions.
User Model fields:
-Name
-Age
-Gender
-Address
I want each user field as an item in a recyclerview, but since my UserModel is not a List, I wouldnt be able to fed it to the RecyclerView. Below picture is what I want to achieve. Sorry If my problem is a bit ambiguous, I'm having a hard time explaining my problem.
android android-studio layout android-recyclerview
android android-studio layout android-recyclerview
New contributor
New contributor
edited yesterday
New contributor
asked yesterday
Boomiyaah
134
134
New contributor
New contributor
imho RecyclerView
– Vadim Eksler
yesterday
It is possible, usingRecyclerView
with differentviewType
but i would recommend doing it as nested views.
– jake
yesterday
If you want to create it using RecyclerView with multiple ViewType. Here is the relevant link stackoverflow.com/questions/26245139/…
– Rohit Singh
yesterday
@RohitSingh Thank you for the response, that would solve partially my problem, the problem is what kind of List<Object> would I feed the recyclerview? Can I loop inside a model with each fields? For example, I have 3 fields in a model [Name, Age, Address], how to make 1 model field for 1 recyclerview item? Thank you.
– Boomiyaah
yesterday
1
Welcome to StackOverflow! In my opinion a RecyclerView with n different viewTypes would be overkill. It's not like you'll have more than 20 items even when your model increases a LOT and each will have a different function (call a number, start navigation, etc.). I'd go with a manual implementation of each element inside a LinearLayout.
– Oliver Metz
yesterday
|
show 9 more comments
imho RecyclerView
– Vadim Eksler
yesterday
It is possible, usingRecyclerView
with differentviewType
but i would recommend doing it as nested views.
– jake
yesterday
If you want to create it using RecyclerView with multiple ViewType. Here is the relevant link stackoverflow.com/questions/26245139/…
– Rohit Singh
yesterday
@RohitSingh Thank you for the response, that would solve partially my problem, the problem is what kind of List<Object> would I feed the recyclerview? Can I loop inside a model with each fields? For example, I have 3 fields in a model [Name, Age, Address], how to make 1 model field for 1 recyclerview item? Thank you.
– Boomiyaah
yesterday
1
Welcome to StackOverflow! In my opinion a RecyclerView with n different viewTypes would be overkill. It's not like you'll have more than 20 items even when your model increases a LOT and each will have a different function (call a number, start navigation, etc.). I'd go with a manual implementation of each element inside a LinearLayout.
– Oliver Metz
yesterday
imho RecyclerView
– Vadim Eksler
yesterday
imho RecyclerView
– Vadim Eksler
yesterday
It is possible, using
RecyclerView
with different viewType
but i would recommend doing it as nested views.– jake
yesterday
It is possible, using
RecyclerView
with different viewType
but i would recommend doing it as nested views.– jake
yesterday
If you want to create it using RecyclerView with multiple ViewType. Here is the relevant link stackoverflow.com/questions/26245139/…
– Rohit Singh
yesterday
If you want to create it using RecyclerView with multiple ViewType. Here is the relevant link stackoverflow.com/questions/26245139/…
– Rohit Singh
yesterday
@RohitSingh Thank you for the response, that would solve partially my problem, the problem is what kind of List<Object> would I feed the recyclerview? Can I loop inside a model with each fields? For example, I have 3 fields in a model [Name, Age, Address], how to make 1 model field for 1 recyclerview item? Thank you.
– Boomiyaah
yesterday
@RohitSingh Thank you for the response, that would solve partially my problem, the problem is what kind of List<Object> would I feed the recyclerview? Can I loop inside a model with each fields? For example, I have 3 fields in a model [Name, Age, Address], how to make 1 model field for 1 recyclerview item? Thank you.
– Boomiyaah
yesterday
1
1
Welcome to StackOverflow! In my opinion a RecyclerView with n different viewTypes would be overkill. It's not like you'll have more than 20 items even when your model increases a LOT and each will have a different function (call a number, start navigation, etc.). I'd go with a manual implementation of each element inside a LinearLayout.
– Oliver Metz
yesterday
Welcome to StackOverflow! In my opinion a RecyclerView with n different viewTypes would be overkill. It's not like you'll have more than 20 items even when your model increases a LOT and each will have a different function (call a number, start navigation, etc.). I'd go with a manual implementation of each element inside a LinearLayout.
– Oliver Metz
yesterday
|
show 9 more comments
2 Answers
2
active
oldest
votes
up vote
-1
down vote
accepted
I would use a RecyclerView
with a custom adapter to achieve this result, depending on how advanced each item can be. If it's simple interaction and design, eg icon
, text
and maybe a click listener
then it's good enough.
If it's anything more advanced, many different types of actions that can be preformed by each button, then maybe you could just go with a LinearLayout
and generate each button with their specific action.
If you would rather generate the buttons, scroll to the bottom for an example
Assuming that you want to use a RecyclerView
, then it could look something like this:
You could have an adapter item looking something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/padding_view_small">
<ImageView
android:id="@+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_person_black_24dp"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/placeholder"
android:textColor="@color/black"
android:layout_marginStart="@dimen/padding_view_small"
android:layout_marginLeft="@dimen/padding_view_small"/>
</LinearLayout>
Your adapter
public class MyAdapter extends RecyclerView.Adapter
{
private Context _context;
private List<MyObject> _items;
public void setItems(List<MyObject> items)
{
this._items = items;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
_context = parent.getContext();
return new MyAdapter.ItemViewHolder(parent);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position)
{
final MyAdapter.ItemViewHolder viewHolder = (MyAdapter.ItemViewHolder) holder;
final MyObject item = _items.get(position);
viewHolder._icon.setImageResource(item.getIcon());
viewHolder._name.setText(item.getText());
}
@Override
public int getItemCount()
{
return _items != null ? _items.size() : 0;
}
private static class ItemViewHolder extends RecyclerView.ViewHolder
{
private ImageView _icon;
private TextView _text;
private ItemViewHolder(ViewGroup parent)
{
super(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout, parent, false));
this._icon = itemView.findViewById(R.id.icon);
this._text = itemView.findViewById(R.id.text);
}
}
}
MyObject class for storing information about each button
public class MyObject
{
private int _icon;
private String _text;
public MyObject(int icon, String text)
{
this._icon = icon;
this._text = text;
}
public int getIcon()
{
return this._icon;
}
public int getText()
{
return this._text;
}
}
Then you could also maybe directly pass a ClickListener
to the MyObject
, or you can also add an additional variable called Type
so that you know what to do when a user clicks a specific type.
Generating buttons without a RecyclerView
You can have a basic LinearLayout
view holder that's gonna hold all the views.
<LinearLayout
android:id="@+id/action_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
Method for generating views
private View generateView(int icon, String text)
{
LayoutInflater inflater = LayoutInflater.from(_context);
View view = inflater.inflate(R.layout.adapter_layout, null);
ImageView icon = view.findViewById(R.id.icon);
icon.setImageResource(icon);
TextView text = view.findViewById(R.id.text);
text.setText(text);
return view;
}
Dynamically adding new views
View showDialogView = generateView(ContextCompat.getDrawable(_context, R.drawable.test), "Show dialog");
//Here you can set a click listener to what should happen when you click the item
showDialogView.setOnClickListener....
linearLayout.addView(showDialogView);
1
Why the downvote? It's a valid suggestion.
– Ezzy
yesterday
In regard for your recyclerview suggestion, that wasnt the answer that I was looking for, I have updated my question, and sorry for the ambiguous question. I actually like your second answer, I did not know that you can generate views without using a RecyclerView. If there will be no answer regarding the recyclerview, I will actually take this as an alternative for my problem. Thank you, you really have a good suggestion. Sorry I cant still upvote, i will mark this as an answer If the recyclerview cant be done.
– Boomiyaah
yesterday
add a comment |
up vote
0
down vote
As you have explained me in the comments.
- You have a Model class USER which has 50 fields.
- And if the items look like as they are in Screenshot.
If this is the case, it can be easily solved with Simple RecyclerView without even using ViewType
Your ItemView has a logo, a title, and a description.
1) Create a new Model for items of the List.
class ListItem{
int logo;
int title;
int desc;
// write getter and setter
}
2) Populate list?
Get data from User
Model and convert it to ListItem
Model
Lets say that first item is Name of the user.
Do like this
ListItem username = new ListItem();
username.setTitle("NAME");
username .setLogo(R.id.userLogo);
username.setDesc(user.name);
Lets say that second item is Age of the user
Do like this
ListItem age = new ListItem();
age.setTitle("AGE");
username .setLogo(R.id.ageLogo);
username.setDesc(user.age);
3) Feed the itemList in the Adapter
List<ListItem> myList = new List<ListItem>();
myList.add(username);
myList.add(age);
YourAdapter adapter = new Adapter(context,myList);
recyclerView.setAdapter(adapter);
Hope this helps. Peace:)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
accepted
I would use a RecyclerView
with a custom adapter to achieve this result, depending on how advanced each item can be. If it's simple interaction and design, eg icon
, text
and maybe a click listener
then it's good enough.
If it's anything more advanced, many different types of actions that can be preformed by each button, then maybe you could just go with a LinearLayout
and generate each button with their specific action.
If you would rather generate the buttons, scroll to the bottom for an example
Assuming that you want to use a RecyclerView
, then it could look something like this:
You could have an adapter item looking something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/padding_view_small">
<ImageView
android:id="@+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_person_black_24dp"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/placeholder"
android:textColor="@color/black"
android:layout_marginStart="@dimen/padding_view_small"
android:layout_marginLeft="@dimen/padding_view_small"/>
</LinearLayout>
Your adapter
public class MyAdapter extends RecyclerView.Adapter
{
private Context _context;
private List<MyObject> _items;
public void setItems(List<MyObject> items)
{
this._items = items;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
_context = parent.getContext();
return new MyAdapter.ItemViewHolder(parent);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position)
{
final MyAdapter.ItemViewHolder viewHolder = (MyAdapter.ItemViewHolder) holder;
final MyObject item = _items.get(position);
viewHolder._icon.setImageResource(item.getIcon());
viewHolder._name.setText(item.getText());
}
@Override
public int getItemCount()
{
return _items != null ? _items.size() : 0;
}
private static class ItemViewHolder extends RecyclerView.ViewHolder
{
private ImageView _icon;
private TextView _text;
private ItemViewHolder(ViewGroup parent)
{
super(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout, parent, false));
this._icon = itemView.findViewById(R.id.icon);
this._text = itemView.findViewById(R.id.text);
}
}
}
MyObject class for storing information about each button
public class MyObject
{
private int _icon;
private String _text;
public MyObject(int icon, String text)
{
this._icon = icon;
this._text = text;
}
public int getIcon()
{
return this._icon;
}
public int getText()
{
return this._text;
}
}
Then you could also maybe directly pass a ClickListener
to the MyObject
, or you can also add an additional variable called Type
so that you know what to do when a user clicks a specific type.
Generating buttons without a RecyclerView
You can have a basic LinearLayout
view holder that's gonna hold all the views.
<LinearLayout
android:id="@+id/action_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
Method for generating views
private View generateView(int icon, String text)
{
LayoutInflater inflater = LayoutInflater.from(_context);
View view = inflater.inflate(R.layout.adapter_layout, null);
ImageView icon = view.findViewById(R.id.icon);
icon.setImageResource(icon);
TextView text = view.findViewById(R.id.text);
text.setText(text);
return view;
}
Dynamically adding new views
View showDialogView = generateView(ContextCompat.getDrawable(_context, R.drawable.test), "Show dialog");
//Here you can set a click listener to what should happen when you click the item
showDialogView.setOnClickListener....
linearLayout.addView(showDialogView);
1
Why the downvote? It's a valid suggestion.
– Ezzy
yesterday
In regard for your recyclerview suggestion, that wasnt the answer that I was looking for, I have updated my question, and sorry for the ambiguous question. I actually like your second answer, I did not know that you can generate views without using a RecyclerView. If there will be no answer regarding the recyclerview, I will actually take this as an alternative for my problem. Thank you, you really have a good suggestion. Sorry I cant still upvote, i will mark this as an answer If the recyclerview cant be done.
– Boomiyaah
yesterday
add a comment |
up vote
-1
down vote
accepted
I would use a RecyclerView
with a custom adapter to achieve this result, depending on how advanced each item can be. If it's simple interaction and design, eg icon
, text
and maybe a click listener
then it's good enough.
If it's anything more advanced, many different types of actions that can be preformed by each button, then maybe you could just go with a LinearLayout
and generate each button with their specific action.
If you would rather generate the buttons, scroll to the bottom for an example
Assuming that you want to use a RecyclerView
, then it could look something like this:
You could have an adapter item looking something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/padding_view_small">
<ImageView
android:id="@+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_person_black_24dp"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/placeholder"
android:textColor="@color/black"
android:layout_marginStart="@dimen/padding_view_small"
android:layout_marginLeft="@dimen/padding_view_small"/>
</LinearLayout>
Your adapter
public class MyAdapter extends RecyclerView.Adapter
{
private Context _context;
private List<MyObject> _items;
public void setItems(List<MyObject> items)
{
this._items = items;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
_context = parent.getContext();
return new MyAdapter.ItemViewHolder(parent);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position)
{
final MyAdapter.ItemViewHolder viewHolder = (MyAdapter.ItemViewHolder) holder;
final MyObject item = _items.get(position);
viewHolder._icon.setImageResource(item.getIcon());
viewHolder._name.setText(item.getText());
}
@Override
public int getItemCount()
{
return _items != null ? _items.size() : 0;
}
private static class ItemViewHolder extends RecyclerView.ViewHolder
{
private ImageView _icon;
private TextView _text;
private ItemViewHolder(ViewGroup parent)
{
super(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout, parent, false));
this._icon = itemView.findViewById(R.id.icon);
this._text = itemView.findViewById(R.id.text);
}
}
}
MyObject class for storing information about each button
public class MyObject
{
private int _icon;
private String _text;
public MyObject(int icon, String text)
{
this._icon = icon;
this._text = text;
}
public int getIcon()
{
return this._icon;
}
public int getText()
{
return this._text;
}
}
Then you could also maybe directly pass a ClickListener
to the MyObject
, or you can also add an additional variable called Type
so that you know what to do when a user clicks a specific type.
Generating buttons without a RecyclerView
You can have a basic LinearLayout
view holder that's gonna hold all the views.
<LinearLayout
android:id="@+id/action_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
Method for generating views
private View generateView(int icon, String text)
{
LayoutInflater inflater = LayoutInflater.from(_context);
View view = inflater.inflate(R.layout.adapter_layout, null);
ImageView icon = view.findViewById(R.id.icon);
icon.setImageResource(icon);
TextView text = view.findViewById(R.id.text);
text.setText(text);
return view;
}
Dynamically adding new views
View showDialogView = generateView(ContextCompat.getDrawable(_context, R.drawable.test), "Show dialog");
//Here you can set a click listener to what should happen when you click the item
showDialogView.setOnClickListener....
linearLayout.addView(showDialogView);
1
Why the downvote? It's a valid suggestion.
– Ezzy
yesterday
In regard for your recyclerview suggestion, that wasnt the answer that I was looking for, I have updated my question, and sorry for the ambiguous question. I actually like your second answer, I did not know that you can generate views without using a RecyclerView. If there will be no answer regarding the recyclerview, I will actually take this as an alternative for my problem. Thank you, you really have a good suggestion. Sorry I cant still upvote, i will mark this as an answer If the recyclerview cant be done.
– Boomiyaah
yesterday
add a comment |
up vote
-1
down vote
accepted
up vote
-1
down vote
accepted
I would use a RecyclerView
with a custom adapter to achieve this result, depending on how advanced each item can be. If it's simple interaction and design, eg icon
, text
and maybe a click listener
then it's good enough.
If it's anything more advanced, many different types of actions that can be preformed by each button, then maybe you could just go with a LinearLayout
and generate each button with their specific action.
If you would rather generate the buttons, scroll to the bottom for an example
Assuming that you want to use a RecyclerView
, then it could look something like this:
You could have an adapter item looking something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/padding_view_small">
<ImageView
android:id="@+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_person_black_24dp"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/placeholder"
android:textColor="@color/black"
android:layout_marginStart="@dimen/padding_view_small"
android:layout_marginLeft="@dimen/padding_view_small"/>
</LinearLayout>
Your adapter
public class MyAdapter extends RecyclerView.Adapter
{
private Context _context;
private List<MyObject> _items;
public void setItems(List<MyObject> items)
{
this._items = items;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
_context = parent.getContext();
return new MyAdapter.ItemViewHolder(parent);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position)
{
final MyAdapter.ItemViewHolder viewHolder = (MyAdapter.ItemViewHolder) holder;
final MyObject item = _items.get(position);
viewHolder._icon.setImageResource(item.getIcon());
viewHolder._name.setText(item.getText());
}
@Override
public int getItemCount()
{
return _items != null ? _items.size() : 0;
}
private static class ItemViewHolder extends RecyclerView.ViewHolder
{
private ImageView _icon;
private TextView _text;
private ItemViewHolder(ViewGroup parent)
{
super(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout, parent, false));
this._icon = itemView.findViewById(R.id.icon);
this._text = itemView.findViewById(R.id.text);
}
}
}
MyObject class for storing information about each button
public class MyObject
{
private int _icon;
private String _text;
public MyObject(int icon, String text)
{
this._icon = icon;
this._text = text;
}
public int getIcon()
{
return this._icon;
}
public int getText()
{
return this._text;
}
}
Then you could also maybe directly pass a ClickListener
to the MyObject
, or you can also add an additional variable called Type
so that you know what to do when a user clicks a specific type.
Generating buttons without a RecyclerView
You can have a basic LinearLayout
view holder that's gonna hold all the views.
<LinearLayout
android:id="@+id/action_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
Method for generating views
private View generateView(int icon, String text)
{
LayoutInflater inflater = LayoutInflater.from(_context);
View view = inflater.inflate(R.layout.adapter_layout, null);
ImageView icon = view.findViewById(R.id.icon);
icon.setImageResource(icon);
TextView text = view.findViewById(R.id.text);
text.setText(text);
return view;
}
Dynamically adding new views
View showDialogView = generateView(ContextCompat.getDrawable(_context, R.drawable.test), "Show dialog");
//Here you can set a click listener to what should happen when you click the item
showDialogView.setOnClickListener....
linearLayout.addView(showDialogView);
I would use a RecyclerView
with a custom adapter to achieve this result, depending on how advanced each item can be. If it's simple interaction and design, eg icon
, text
and maybe a click listener
then it's good enough.
If it's anything more advanced, many different types of actions that can be preformed by each button, then maybe you could just go with a LinearLayout
and generate each button with their specific action.
If you would rather generate the buttons, scroll to the bottom for an example
Assuming that you want to use a RecyclerView
, then it could look something like this:
You could have an adapter item looking something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/padding_view_small">
<ImageView
android:id="@+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_person_black_24dp"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/placeholder"
android:textColor="@color/black"
android:layout_marginStart="@dimen/padding_view_small"
android:layout_marginLeft="@dimen/padding_view_small"/>
</LinearLayout>
Your adapter
public class MyAdapter extends RecyclerView.Adapter
{
private Context _context;
private List<MyObject> _items;
public void setItems(List<MyObject> items)
{
this._items = items;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
_context = parent.getContext();
return new MyAdapter.ItemViewHolder(parent);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position)
{
final MyAdapter.ItemViewHolder viewHolder = (MyAdapter.ItemViewHolder) holder;
final MyObject item = _items.get(position);
viewHolder._icon.setImageResource(item.getIcon());
viewHolder._name.setText(item.getText());
}
@Override
public int getItemCount()
{
return _items != null ? _items.size() : 0;
}
private static class ItemViewHolder extends RecyclerView.ViewHolder
{
private ImageView _icon;
private TextView _text;
private ItemViewHolder(ViewGroup parent)
{
super(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout, parent, false));
this._icon = itemView.findViewById(R.id.icon);
this._text = itemView.findViewById(R.id.text);
}
}
}
MyObject class for storing information about each button
public class MyObject
{
private int _icon;
private String _text;
public MyObject(int icon, String text)
{
this._icon = icon;
this._text = text;
}
public int getIcon()
{
return this._icon;
}
public int getText()
{
return this._text;
}
}
Then you could also maybe directly pass a ClickListener
to the MyObject
, or you can also add an additional variable called Type
so that you know what to do when a user clicks a specific type.
Generating buttons without a RecyclerView
You can have a basic LinearLayout
view holder that's gonna hold all the views.
<LinearLayout
android:id="@+id/action_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
Method for generating views
private View generateView(int icon, String text)
{
LayoutInflater inflater = LayoutInflater.from(_context);
View view = inflater.inflate(R.layout.adapter_layout, null);
ImageView icon = view.findViewById(R.id.icon);
icon.setImageResource(icon);
TextView text = view.findViewById(R.id.text);
text.setText(text);
return view;
}
Dynamically adding new views
View showDialogView = generateView(ContextCompat.getDrawable(_context, R.drawable.test), "Show dialog");
//Here you can set a click listener to what should happen when you click the item
showDialogView.setOnClickListener....
linearLayout.addView(showDialogView);
edited yesterday
answered yesterday
Ezzy
5822521
5822521
1
Why the downvote? It's a valid suggestion.
– Ezzy
yesterday
In regard for your recyclerview suggestion, that wasnt the answer that I was looking for, I have updated my question, and sorry for the ambiguous question. I actually like your second answer, I did not know that you can generate views without using a RecyclerView. If there will be no answer regarding the recyclerview, I will actually take this as an alternative for my problem. Thank you, you really have a good suggestion. Sorry I cant still upvote, i will mark this as an answer If the recyclerview cant be done.
– Boomiyaah
yesterday
add a comment |
1
Why the downvote? It's a valid suggestion.
– Ezzy
yesterday
In regard for your recyclerview suggestion, that wasnt the answer that I was looking for, I have updated my question, and sorry for the ambiguous question. I actually like your second answer, I did not know that you can generate views without using a RecyclerView. If there will be no answer regarding the recyclerview, I will actually take this as an alternative for my problem. Thank you, you really have a good suggestion. Sorry I cant still upvote, i will mark this as an answer If the recyclerview cant be done.
– Boomiyaah
yesterday
1
1
Why the downvote? It's a valid suggestion.
– Ezzy
yesterday
Why the downvote? It's a valid suggestion.
– Ezzy
yesterday
In regard for your recyclerview suggestion, that wasnt the answer that I was looking for, I have updated my question, and sorry for the ambiguous question. I actually like your second answer, I did not know that you can generate views without using a RecyclerView. If there will be no answer regarding the recyclerview, I will actually take this as an alternative for my problem. Thank you, you really have a good suggestion. Sorry I cant still upvote, i will mark this as an answer If the recyclerview cant be done.
– Boomiyaah
yesterday
In regard for your recyclerview suggestion, that wasnt the answer that I was looking for, I have updated my question, and sorry for the ambiguous question. I actually like your second answer, I did not know that you can generate views without using a RecyclerView. If there will be no answer regarding the recyclerview, I will actually take this as an alternative for my problem. Thank you, you really have a good suggestion. Sorry I cant still upvote, i will mark this as an answer If the recyclerview cant be done.
– Boomiyaah
yesterday
add a comment |
up vote
0
down vote
As you have explained me in the comments.
- You have a Model class USER which has 50 fields.
- And if the items look like as they are in Screenshot.
If this is the case, it can be easily solved with Simple RecyclerView without even using ViewType
Your ItemView has a logo, a title, and a description.
1) Create a new Model for items of the List.
class ListItem{
int logo;
int title;
int desc;
// write getter and setter
}
2) Populate list?
Get data from User
Model and convert it to ListItem
Model
Lets say that first item is Name of the user.
Do like this
ListItem username = new ListItem();
username.setTitle("NAME");
username .setLogo(R.id.userLogo);
username.setDesc(user.name);
Lets say that second item is Age of the user
Do like this
ListItem age = new ListItem();
age.setTitle("AGE");
username .setLogo(R.id.ageLogo);
username.setDesc(user.age);
3) Feed the itemList in the Adapter
List<ListItem> myList = new List<ListItem>();
myList.add(username);
myList.add(age);
YourAdapter adapter = new Adapter(context,myList);
recyclerView.setAdapter(adapter);
Hope this helps. Peace:)
add a comment |
up vote
0
down vote
As you have explained me in the comments.
- You have a Model class USER which has 50 fields.
- And if the items look like as they are in Screenshot.
If this is the case, it can be easily solved with Simple RecyclerView without even using ViewType
Your ItemView has a logo, a title, and a description.
1) Create a new Model for items of the List.
class ListItem{
int logo;
int title;
int desc;
// write getter and setter
}
2) Populate list?
Get data from User
Model and convert it to ListItem
Model
Lets say that first item is Name of the user.
Do like this
ListItem username = new ListItem();
username.setTitle("NAME");
username .setLogo(R.id.userLogo);
username.setDesc(user.name);
Lets say that second item is Age of the user
Do like this
ListItem age = new ListItem();
age.setTitle("AGE");
username .setLogo(R.id.ageLogo);
username.setDesc(user.age);
3) Feed the itemList in the Adapter
List<ListItem> myList = new List<ListItem>();
myList.add(username);
myList.add(age);
YourAdapter adapter = new Adapter(context,myList);
recyclerView.setAdapter(adapter);
Hope this helps. Peace:)
add a comment |
up vote
0
down vote
up vote
0
down vote
As you have explained me in the comments.
- You have a Model class USER which has 50 fields.
- And if the items look like as they are in Screenshot.
If this is the case, it can be easily solved with Simple RecyclerView without even using ViewType
Your ItemView has a logo, a title, and a description.
1) Create a new Model for items of the List.
class ListItem{
int logo;
int title;
int desc;
// write getter and setter
}
2) Populate list?
Get data from User
Model and convert it to ListItem
Model
Lets say that first item is Name of the user.
Do like this
ListItem username = new ListItem();
username.setTitle("NAME");
username .setLogo(R.id.userLogo);
username.setDesc(user.name);
Lets say that second item is Age of the user
Do like this
ListItem age = new ListItem();
age.setTitle("AGE");
username .setLogo(R.id.ageLogo);
username.setDesc(user.age);
3) Feed the itemList in the Adapter
List<ListItem> myList = new List<ListItem>();
myList.add(username);
myList.add(age);
YourAdapter adapter = new Adapter(context,myList);
recyclerView.setAdapter(adapter);
Hope this helps. Peace:)
As you have explained me in the comments.
- You have a Model class USER which has 50 fields.
- And if the items look like as they are in Screenshot.
If this is the case, it can be easily solved with Simple RecyclerView without even using ViewType
Your ItemView has a logo, a title, and a description.
1) Create a new Model for items of the List.
class ListItem{
int logo;
int title;
int desc;
// write getter and setter
}
2) Populate list?
Get data from User
Model and convert it to ListItem
Model
Lets say that first item is Name of the user.
Do like this
ListItem username = new ListItem();
username.setTitle("NAME");
username .setLogo(R.id.userLogo);
username.setDesc(user.name);
Lets say that second item is Age of the user
Do like this
ListItem age = new ListItem();
age.setTitle("AGE");
username .setLogo(R.id.ageLogo);
username.setDesc(user.age);
3) Feed the itemList in the Adapter
List<ListItem> myList = new List<ListItem>();
myList.add(username);
myList.add(age);
YourAdapter adapter = new Adapter(context,myList);
recyclerView.setAdapter(adapter);
Hope this helps. Peace:)
edited yesterday
answered yesterday
Rohit Singh
2,16811930
2,16811930
add a comment |
add a comment |
Boomiyaah is a new contributor. Be nice, and check out our Code of Conduct.
Boomiyaah is a new contributor. Be nice, and check out our Code of Conduct.
Boomiyaah is a new contributor. Be nice, and check out our Code of Conduct.
Boomiyaah is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53202821%2fthe-most-efficient-way-to-show-details-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
imho RecyclerView
– Vadim Eksler
yesterday
It is possible, using
RecyclerView
with differentviewType
but i would recommend doing it as nested views.– jake
yesterday
If you want to create it using RecyclerView with multiple ViewType. Here is the relevant link stackoverflow.com/questions/26245139/…
– Rohit Singh
yesterday
@RohitSingh Thank you for the response, that would solve partially my problem, the problem is what kind of List<Object> would I feed the recyclerview? Can I loop inside a model with each fields? For example, I have 3 fields in a model [Name, Age, Address], how to make 1 model field for 1 recyclerview item? Thank you.
– Boomiyaah
yesterday
1
Welcome to StackOverflow! In my opinion a RecyclerView with n different viewTypes would be overkill. It's not like you'll have more than 20 items even when your model increases a LOT and each will have a different function (call a number, start navigation, etc.). I'd go with a manual implementation of each element inside a LinearLayout.
– Oliver Metz
yesterday