我正在开发一个可扩展的listview,这意味着我将listview包含在scrollview中,与listview默认行为不同,listview高度现在将是它的实际大小,我滚动滚动视图而不是列表显示。下面的代码是我的实现,问题是,当listview扩展时屏幕会推到底部,我怎样才能将屏幕视图保持在顶部?感谢
ListAdapter customAdapter = new LeadersAdapter(this,R.layout.leader_record_row, records);
results.setAdapter(customAdapter);
updateListViewHeight(results);
public static void updateListViewHeight(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
return;
}
//get listview height
int totalHeight = 0;
int adapterCount = myListAdapter.getCount();
for (int size = 0; size < adapterCount ; size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//Change Height of ListView
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
myListView.setLayoutParams(params);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/share_bg" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/lbd_top_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/lbd_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<RelativeLayout
android:id="@+id/lbd_my_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/lbd_logo" >
<ImageView
android:id="@+id/lbd_user_pic"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/lbd_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/lbd_user_pic"
android:paddingTop="5dp"
android:layout_toRightOf="@+id/lbd_user_pic"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/lbd_user_pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lbd_user_name"
android:layout_toRightOf="@+id/lbd_user_pic"
android:textColor="@color/dark_orange" />
</RelativeLayout>
</RelativeLayout>
<TextView
android:id="@+id/lbd_name_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/lbd_top_section"
android:layout_marginLeft="20dp"
android:text="@string/leader_board_name"
android:textColor="@android:color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/lbd_pt_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/lbd_name_tag"
android:layout_marginRight="20dp"
android:text="@string/leader_board_pt"
android:textColor="@android:color/white"
android:textSize="20sp" />
<ListView
android:id="@+id/lbd_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/lbd_name_tag" >
</ListView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>