我想显示类似RecyclerView
的内容,但是大小会根据列表中的项目数动态变化。每个项目都显示在项目的View
中。由于列表视图的高度设置为有多少个项目视图的总高度,因此视图中不需要进行任何滚动。
当前,我正在将RecyclerView
与setHasFixedSize = false
结合使用,如您在此代码段中所见:
recyclerview.apply {
layoutManager = LinearLayoutManager(rootView.context)
adapter = PlanAdapter(dateItem.planItems)
setHasFixedSize(false)
}
第一个之后没有显示任何项目。这是我实现它的示例屏幕:
蓝色高亮显示那些项目在本地存储中那里,但是我制作的动态RecyclerView没有显示。比为此创建一个整体RecyclerView
更好的选择,对吧?
如果只想禁用回收站的滚动功能,则可以通过编程方式或通过XML为recyclerView调用android:nestedScrollingEnabled="false"
来实现。
现在,如果您不想为此使用recyclerview,则可以使用可扩展布局的自定义视图,例如in
public class AllergenView extends LinearLayout {
private View rootView;
private ImageView icon;
private String allergenTag;
Activity mActivity;
public AllergenView(Activity activity, String allergenTag) {
super(activity);
this.mActivity = activity;
this.allergenTag = allergenTag;
init(activity);
}
public AllergenView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
private void init(Context context) {
rootView = inflate(context, R.layout.view_allergen_icon, this);
// Bind views
icon = (ImageView) findViewById(R.id.img_icon);
}
这样叫
for (Allergen a : recipe.getAllergen()) {
View v1 = new AllergenView(mActivity, Utils.toString(a.getAllergen()));
holder.linearLayoutHorizontal.addView(v1);
}
}