我尝试在ListView
内创建一个ScrollView
内EdiText
ListView
。所以我使用自定义方法来制作ListView
NonScrollable。
我的ListView项目xml是这样的:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="3dp"
android:text="Text"
android:textColor="#222"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_below="@+id/title1"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="2dp"
android:text="New Text"
android:textColor="#727272"
android:textSize="15sp"
tools:ignore="HardcodedText" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title2"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="8dp"
android:hint="Note"
android:textSize="13sp"
android:id="@+id/text_note"/>
</RelativeLayout>
自定义方法代码如下:
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
问题是当我在EditText中创建一个新行时,我的listview高度没有调整,并使我在ListView中的最后一项消失。 屏幕截图如下:
我从这里看到: Android: Listview in ScrollView with dynamic height
它说将ListView
更改为LinearLayout
..有人可以帮助我如何制作吗?我不知道如何将ListView
更改为LinearLayout
。
如果不必更改为LinearLayout
,是否有任何方法可以调整ListView
的高度,以便我的项目不会消失?
需要帮助来解决这个问题。
答案 0 :(得分:2)
尝试使用此方法:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
喜欢setListViewHeightBasedOnChildren(listView)
。
答案 1 :(得分:0)
使用以下课程
public class ListViewUtil {
private static final String CNAME = ListViewUtil.class.getSimpleName();
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
用于调用方法
ListViewUtil.setListViewHeightBasedOnChildren(lview);