Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- BOJ
- 선형레이아웃
- java
- 부스트코스
- 윈도우10
- 노개북
- 알고리즘
- 사용자폴더
- RecyclerView
- CardView
- 부스트캠프
- 지하철api
- Node.js
- androidstudio
- 백준
- Windows10
- 트렐로 삭제
- 클린코드
- 노마드북클럽
- 안드로이드
- github
- 노마드코더
- 데이터바인딩
- 액티비티
- Android
- activity
- CS50
- 북클럽
- listview
- codility
Archives
- Today
- Total
Be Developer
[Android] ScrollView 안에 ListView의 height 조절 안되는 문제 본문
반응형
현재 하고 있는 프로젝트의 내부 구조가 이렇게 되어있다.
<ScrollView>
<ConstraintLayout>
...
<ListView />
...
</ConstraintLayout>
</ScrollView>
개발을 진행하면서 ListView에 아이템이 추가되는데 height는 고정되어있는 문제를 발견했다.
구글링을 해보니 여러 해결 방법이 나와있었는데 처음에 시도했던 방법은 이 메서드를 이용하는 것이다.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
구글링을 하면 제일 많이 나오는 메서드인데 나의 경우엔 제대로 적용이 되지 않고, 아이템 밑 하단에 많은 여백이 생겼다.
다시 검색을 해보니 나와 비슷한 질문을 올린 Stack Overflow글을 발견하게 되었고 그 방법으로 해결했다.
위의 방법이 제대로 적용되지 않은 게 내가 Custom Adapter를 사용해서 그런 것 같은데 좀 더 코드 분석을 해봐야 할 것 같다.
문제 해결을 한 메서드
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
float px = 500 * (listView.getResources().getDisplayMetrics().density);
item.measure(View.MeasureSpec.makeMeasureSpec((int) px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Get padding
int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight + totalPadding;
listView.setLayoutParams(params);
listView.requestLayout();
//setDynamicHeight(listView);
return true;
} else {
return false;
}
}
반응형
'Android' 카테고리의 다른 글
[Android] 리스트뷰(ListView) (0) | 2019.09.10 |
---|---|
[Android] 액티비티 (Activity) (0) | 2019.08.27 |
[Android] 선형 레이아웃(LinearLayout) (0) | 2019.05.03 |
[Android] 인텐트 및 인텐트 필터(Intent and IntentFilter) (0) | 2019.04.30 |
[Android] 액티비티의 구성 변경 처리와 조정 (0) | 2019.04.29 |
Comments