Android:如何在没有滚动功能的情况下显示列表中的所有项目

问题描述 投票:0回答:1

我想显示类似RecyclerView的内容,但是大小会根据列表中的项目数动态变化。每个项目都显示在项目的View中。由于列表视图的高度设置为有多少个项目视图的总高度,因此视图中不需要进行任何滚动。

当前,我正在将RecyclerViewsetHasFixedSize = false结合使用,如您在此代码段中所见:

recyclerview.apply {
            layoutManager = LinearLayoutManager(rootView.context)
            adapter = PlanAdapter(dateItem.planItems)
            setHasFixedSize(false)
}

第一个之后没有显示任何项目。这是我实现它的示例屏幕:

enter image description here

蓝色高亮显示那些项目在本地存储中那里,但是我制作的动态RecyclerView没有显示。比为此创建一个整体RecyclerView更好的选择,对吧?

android listview kotlin android-recyclerview recycler-adapter
1个回答
2
投票

如果只想禁用回收站的滚动功能,则可以通过编程方式或通过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);

        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.