Question

I have this issue where I have a relative layout that has two child relative layouts (leftpanel and rightpanel). They're inside a custom layout for listview items and each item is updated from a json response from the server. So the size depends on what the server provides.

Issue: I want to have each panel's height to match each other, but it seems that setting layout_height to match_parent doesn't work (actually, if this can be resolved, then no more problems).

What I did: I programmatically set the align top and bottom of each panel to each other -- if the other's bigger, adjust the other one and vice versa. So what I did was to have a view (rightpanel) to listen to rightPanel.getViewTreeObserver().addOnScrollChangedListener(), and call the method below everytime there's a scroll change:

private void updateLayoutAlignmentParams(ViewHolder viewHolder) {
    RelativeLayout.LayoutParams leftPanelLayoutParams = (RelativeLayout.LayoutParams)viewHolder.leftPanel.getLayoutParams();
    RelativeLayout.LayoutParams rightPanelLayoutParams = (RelativeLayout.LayoutParams)viewHolder.rightPanel.getLayoutParams();
    int leftPanelHeight = viewHolder.leftPanel.getHeight();
    int rightPanelHeight = viewHolder.rightPanel.getHeight();
    if(leftPanelHeight > rightPanelHeight) {
        rightPanelLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, 0);
        rightPanelLayoutParams.addRule(RelativeLayout.ALIGN_TOP, 0);
        leftPanelLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, viewHolder.rightPanel.getId());
        leftPanelLayoutParams.addRule(RelativeLayout.ALIGN_TOP, viewHolder.rightPanel.getId());
    } else {
        leftPanelLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, 0);
        leftPanelLayoutParams.addRule(RelativeLayout.ALIGN_TOP, 0);
        rightPanelLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, viewHolder.leftPanel.getId());
        rightPanelLayoutParams.addRule(RelativeLayout.ALIGN_TOP, viewHolder.leftPanel.getId());

    }
}

What happens: not all the views get updated while scrolling; so I get a lop-sided listview item where one is bigger than the other vertically but some do adjust well. Odd thing is, when the item gets out of view, it's lop-sided, then gets corrected consistently.

Note: I also tried

  • addOnDrawListener() - every item is updated but I get an ArrayList out of bounds index but doesn't point to any line in my code. Wouldn't be the best solution anyway as I need to support devices with API < 16.
  • setOnGlobalLayoutListener() - Nothing happens.

Please let me know if you know why or have a better solution.

Was it helpful?

Solution

Finally [kindof] fixed it! Replaced the whole method with the code below:

   private void updateLayoutAlignmentParams(ViewHolder viewHolder) {
        viewHolder.rightPanel.setMinimumHeight(viewHolder.leftPanel.getHeight());
        viewHolder.leftPanel.setMinimumHeight(viewHolder.rightPanel.getHeight());
   }

Although, I was able to achieve having the left and right panel aligned with each other using the code above. I'm now having issues where the previous view's height and width are retained when I switch views. :(

Edit: Okay, I ended up using LinearLayout to wrap the whole listview item. Not really sure why RelativeLayout isn't complying with match_parent, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top