【发布时间】:2015-05-17 19:14:49
【问题描述】:
我正在尝试根据检索到的数据创建具有动态视图的自定义适配器。一切都设置好了,我可以区分即将到来的数据,甚至实现getItemViewType 和我自己的getItem 以相应地设置类型。
我有什么:
@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
PostItemView v = (PostItemView)inflater.inflate(R.layout.post_item_view, parent, false);
PostViewHolder postViewHolder = new PostViewHolder(v);
return postViewHolder;
}
现在考虑到getItemViewType 已经实现并且getViewTypeCount(不确定recyclerview 中是否真的需要)我认为它会选择正确的viewType
PostItemView:相关的 XML 布局部分如下所示:
...
<RelativeLayout
android:id="@+id/post_wrapper"
style="@style/Match">
<include
android:id="@+id/post_item_header_view"
layout="@layout/post_item_header_view"/>
<!--OLD XML includes this part and excludes FRAMELAYOUT-->
<!--<include-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:id="@+id/post_item_image_view"-->
<!--android:layout_below="@id/post_item_header_view"-->
<!--layout="@layout/post_item_image_view"-->
<!--android:layout_alignParentRight="true"-->
<!--android:layout_alignParentEnd="true" />-->
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/post_item_image_view"
android:layout_below="@id/post_item_header_view"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"></FrameLayout>
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/post_item_text_view"
android:layout_below="@id/post_item_image_view"
layout="@layout/post_item_text_view"/>
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/post_item_action_view"
android:layout_below="@id/post_item_text_view"
layout="@layout/post_item_action_view"/>
</RelativeLayout>
...
在我为PostItemView 膨胀后,如何在我迭代它们时为我拥有的每种类型(我有 6 个)在其子 FrameLayout 中膨胀特定布局?
还有其他方法吗?
【问题讨论】:
标签: java android android-recyclerview custom-adapter