Reputation: 43
So I have cardview which contains a textview and linearlayout, and inside the linearlayout I want to add variable number of image views (These views will be holding app icons). How can I achieve this inside RecyclerView's onBindViewHolder.
Code for CardView (Here the linearlayout will hold imageviews):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="3dp"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground">
<TextView
android:id="@+id/profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="4sp"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="12sp" />
<LinearLayout
android:id="@+id/profile_app_icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true">
</LinearLayout>
<ToggleButton
android:id="@+id/main_page_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Code for Adapter (Which I am trying to implement):
public class ProfileListAdapter extends RecyclerView.Adapter<ProfileListAdapter.ProfileViewHolder>{
private List<ProfileContentsFull> pcf;
ProfileListAdapter(List<ProfileContentsFull> pcf) {
this.pcf = pcf;
}
@Override
public ProfileViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_main_page, parent, false);
return new ProfileViewHolder(v);
}
@Override
public void onBindViewHolder(ProfileViewHolder holder, int position) {
holder.profileName.setText(pcf.get(position).getProfileName());
holder.linearLayout.set
}
@Override
public int getItemCount() {
return 0;
}
public static class ProfileViewHolder extends RecyclerView.ViewHolder {
public TextView profileName;
public LinearLayout linearLayout;
public ToggleButton toggle;
public ProfileViewHolder(View itemView) {
super(itemView);
profileName = (TextView) itemView.findViewById(R.id.profile_name);
linearLayout = (LinearLayout) itemView.findViewById(R.id.profile_app_icons);
toggle = (ToggleButton) itemView.findViewById(R.id.main_page_toggle);
}
}
}
Layout containing the recyclerview:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.retr0spect.quit.social.media.addiction.MainActivity"
tools:showIn="@layout/app_bar_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_page_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</RelativeLayout>
Thanks in advance!
Upvotes: 1
Views: 3407
Reputation: 5711
You can add ImageView
dynamically into LinearLayout
like below.
ImageView imageView = new ImageView(holder.linearLayout.getContext());
holder.linearLayout.addView(imageView);
And you can add multipal ImageView
by putting above lines within Loop. ImageViews
are added only in vertical
or Horizontally
.
Upvotes: 2
Reputation: 3348
@Override
public void onBindViewHolder(ProfileViewHolder holder, int position) {
holder.profileName.setText(pcf.get(position).getProfileName());
LayoutInflater layoutinflater=(LayoutInflater)getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
holder. linearLayout.removeAllViews();
for (int j = 1; j < pcf.get(position).getImageArray().size(); j++) {
Log.d(TAG, "&& J :" + j);
// inflate the layout
convertView = layoutinflater.inflate(R.layout.adapter_images, null, false);
convertView.setId(j);
ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_image);
//set image here
holder.linearLayout.addView(convertView);
}
}
adapter_images.xml
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Upvotes: 0