我的活动布局中有一个 ListView ,存在于 ScrollView 中,如下所示:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:orientation="vertical"
android:padding="4dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_vertical|center_horizontal"
android:padding="5dp"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:visibility="gone" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/article_title"
android:gravity="center_vertical|center_horizontal"
android:padding="5dp"
android:text="@string/add_to_my_resources"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" />
</LinearLayout>
<LinearLayout
android:id="@+id/buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:id="@+id/article_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/article_title"
android:gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" />
<Button
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" />
</LinearLayout>
<ListView
android:id="@+id/authors_listView"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</ListView>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:divider="@color/light_gray"
android:dividerHeight="0.5dp"
android:groupIndicator="@null"
android:scrollbars="none"
android:textAlignment="center">
</ExpandableListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
我有一个列表适配器名称 MyListAdapter ,用于填充我的列表视图:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = mcontex.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.my_list, null);
holder = new ViewHolder();
holder.txtViewName = (TextView) convertView.findViewById(R.id.textView1);
holder.txtViewAff = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewName.setGravity(Gravity.LEFT);
holder.txtViewName.setText(name.get(position));
if (aff != null) {
holder.txtViewAff.setGravity(Gravity.LEFT);
holder.txtViewAff.setText(aff.get(position));
}
return convertView;
}
my_list.xml 是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="@+id/textView1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1">
</TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
app:srcCompat="@drawable/ic_draw"/>
</RelativeLayout>
我想动态设置listview高度,以避免在父布局中滚动列表视图。所以我使用这种方法:
public static void setListViewHeightBasedOnChildren(ListView listView) {
AuthorsListAdapter listAdapter = (AuthorsListAdapter) listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight=0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++)
{
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LinearLayoutCompat.LayoutParams.MATCH_PARENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));
listView.setLayoutParams(params);
listView.requestLayout();
}
此方法在正常情况下正常工作,但当 txtViewAff 值超过2或3行时,listview的最后一项将落后于 ExpandableListView 。我在方法 setListViewHeightBasedOnChildren 中将<40>常量值添加到 totalHeight :
totalHeight + = view.getMeasuredHeight()+ 40;
但它为正常项添加了一些空格,而不添加40。
我该怎么做才能解决这个问题?