【发布时间】:2017-07-06 12:07:01
【问题描述】:
所以我有一个包含文本视图和线性布局的卡片视图,并且在线性布局内我想添加可变数量的图像视图(这些视图将包含应用程序图标)。如何在 RecyclerView 的 onBindViewHolder 中实现这一点。
CardView 的代码(这里的线性布局将保存图像视图):
<?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>
提前致谢!
【问题讨论】:
-
能否用您的代码更新您的原始帖子?
-
进行了修改。
-
RecyclerView 在你的布局中在哪里?
-
在我看来,您可以使用 RecyclerView 来保存 ImageView,而不是保存 ImageViews 的 LinearLayout。
-
RecyclerView 持有所有卡片视图。好的,那么我该如何继续让 RecyclerView 在适配器内保存图像视图?
标签: android dynamic android-recyclerview