Last active
August 29, 2015 14:13
-
-
Save baleen37/9de274c03db3740df8b2 to your computer and use it in GitHub Desktop.
listview in scrollview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protected void setListViewHeightBasedOnChildren(ListView listView) { | |
| Adapter listAdapter = listView.getAdapter(); | |
| if(listAdapter == null) | |
| return ; | |
| int totalHeight = 0; | |
| for (int i = 0; i < listAdapter.getCount(); i++) { | |
| View listItem = listAdapter.getView(i, null, listView); | |
| listItem.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); | |
| totalHeight += listItem.getMeasuredHeight(); | |
| } | |
| ViewGroup.LayoutParams params = listView.getLayoutParams(); | |
| params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); | |
| listView.setLayoutParams(params); | |
| listView.requestLayout(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protected void setListViewInScrollObserver(final ListView listView) { | |
| final Adapter listAdapter = listView.getAdapter(); | |
| if (listAdapter == null) | |
| return; | |
| listAdapter.registerDataSetObserver(new ListViewInScrollObserver(listView, listAdapter)); | |
| } | |
| class ListViewInScrollObserver extends DataSetObserver { | |
| Adapter listAdapter; | |
| ListView listView; | |
| public ListViewInScrollObserver(ListView listView, Adapter listAdapter) { | |
| this.listView = listView; | |
| this.listAdapter = listAdapter; | |
| setListViewHeightBasedOnChildren(listView); | |
| } | |
| public void onChanged() { | |
| setListViewHeightBasedOnChildren(listView); | |
| } | |
| public void onInvalidated() { | |
| setListViewHeightBasedOnChildren(listView); | |
| } | |
| public void setListViewHeightBasedOnChildren(ListView listView) { | |
| int totalHeight = 0; | |
| for (int i = 0; i < listAdapter.getCount(); i++) { | |
| View listItem = listAdapter.getView(i, null, listView); | |
| listItem.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); | |
| totalHeight += listItem.getMeasuredHeight(); | |
| } | |
| ViewGroup.LayoutParams params = listView.getLayoutParams(); | |
| params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); | |
| listView.setLayoutParams(params); | |
| listView.requestLayout(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment