Paw*_*dki 21 android listview scrollview textview
我在ScrollView中嵌入ListView时遇到了一个问题,或者至少我觉得问题来自哪里.ListView元素非常简单:
<?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>
Run Code Online (Sandbox Code Playgroud)
但是,当我在ScrollView中嵌入这样的元素列表时,在其他一些元素之间,行没有完全显示,如果文本被包装,它们将被剪裁(见下图).ListView在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>
Run Code Online (Sandbox Code Playgroud)
如果ListView的高度设置为"wrap_content",则仅显示第一个元素.这就是为什么我使用一种方法来计算列表行的高度:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果TextView中的文本被包装,甚至包括多行,则getMeasuredHeight()方法返回的row元素的高度是不变的.即使文本被包装,在row元素内的TextView上调用的getLineCount()也返回1.
另一方面,如果此ListView嵌入在LinearLayout中,则一切正常,并且显示完整列表时不会剪切.
你对这里可能有什么问题有什么建议吗?我真的不喜欢手动测量列表元素的高度的想法,它显然不起作用,但为什么不能很好地拉伸ScrollView内的ListView以适应它在那里?
剪辑列表:

Raj*_*hwa 40
使用/sf/users/14363471/创建的此方法
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);
}
Run Code Online (Sandbox Code Playgroud)
waq*_*lam 11
封装在a中是一种不好的做法,因为ListView本身包含滚动功能.你应该实现一个不包含这种层次结构视图的解决方案,我希望它能做到这一点:)ListViewScrollView
| 归档时间: |
|
| 查看次数: |
34720 次 |
| 最近记录: |