Reputation: 1043
I try to create a ListView
Inside ScrollView
with EdiText
inside the ListView
. So I use a Custom method to make my ListView
NonScrollable.
My ListView Item xml is like this:
<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>
The Custom method Code is like this:
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();
}
}
The problem is when I make a new line in EditText my listview height is not adjust and make my last Item in ListView disappear. The Screenshot is like this:
I see from here: Android: Listview in ScrollView with dynamic height
It said that Change ListView
to LinearLayout
.. Can someone help me how to make that ? I have no idea how to change ListView
to LinearLayout
.
And if not have to change to LinearLayout
is there any way how to adjust the height of my ListView
so my item will not disappear ?
Need help to solve this.
Upvotes: 1
Views: 1843
Reputation: 3212
Try using this method :
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);
}
like setListViewHeightBasedOnChildren(listView)
.
Upvotes: 4