【发布时间】:2016-03-05 02:05:24
【问题描述】:
我尝试在ScrollView 内部创建一个ListView,并在ListView 内部创建一个EdiText。所以我使用自定义方法使我的ListView NonScrollable。
我的 ListView Item 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 中的最后一个项目消失。 截图是这样的:
我从这里看到: Android: Listview in ScrollView with dynamic height
它说将ListView 更改为LinearLayout.. 有人可以帮我怎么做吗?我不知道如何将ListView 更改为LinearLayout。
如果不必更改为LinearLayout,有什么办法可以调整我的ListView 的高度,这样我的项目就不会消失?
需要帮助来解决这个问题。
【问题讨论】:
-
可以分享列表项的xml视图吗?
-
@Ajitha 我已经添加了我的列表项 xml,你能帮我解决这个问题吗?
标签: android listview height scrollview onmeasure