How can I use ListView inside NestedScrollView

I created an application with a page that should dynamically download content from a web service. I want to have a list that can be scrolled along with a linear layout inside a NestedScrollView. But when the content is loaded into the list view, it does not stretch to full height.

Here is my code

<?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="com.myquestionth.myquestionth10.Profile2Activity" tools:showIn="@layout/activity_profile2"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="400dp" android:background="#BBBBBB" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Media heading" android:id="@+id/textView2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis." android:id="@+id/textView8" /> </LinearLayout> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#70bcf5" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> 

I searched that scrolling cannot be nested. This is the example I need to fit my layout design from the Google Play review page. What method do they use? Suggest me if I do something wrong. Thank you very much.

enter image description here

Here is what I want.

enter image description here

+6
source share
4 answers

Well, I would suggest you 2 ways to solve this problem:

1) Try making LinearLayout the title of your ListView. Please note that the title should be inflated as it is written here .

2) You mentioned that you are using NestedScrollView, so maybe you should also try replacing the ListView inside the Nested ScrollView with LinearLayout, as wise people have suggested here by adding row views in a loop similar to how your adapter works.

Good luck

+5
source

on lollipop forward you can use

 yourtListView.setNestedScrollingEnabled(true); 

To enable or disable nested scrolling for this view, if you need backward compatibility with the old version of the OS, you will have to use RecyclerView.

+3
source

Instead of adding a ListView under Linear Layout and inside ScrollView, I would suggest putting everything in a ListView.

Yes, you can.

Deploy (override) the following method on your adapter:

 public class MyAdapter extends BaseAdapter { // One view to Header // One view to filter options ("most helpful first" and "Options") // One view to comments private final static int VIEW_HEADER = 0; private final static int VIEW_OPTIONS = 1; private final static int VIEW_COMMENTS = 2; private final static int VIEW_TYPE_MAX = 3; @Override public int getViewTypeCount () { // It will return 3 since I have 3 different types of VIEW return VIEW_TYPE_MAX; } @Override public int getItemViewType(int position) { if (position == 0) return VIEW_HEADER; else if (position == 1) return VIEW_OPTIONS; else return VIEW_COMMENTS; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { if(getItemViewType(position) == VIEW_HEADER) // Inflate HEADER Layout else if (getItemViewType(position) == VIEW_OPTIONS) // Inflate Options Layout else // Inflate comments Layout } // Fill the view contents according to its type .... return convertView; } } 

Android will reuse views. However, Android will reuse views of the same type.

+1
source

Just use this code if you want listview to expand in nestedscrollview. Pass the link to view the function list at the end of the list creation.

 private static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) return; int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED); int totalHeight = 0; View view = null; for (int i = 0; i < listAdapter.getCount(); i++) { view = listAdapter.getView(i, view, listView); if (i == 0) view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT)); view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += view.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } 
0
source

Source: https://habr.com/ru/post/1243872/


All Articles