Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependent on size of first item in adapter #20

Open
crearo opened this issue May 10, 2017 · 8 comments
Open

Dependent on size of first item in adapter #20

crearo opened this issue May 10, 2017 · 8 comments

Comments

@crearo
Copy link

crearo commented May 10, 2017

Problem : all list items take up the size of the first item in the adapter list - this is a terrible assumption for a generic library like this one!

My adapter has a header of toolbar height ?android:attr/actionBarSize and then a list variable length images - something like facebook's newsfeed. I ended up with a vertical stack of lots of items, all of the same toolbar height.

Is ther an easy fix to this? I haven't gone through the code yet, hence not aware of how much effort this would require.

@crearo
Copy link
Author

crearo commented May 12, 2017

I've identified where it needs to be fixed, and why it isn't all that easy to fix.
In the DiscreteScrollLayoutManager there are two variables, private int childHalfWidth, childHalfHeight;. These are used in initChildDimensions to measure and set the values. Various other methods then use these (constant) cached values to perform the LayoutManager logic.

So, what I tried doing was in place of using these fixed ones, I created a hashmap like so private HashMap<Integer, Point> childDimensions;, and a helper function to measure and cache these values.

    private Point getChildDimension(RecyclerView.Recycler recycler, int position) {
        if (childDimensions.containsKey(position)) return childDimensions.get(position);
        View viewToMeasure = recycler.getViewForPosition(position);
        measureChildWithMargins(viewToMeasure, 0, 0);

        int childViewWidth = getDecoratedMeasuredWidth(viewToMeasure) / 2;
        int childViewHeight = getDecoratedMeasuredHeight(viewToMeasure) / 2;
        Point dimension = new Point(childViewWidth, childViewHeight);
        childDimensions.put(position, dimension);
        return dimension;
    }

Sadly, this doesn't work. The offsets are still messed up and the continuous measurements (even though cached) slow down the app greatly.

Since the library developer is more versed in the LayoutManager logic, I'm certain this wouldn't be too difficult a task for them. Highly suggest this is looked into asap!

@karanatwal
Copy link

Hi @crearo sadly i am so much disapointed with this library .. after wasting whole my day now i saw your issue reported ...now i have very less time to deliver project and i can't even fix ths issue by myself in less time ...do you know any other library with same functionality ,,help would be really apreciated

@yarolegovich
Copy link
Owner

The main problem here is that we should somehow measure all views and cache their dimensions, and then use the data to calculate scroll distance.
The majority of code which does layout should also be rewritten.
Unfortunately it's very hard for me to find time for these changes.

@bailandi
Copy link

bailandi commented Feb 3, 2018

You can change code like this, which makes the result of the measurement the largest of all Items, but it still doesn't solve the fixed width or height problem.

 private void initChildDimensions(RecyclerView.Recycler recycler) {
        int itemCount = getItemCount();
        int childViewWidth = 0;
        int childViewHeight = 0;
        View firstView = null;
        for (int i = 0; i < itemCount; i++) {
            View viewToMeasure = recycler.getViewForPosition(i);
            if (i == 0) {
                addView(viewToMeasure);
                firstView = viewToMeasure;
            }
            measureChildWithMargins(viewToMeasure, 0, 0);

            childViewHeight = Math.max(getDecoratedMeasuredHeight(viewToMeasure), childViewHeight);
            childViewWidth = Math.max(getDecoratedMeasuredWidth(viewToMeasure), childViewWidth);
        }

        childHalfWidth = childViewWidth / 2;
        childHalfHeight = childViewHeight / 2;

        scrollToChangeCurrent = orientationHelper.getDistanceToChangeCurrent(
                childViewWidth,
                childViewHeight);

        extraLayoutSpace = scrollToChangeCurrent * offscreenItems;

        detachAndScrapView(firstView, recycler);
    }

@BejanAlina
Copy link

@yarolegovich is this issue still not fixed ? It seems that the width and height of all items are stilll dependent on the first item size

@india2sarthak
Copy link

india2sarthak commented Aug 27, 2020

@yarolegovich Any updates on this? The library works great. However, not being able to set the height and width of the individual items is a huge turn-off

@Meta-Ben
Copy link

@yarolegovich any updates ? because this is a very painfull problem I think

@javalue
Copy link

javalue commented Apr 8, 2021

try to set different holder, and background be transparent. but when horizontal scroll, you only change item's height, and verizontal, you only change item's width. If you want both change w/h, now it's hard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants