0

I want to display something like a RecyclerView but the size is dynamically changing depending on how many items there are in the list. Each item is shown within the item's View. There shouldn't need to be any scrolling in the view as the list view's height is set to the total height of how many item views there are.

Currently, I am using a RecyclerView with the setHasFixedSize = false as you can see in this snippet:

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

which doesn't show any item after the first one. Here's an example screen where I've implemented it:

enter image description here

the blue highlights show that those items are there in the local storage, but is not shown by the dynamic RecyclerView that I made. There is a better option than creating a whole RecyclerView for this, though, right?

3
  • Hi @fawwaz, not sure if understood your question so can I please get some clarifications on it? You don't want the whole page to be able to scroll or just the list? You don't want to use recyclerview at all for this issue? Is the size of the list that is dynamically changing or each item on it? Could you know before hand how many items will be displayed? Are they many or just a few? Could you not just have a linear layout to display the items? Even if the number of items is not always the same you could draw this layout programmatically based on the amount of views you have. Thank you. Commented Feb 4, 2019 at 4:28
  • I don't want the list view to scroll, but the page can scroll if needed. I just thought making a recyclerview is not the best solution performance wise. Well the list starts at 0 items and the user can make a new item from the app. How can I draw this layout programmatically? Is it just adding a linear layout everytime an item is created? Thanks Commented Feb 4, 2019 at 4:34
  • 1
    Owh, I see. Well, recyclerViews are usually pretty good with performance but you can disable their scrolling and leaving this just for the parent of the view through android:nestedScrollingEnabled="false". Yes, you can draw the layout programmatically by having a custom that adds new items to the layout programmatically. If I find a sample I'll post it here. Commented Feb 4, 2019 at 4:40

1 Answer 1

2

If just disabling the scrolling of the recycler is what you want to achieve, you can do this by calling android:nestedScrollingEnabled="false" either programmatically or through the XML for the recyclerView.

Now, if you don't want to use recyclerview for that, you could maybe use a custom view that extends you layout such as 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);
    }

Called this way

for (Allergen a : recipe.getAllergen()) {

                View v1 = new AllergenView(mActivity, Utils.toString(a.getAllergen()));
            holder.linearLayoutHorizontal.addView(v1);

        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

set equal weight to views added to linear layout so that views share the vertical space equally

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.