我想为某事物的历史创建一个活动。 所以我用ListView创建了一个NestedScrollView(new> activity> scrollingActivity)。 如果底部列表中的项目超出屏幕,则无法再向下滚动。
如何让ListView在NestedScrollView中可滚动?
content_history.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="net.thomi100.allintom.HistoryActivity"
tools:showIn="@layout/activity_history"
android:fillViewport="true"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_view"
/>
</android.support.v4.widget.NestedScrollView>
Java类HistoryActivity:
public class HistoryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String[] list = {};
ArrayList<String> List = new ArrayList<>();
for(int i = 0; i < 40; i++) {
List.add(i + "-Test");
}
ListView listView = ((ListView) findViewById(R.id.list_view));
ArrayAdapter<String> adapter = new ArrayAdapter<>(listView.getContext(), R.layout.history_list_item, List.toArray(list));
listView.setAdapter(adapter);
}
}
activity_history.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="net.thomi100.allintom.HistoryActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_history" />
</android.support.design.widget.CoordinatorLayout>