我正在尝试使用多个Feed项实现Newsfeed类型的布局。 新闻源项目在折叠时会有一定的布局,当点击该项目时,此布局将被“爆炸”版本替换。
我通过使用自定义项的ListView来完成此操作。自定义项目XML布局文件有一个ViewStub,这是我用来来回更改布局的。 但是,现在,我想将布局“迁移”到RecyclerView,并遵循ViewHolder设计模式。 后者是我首先尝试过的,而且我遇到了各种各样的问题。
我的方法如下:
获取对折叠布局(events_list_item_content)和展开布局(events_list_item_selected_content)的引用;
获取对要设置为ViewStub布局(view_stub_layout)的简单布局资源文件的引用。
获取ViewStub参考,设置其布局(view_stub_layout)膨胀,并将折叠的布局视图添加到此布局(首次创建Feed时,其所有项目都将折叠)。
(初始化后,单击某个项目时)从ViewStubLayout中删除上一个视图(布局),添加其他类型的布局。
这是我的自定义适配器类:
public class FeedRecyclerAdapter extends BaseAdapter {
public class ViewHolder {
View inflatedViewStub1;
ViewStub viewStub;
LinearLayout viewStubLayout;
LinearLayout listItemContent, listItemContentSelected;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final FeedItem item = feedItems.get(position);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.events_list_item_content_new_container, parent, false);
View view = null;
view = inflater.inflate(R.layout.events_list_item_content, null);
viewHolder.listItemContent = (LinearLayout) view.findViewById(R.id.events_list_item_content);
view = inflater.inflate(R.layout.events_list_item_selected_content, null);
viewHolder.listItemContentSelected = (LinearLayout) view.findViewById(R.id.events_list_item_content_selected);
view = inflater.inflate(R.layout.view_stub_layout, null);
viewHolder.viewStubLayout = (LinearLayout) view.findViewById(R.id.view_stub_layout);
viewHolder.viewStub = (ViewStub) convertView.findViewById(R.id.list_item_feed);
(viewHolder.viewStubLayout).addView(viewHolder.listItemContent);
viewHolder.viewStub.setLayoutResource(R.layout.view_stub_layout);
viewHolder.inflatedViewStub1 = viewHolder.viewStub.inflate();
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();
if (item.getExploded()) {
viewHolder.viewStubLayout.removeAllViews();
viewHolder.viewStubLayout.addView(viewHolder.listItemContentSelected);
} else {
viewHolder.viewStubLayout.removeAllViews();
viewHolder.viewStubLayout.addView(viewHolder.listItemContent);
}
return convertView;
}
但是,在测试时,应该显示新闻源的页面是空白的。
答案 0 :(得分:0)
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
stub.setLayoutResource(layoutId);
stub.inflate(); // inflate 1st layout
ll.removeAllViews(); // remove previous view, add 2nd layout
ll.addView(LayoutInflater.from(context).inflate(secondLayoutId, ll, false));