Android examples for User Interface:ListView
Method for Setting the Height of the ListView dynamically.
import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ListAdapter; import android.widget.ListView; public class Main { /****/*from ww w . ja v a 2 s . c o m*/ * Method for Setting the Height of the ListView dynamically. *** Hack to fix * the issue of not showing all the items of the ListView *** when placed inside * a ScrollView ****/ public static void setListViewHeightBasedOnChildren(ListView listView, RecyclerView recyclerView) { ListAdapter listAdapter = listView.getAdapter(); RecyclerView.Adapter adapter = recyclerView.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); } public static void setListViewHeightBasedOnChildren(RecyclerView recyclerView) { RecyclerView.Adapter adapter = recyclerView.getAdapter(); if (recyclerView == null) return; int desiredWidth = View.MeasureSpec.makeMeasureSpec(recyclerView.getWidth(), View.MeasureSpec.UNSPECIFIED); int totalHeight = 0; View view = null; for (int i = 0; i < adapter.getItemCount(); i++) { view = recyclerView.getChildAt(i); 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 = recyclerView.getLayoutParams(); params.height = totalHeight + (3 * (adapter.getItemCount() - 1)); recyclerView.setLayoutParams(params); } }