ListView problem solved under ScrollView

Problem description: In a composite layout, there are TextView, ListView, Button, etc. Except for the button, the height of the rest is variable and changes dynamically, and it is easy to exceed the height of a screen. At this time, we need to add a ScrollView to the overall layout, But because ListView is integrated with ScrollView, it will cause the Scroll effect of listview to fail;

Solution: I found a lot of methods. The general idea is to dynamically design the Item height of ListView. After constantly looking for articles and trying demos, I finally found a solution for netizens. Now I will share it with you~

Solution: rewrite ListView, rewrite onMeasure method

package com.jby.exam;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class VoteList extends ListView {

	public VoteList(Context context) {
		super(context);
	}
	
	public VoteList(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

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

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




Note: The XML layout file refers to its own control, and the package name needs to be written , such as:

<com.jby.exam.VoteList
      android:id="@+id/vote_listview"
      android:layout_height="wrap_content"
      style="@style/vote_listview" >
</com.jby.exam.VoteList>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325521610&siteId=291194637