所以我的cardview包含textview和linearlayout,而在linearlayout里面我想添加可变数量的图像视图(这些视图将保存app图标)。如何在RecyclerView的onBindViewHolder中实现这一点。
CardView代码(此处linearlayout将保存图像视图):
<?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>
适配器代码(我正在尝试实现):
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);
}
}
}
包含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>
提前致谢!
答案 0 :(得分:1)
您可以动态地将ImageView
添加到LinearLayout
,如下所示。
ImageView imageView = new ImageView(holder.linearLayout.getContext());
holder.linearLayout.addView(imageView);
你可以通过在Loop中放置上面的行来添加multipal ImageView
。 ImageViews
仅在vertical
或Horizontally
中添加。
答案 1 :(得分:0)
@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"
/>