如何更新scrollview中的视图高度(NestedScrollView)

时间:2016-10-13 21:58:26

标签: android android-recyclerview scrollview

我在show_quick_panel()内有一个RecyclerView高度为wrap_content。当我收到来自API的响应时,我正在尝试在执行时更新此高度。因此,当收到回复时,我将这些项目放入recyclerview,尝试更新其高度,但它不起作用。当我强制暂停/恢复状态,返回到我的应用程序时,recyclelerview高度显示正确。是什么让Android刷新所有视图?

2 个答案:

答案 0 :(得分:0)

了解@mehdi akbarian的评论,您可以尝试在适当的时间(当您的回复到达时)调用notifyDataSetChanged()

public void onResponse(.....){
  //do stuff with data

  mAdapter.notifyDataSetChanged();
}  

将重复由底层布局管理器触发的所有测量,并生成新的布局。

答案 1 :(得分:0)

我的类似情况:

ScrollView -> normal LinearLayout(MATCH, WRAP, vertical) -> custom SizeCappedLinearLayout(MATCH, WRAP, horizontal)

我想基于外部(页眉)视图的高度为ScrollView内部的所有动态元素设置最大高度。棘手的部分是,例如方向更改后,很晚才可以计算出标题视图的高度。仅仅设置LayoutParams还不够灵活。

View heightLink = activ.findViewById(R.id.someHeaderView);
SizeCappedLinearLayout scrollLine = new SizeCappedLinearLayout(activ);
scrollLine.setHeightLink(heightLink);
scrollLine.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
scrollLine.setOrientation(LinearLayout.HORIZONTAL);

自定义视图对象:

public class SizeCappedLinearLayout extends LinearLayout {
    private View widthLink = null;
    private View heightLink = null;

    public SizeCappedLinearLayout(Context context) {
        super(context);
    }

    protected void setWidthLink(View v) {
        widthLink = v;
    }

    protected void setHeightLink(View v) {
        heightLink = v;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(widthLink != null) {
            heightMeasureSpec = getLinkedSpec(widthLink.getMeasuredHeight(), widthLink.getHeight());
        }
        if(heightLink != null) {
            heightMeasureSpec = getLinkedSpec(heightLink.getMeasuredHeight(), heightLink.getHeight());
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private int getLinkedSpec(int measureDim, int dim) {
        if(dim > 0) {
            return MeasureSpec.makeMeasureSpec(dim, MeasureSpec.AT_MOST);
        } else {
            return MeasureSpec.makeMeasureSpec(measureDim, MeasureSpec.AT_MOST);
        }
    }
}