ListView in ScrollView - small bug

谁说胖子不能爱 提交于 2019-12-02 09:11:36

Put your listview in a relative layout and your button in another layout. listview is itself scrollable so you don't need to put your listview in a scrollview. It is useless.

by putting the button in another layout, you will be able to scroll listview easily, and your button will be visible below the layout

Jason Y

Try this solution that I posted here.

If you do not want the ListView itself to scroll and just want the ListView's height to be such that all elements are shown, then you can create a subclass of ListView to accomplish this.

public class ListViewForEmbeddingInScrollView extends ListView {
    public ListViewForEmbeddingInScrollView(Context context) {
        super(context);
    }

    public ListViewForEmbeddingInScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListViewForEmbeddingInScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 4, MeasureSpec.AT_MOST));
    }
}

Manipulating the heightMeasureSpec to be AT_MOST with a very large size (Integer.MAX_VALUE >> 4) causes the ListView to measure all of its children up to the given (very large) height and set its height accordingly.

There is much discussion about this problem here including a lot of discussion as to whether or not is is appropriate to even do this. I don't have enough reputation to post my answer there but this is better than any of the solutions in that link. Perhaps someone with more reputation will run across this and post a solution on that other question that links back to this one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!