问题
I am using ListView within NestedScrollView, but can't see more than 1 item in ListView. I can scroll upto the end of TextView (textContact) but can't scroll within the ListView (listContact).
Here's the code for .xml File :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
... >
<android.support.design.widget.CoordinatorLayout
... >
<LinearLayout
...
android:orientation="vertical">
<android.support.v7.widget.Toolbar
... />
<android.support.v4.widget.NestedScrollView
... >
<LinearLayout
...
android:orientation="vertical">
<TextView
android:id="@+id/textContact"
... />
<LinearLayout
...
android:orientation="vertical">
<ListView
android:id="@+id/listContact"
... />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
... />
</android.support.v4.widget.DrawerLayout>
Is there any design issue, any compatibility issue of the Widgets/Controls used here, or anything else ?
回答1:
You need to enable the following two properties inside the NestedScrollView,
<android.support.v4.widget.NestedScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
Then use a helper class to extend the ListView width & fix scrolling functionality,
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
}
To use the helper class while setting the ListView adapter,
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
Helper.getListViewSize(listView);
PS: Make sure you have added support design lib for your project.
compile 'com.android.support:design:23.4.0'
回答2:
Use ViewCompat.setNestedScrollingEnabled() on your listview and your problem should be solved.
回答3:
I think, you have bad layout design and you should redesign it. In general, you shouldn't have a scrollable view like ListView inside another scrollable view like NestedScrollView or ScrollView. Do you really need that? Try to change it, flatten your views structure and this problem should disappear.
回答4:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:clipToPadding="false" >
</android.support.v7.widget.RecyclerView>
Are you missing app:layout_behavior
来源:https://stackoverflow.com/questions/39104713/cant-scroll-listview-in-its-parent-nestedscrollview-android