Wiersze Android ListView w ScrollView nie są w pełni wyświetlane - przycięte
Napotkałem problem podczas osadzania ListView wewnątrz ScrollView, a przynajmniej tak się wydaje, że problem pochodzi. Element ListView jest dość prosty:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/general_background_list_middle"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:paddingRight="0dp"
android:paddingLeft="0dp">
<ImageView
android:id="@+id/chat_friends_avatar"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="8dp"
android:paddingRight="0dp"
android:paddingLeft="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/friends_icon_avatar_default"/>
<TextView
android:id="@+id/chat_message_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/chat_friends_avatar"
android:layout_alignParentTop="true"
android:layout_marginRight="35dp"
android:maxLines="10"
android:textSize="12dp"/>
<TextView
android:id="@+id/chat_friend_name"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
style="@style/SubText"
android:layout_toRightOf="@id/chat_friends_avatar"
android:layout_below="@id/chat_message_text" />
<TextView
android:id="@+id/chat_message_time"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="3dp"
style="@style/SubText"
android:layout_alignParentRight="true"
android:layout_below="@id/chat_message_text" />
</RelativeLayout>
Jednakże, gdy osadzam listę takich elementów w ScrollView, między innymi elementami, wiersze nie są w pełni wyświetlane, są przycinane (patrz obraz poniżej), jeśli tekst jest zawinięty. ListView jest tworzony w następujący sposób w ScrollView:
<ListView
android:id="@+id/info_chat_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:cacheColorHint="@color/frame_background_color"
android:clickable="false"
android:divider="@null"
android:footerDividersEnabled="false"
android:focusable="false" >
</ListView>
Jeśli wysokość ListView jest ustawiona na „wrap_content”, wyświetlany jest tylko pierwszy element. Dlatego używam metody obliczania wysokości wierszy listy:
private int getCommentsListHeight() {
if (mChatAdapter != null && mChatAdapter.getCount() != 0) {
if (mChatList != null) {// && mCommentsListItemHeight == 0) {
mCommentsListItemHeight = 0;
for (int i = 0; i < mChatAdapter.getCount(); i++) {
// Get view item height
View viewItem = mChatAdapter
.getView(i, new View(OnAirActivity.this), mChatList);
viewItem.measure(0, 0);
Logger.d(LOGTAG, "View " + i + " measured height = " + viewItem.getMeasuredHeight());
mCommentsListItemHeight += viewItem.getMeasuredHeight();
}
}
//return mChatAdapter.getCount() * mCommentsListItemHeight;
return mCommentsListItemHeight;
} else {
return 0;
}
}
Niestety, w przypadku gdy tekst wewnątrz TextView jest zawinięty, nawet w kilku liniach, wysokość elementu wiersza zwróconego przez metodę getMeasuredHeight () jest stała. Również funkcja getLineCount () wywołana w TextView wewnątrz elementu wiersza zwraca 1, nawet jeśli tekst jest zawinięty.
Z drugiej strony, jeśli ten ListView jest osadzony w LinearLayout, wszystko działa dobrze, a pełna lista jest wyświetlana bez przycinania.
Czy masz jakieś sugestie dotyczące tego, co może być tutaj nie tak? Naprawdę nie podoba mi się pomysł ręcznego mierzenia wysokości elementów listy i najwyraźniej nie działa, ale dlaczego android nie może ładnie rozciągnąć ListView wewnątrz ScrollView, aby pasował do niego wszystko tam?
Obcięta lista: