复杂的 ListActivity 不会滚动
我遇到了 ListActivity 的滚动问题。这是布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/DefaultBackground"
android:orientation="vertical">
<include layout="@layout/default_header" />
<ScrollView android:layout_height="fill_parent" android:layout_width="fill_parent"
android:fillViewport="true">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout style="@style/RoundedCornerContainer" android:orientation="vertical">
<TextView android:text="@string/is_anyone_injured" style="@style/DefaultText.RoundedContainerImmediateChild" />
<Button android:id="@+id/accident_help_call_911_btn" android:text="@string/call_911"
style="@style/BaseButton.Large" />
<View style="@style/LandingPageChoiceDivider" />
<TextView android:text="@string/review_accident_checklist" style="@style/DefaultText.RoundedContainerImmediateChild" />
<Button android:id="@+id/accident_help_checklist_btn" android:text="@string/checklist"
style="@style/BaseButton.Large" />
<View style="@style/LandingPageChoiceDivider" />
<TextView android:text="@string/collect_accident_info" style="@style/DefaultText.RoundedContainerImmediateChild" />
<Button android:id="@+id/accident_help_add_accident_btn" android:text="@string/add_new_accident"
style="@style/BaseButton.Large" />
</LinearLayout>
<LinearLayout style="@style/RoundedCornerContainer" android:orientation="vertical">
<TextView android:text="@string/previous_accidents" style="@style/LabelValueTitle" />
<View style="@style/LandingPageChoiceDivider" />
<ListView android:id="@id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:drawSelectorOnTop="false" />
<TextView android:id="@id/android:empty" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="@string/no_previous_claims_found" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
这是我的 ListActivity:
public class AccidentHelp extends BaseListActivity {
private static final String TAG = "AccidentHelp";
private PreviousClaimAdapter previousClaimsAdapter;
private List<AccidentInfo> previousClaimList = new ArrayList<AccidentInfo>();
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "onCreate called");
super.onCreate(savedInstanceState);
setContentView(R.layout.accident_help);
// code for setting up button OnClickListeners here.
}
@Override
public void onResume() {
super.onResume();
setupPreviousClaimsAdapter();
}
private void setupPreviousClaimsAdapter() {
AccidentInfoDB db = new AccidentInfoDB(getApplicationContext());
previousClaimList = db.read(currentUser.getUserName());
//there were no previous accidents.
if (previousClaimList == null) {
return;
}
previousClaimsAdapter = new PreviousClaimAdapter(this, R.layout.previous_claim_row, previousClaimList);
setListAdapter(previousClaimsAdapter);
getListView().setSelector(R.drawable.landing_choice_selector);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v(TAG, "previous claim selected.");
AccidentInfo accidentInfo = (AccidentInfo)getListAdapter().getItem(position);
Intent intent = new Intent(AccidentHelp.this, ClaimLandingPage.class);
intent.putExtra(ClaimLandingPage.CLAIM_LANDING_PAGE_ACCIDENT_INFO_EXTRA_NAME, accidentInfo);
startActivity(intent);
}
});
}
}
如您所见,我希望页面中除标题之外的所有内容都是可滚动的。我已经在非 ListActivities 中成功完成了此操作。但是,在这种布局中,它根本不允许我滚动页面。
我正在更新 onResume 列表,因为可以在子活动中更新数据;无论如何,将其移至 onCreate 似乎没有什么区别。
当我删除 ScrollView 时,它允许我仅滚动布局的 ListView 部分。但我想滚动整个页面。有什么建议吗?
I'm having a scrolling issue with a ListActivity. Here is the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/DefaultBackground"
android:orientation="vertical">
<include layout="@layout/default_header" />
<ScrollView android:layout_height="fill_parent" android:layout_width="fill_parent"
android:fillViewport="true">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout style="@style/RoundedCornerContainer" android:orientation="vertical">
<TextView android:text="@string/is_anyone_injured" style="@style/DefaultText.RoundedContainerImmediateChild" />
<Button android:id="@+id/accident_help_call_911_btn" android:text="@string/call_911"
style="@style/BaseButton.Large" />
<View style="@style/LandingPageChoiceDivider" />
<TextView android:text="@string/review_accident_checklist" style="@style/DefaultText.RoundedContainerImmediateChild" />
<Button android:id="@+id/accident_help_checklist_btn" android:text="@string/checklist"
style="@style/BaseButton.Large" />
<View style="@style/LandingPageChoiceDivider" />
<TextView android:text="@string/collect_accident_info" style="@style/DefaultText.RoundedContainerImmediateChild" />
<Button android:id="@+id/accident_help_add_accident_btn" android:text="@string/add_new_accident"
style="@style/BaseButton.Large" />
</LinearLayout>
<LinearLayout style="@style/RoundedCornerContainer" android:orientation="vertical">
<TextView android:text="@string/previous_accidents" style="@style/LabelValueTitle" />
<View style="@style/LandingPageChoiceDivider" />
<ListView android:id="@id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:drawSelectorOnTop="false" />
<TextView android:id="@id/android:empty" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="@string/no_previous_claims_found" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
And here is my ListActivity:
public class AccidentHelp extends BaseListActivity {
private static final String TAG = "AccidentHelp";
private PreviousClaimAdapter previousClaimsAdapter;
private List<AccidentInfo> previousClaimList = new ArrayList<AccidentInfo>();
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "onCreate called");
super.onCreate(savedInstanceState);
setContentView(R.layout.accident_help);
// code for setting up button OnClickListeners here.
}
@Override
public void onResume() {
super.onResume();
setupPreviousClaimsAdapter();
}
private void setupPreviousClaimsAdapter() {
AccidentInfoDB db = new AccidentInfoDB(getApplicationContext());
previousClaimList = db.read(currentUser.getUserName());
//there were no previous accidents.
if (previousClaimList == null) {
return;
}
previousClaimsAdapter = new PreviousClaimAdapter(this, R.layout.previous_claim_row, previousClaimList);
setListAdapter(previousClaimsAdapter);
getListView().setSelector(R.drawable.landing_choice_selector);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v(TAG, "previous claim selected.");
AccidentInfo accidentInfo = (AccidentInfo)getListAdapter().getItem(position);
Intent intent = new Intent(AccidentHelp.this, ClaimLandingPage.class);
intent.putExtra(ClaimLandingPage.CLAIM_LANDING_PAGE_ACCIDENT_INFO_EXTRA_NAME, accidentInfo);
startActivity(intent);
}
});
}
}
As you can see, I want all the contents of the page except the header to be scrollable. I have successfully done this in non-ListActivities. However, in this layout, it doesn't allow me to scroll the page at all.
I am updating the list onResume because the data can be updated in sub-activities; regardless, moving that to onCreate doesn't seem to make a difference.
When I remove the ScrollView
, it allows me to scroll just the ListView portion of the layout. But I want to scroll the whole page. Any recommendations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ListView 并不意味着位于 ScrollView 内。请参阅如何自动调整列表视图的答案不滚动。我动态地将子项添加到布局中的空 LinearLayout 中。
The ListView isn't meant to be inside a ScrollView. See the answer to how can i automatically size listview so it doesn't scroll. I am dynamically adding children to an empty LinearLayout in the layout instead.