I am using CoordinatorLayout
in my activity page. In that there is ListView
below the app bar. But its not working when I use ListView
on Lollipop onwards you can use
setNestedScrollingEnabled(true);
on your ListView/GridView/ScrollableView. From the documentation
Enable or disable nested scrolling for this view
if you need backwards compatibility with older version of the OS you'll have to use the RecyclerView
. You can read more here
Edit.
ViewCompat
has the static method setNestedScrollingEnabled(View, boolean)
. Eg.
ViewCompat.setNestedScrollingEnabled(listView, true)
thanks to @Dogcat
for pointing it out
Replace your ListView with RecyclerView
if possible else create your customListView and set onMeasure
of ListView
to this:
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
This ListView
won't be able to scroll anymore and can be used inside NestedScrollView
.
Below code worked for me:
ViewCompat.setNestedScrollingEnabled(listView, true);
Your ListView
should be inside NestedScrollView
this is what worked for me.
set android:fillViewport="true"
on the NestedScrollView
add One Layout Element as Child to NestedScrollView
. In my case LinearLayout
and then
set android:nestedScrollingEnabled="true"
on ListView
Make ListView
a child of LinearLayout
Good to go