2

ExpandableListViewLinearLayout 안에 있으며 CustomAdapter을 사용하여 설정합니다.
해당 하위 항목에 대해 onGroupClick() 메서드를 사용하여 특정 서비스에 요청을 보내고 결과를 String으로 가져온 다음 클릭 한 그룹 항목의 자식 목록을 채 웁니다.ExpandableListView 동적 하위 높이보기

문제는입니다. 서비스 응답이 텍스트보기의 자식보기 텍스트보기로 설정된 후 업데이트 된 높이를 가져올 수 없으므로 linearlayout 컨테이너 높이로 인해 높이가 증가하지 않습니다. 또한 스크롤링 문제가 발생합니다.

비록 목록 하위 항목 XML은 다음과 같이 WRAP_CONTENT입니다 :

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/root" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:minHeight="@dimen/rowHeight" 
android:background="@color/colorLightYellow"> 

<TextView 
    android:id="@+id/tvTitle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="@dimen/marginGeneral" 
    android:layout_toLeftOf="@+id/tvCount" 
    android:gravity="center_vertical" 
    android:paddingBottom="@dimen/marginGeneral" 
    android:paddingTop="@dimen/marginGeneral" 
    android:text="tvTitle" 
    android:textColor="@color/colorGray" 
    android:textSize="@dimen/fontSizeNormal" /> 
... 
</RelativeLayout> 

그래서 코드 부분은 나와 함께 작은 장기 체류입니다 :

@Override 
public boolean onGroupClick(ExpandableListView parent, View v, final int groupPosition, long id) { 
    Map<Item, List<ItemDetail>> childList = detailExpandableAdapter.getChildList(); 
    final Item item = detailExpandableAdapter.getGroup(groupPosition); 
    if (childList.get(item).size() == 0) { 
     startProgressDialog(); 

     GlobalApplication.getService().getItemDetails(Session.getCurrent().getSessionId(), getItem.item.itemNo, item.name, new ServiceCallback<GetItemDetails>() { 
      @Override 
      public void onSuccess(GetItemDetails response) { 
       stopProgressDialog(); 

       List<ItemDetail> itemDetailList = null; 

       if (GetItemDetails.isSuccess(response)) { 
        itemDetailList = response.getItemDetailList(); 
       } else { 
        itemDetail itemDetail = new ItemDetail(); 
        itemDetail.resultDesc = response.getResult().getResultDesc(); 

        if (StringUtils.isNullOrWhitespace(itemDetail.resultDesc)) { 
         itemDetail.resultDesc = Result.getGeneralFailResult().getResultDesc(); 
        } 

        itemDetailList = new ArrayList<ItemDetail>(); 
        itemDetailList.add(itemDetail); 
       } 

       if (itemDetailList != null) { 
        Map<Item, List<ItemDetail>> childList = detailExpandableAdapter.getChildList(); 

        if (childList.containsKey(item)) { 
         childList.remove(item); 
        } 
        childList.put(item, itemDetailList); 

        detailExpandableAdapter.setChildList(childList); 
        detailExpandableAdapter.notifyDataSetChanged(); 
        detailExpandableAdapter.notifyDataSetInvalidated(); 

        listViewLastItems.expandGroup(groupPosition); 
       } 
      } 

      @Override 
      public void onFail() { 
       stopProgressDialog(); 
      } 
     }); 

     return false; 
    } 

    return false; 
} 

@Override 
public void onGroupExpand(int groupPosition) { 
    setExpandableListViewHeightStable(listViewLastItems, llListViewItemDetailContainer); 
    if (lastExpanded != -1 && groupPosition != lastExpanded) 
     listViewItems.collapseGroup(lastExpanded); 
    lastExpanded = groupPosition; 
} 

public void setExpandableListViewHeight(ExpandableListView expandableListView, LinearLayout linearLayoutParent){ 
    try { 
     ExpandableListAdapter expandableListAdapter = expandableListView.getExpandableListAdapter(); 

     int totalHeight = 0; 
     int desiredWidth = View.MeasureSpec.makeMeasureSpec(expandableListView.getWidth(), View.MeasureSpec.UNSPECIFIED); 

     for (int i = 0; i < expandableListAdapter.getGroupCount(); i++) { 
      View groupItem = expandableListAdapter.getGroupView(i, false, null, expandableListView); 
      groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); 

      //Logger.debug("recalculateExpandableListViewHeight listItem:"+groupItem.getMeasuredHeight()); 
      totalHeight += groupItem.getMeasuredHeight(); 

      if (expandableListView.isGroupExpanded(i)){ 
       for (int j = 0; j < expandableListAdapter.getChildrenCount(i); j++) { 
        View listItemChild = expandableListAdapter.getChildView(i, j, false, null, expandableListView); 

        listItemChild.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, View.MeasureSpec.UNSPECIFIED)); 
        listItemChild.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 

        Logger.debug("recalculateExpandableListViewHeight listItemChild:" + listItemChild.getMeasuredHeight()); 
        totalHeight += listItemChild.getMeasuredHeight(); 
       } 
      } 
     } 

     linearLayoutParent.getLayoutParams().height = totalHeight + (expandableListAdapter.getGroupCount() * expandableListView.getDividerHeight()); 
     linearLayoutParent.requestLayout(); 
    } catch (Exception e) { 
     Logger.printStackTrace(e); 
    } 
} 

업데이트 : 이것이 내가 용기로 사용하는 선형 레이아웃입니다은

<LinearLayout 
       android:id="@+id/llListViewItemContainer" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tvItemDueDate" 
       android:layout_marginTop="@dimen/marginGeneral" 
       android:orientation="vertical"/> 

업데이트 2 : ExpandableListView를 동적으로 LinearLayout에 추가합니다.

listViewItems = new ExpandableListView(getContext()); 
listViewItems.setScrollContainer(false); 
listViewItems.setDivider(new ColorDrawable(getResources().getColor(R.color.colorLightGray))); 
listViewItems.setDividerHeight(UIHelper.convertDptoPixels(1)); 
listViewItems.setGroupIndicator(null); 
listViewItems.setOnGroupClickListener(this); 
listViewItems.setOnGroupExpandListener(this); 
listViewItems.setOnGroupCollapseListener(this); 
//generate empty child list 
Map<Item, List<ItemDetail>> childMap = new HashMap<>(); 
for (Item item : getItems.getItemList()) { 
    childMap.put(item, new ArrayList<ItemDetail>()); 
       } 
detailExpandableAdapter = new detailExpandableAdapter(getActivity(), getItems.getItemList(), childMap); 
listViewItems.setAdapter(detailExpandableAdapterF); 
listViewItems.removeAllViews(); 
listViewItems.addView(listViewLastItems); 
UIHelper.setExpandableListViewHeightStable(listViewItems, llListViewDetailContainer); 
+0

LinearLayout (컨테이너) 대신 RecyclerView를 사용할 수 있습니다. – Sabari

+0

그러면 선형 레이아웃 부모가 scrollView 안에 래핑됩니까? – HourGlass

+0

이 솔루션을 사용해 보셨습니까? http://thedeveloperworldisyours.com/android/expandable-listview-inside-scrollview/#sthash.sW6JMsU5.dpbs –

답변

3

아래의 지정된 맞춤형 ExpandableListView 클래스를 사용하고 onMeasure 메서드를 재정의하십시오.

public class MyExpandableListView extends ExpandableListView { 

    public MyExpandableListView(Context context) { 
     super(context); 
    } 

    public MyExpandableListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MyExpandableListView(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(); 
    } 
} 

과 같은 사용,

<com.app.custom.NonScrollExpandableListView 

    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

당신이 사용자 정의 클래스를 넣어하는 패키지 이름 com.app.custom를 교체합니다.

가능한 경우 ScrollView 대신 NestedScrollView을 사용합니다. 이는 Android의 새 버전과 이전 버전 모두에서 중첩 스크롤 부모 및 하위 기능을 모두 지원하기 때문입니다. 중첩 스크롤은 기본적으로 사용됩니다.

도움이 될지 알려주세요. 해피 코딩 !!!

+0

도움, 선형 레이아웃은 동적 항목의 높이로 넓어 지지만 다른 항목은 건너 뜁니다. –

+0

Didnt는 당신을 얻습니다. 무슨 일이 일어나는지 자세히 설명해주세요. –

+0

NestedScrollView 또는 사용자 정의 LinearLayout 클래스를 사용해보십시오 (MyExpandableListView와 같지만 LinearLayout을 확장 함). Linearlayout 컨테이너에 ExpandableListView를 동적으로 추가하고 있습니까? 그렇다면 코드를 붙여 넣으십시오. –

0

HeaderView 속성을 확장형 목록보기으로 사용하는 것이 좋습니다. AS ExpandableListViewListView의 파생 클래스이므로 HeaderView 속성이 있어야한다고 생각합니다. 있는 ScrollView 안에 ListView에 사용에

문제 -

  1. 실적 - 당신의 ListView의 측정 속성과 연극을하려고한다면 그것은 반드시 세포의 재활용에 영향을 미칠 것입니다.
  2. 사용자 환경 - ListView의 부모가 다른 스크롤 가능한보기 인 경우 이상한 동작이 발생합니다.

은 더 나은 다음 헤더 또는 바닥 글 당신의 필요에 따라 ListView에의에 넣고 그보기를 팽창 및 중, 다른보기 내부에있는 LinearLayout 물건을 이동합니다.

// Inflated View which is going to be use as Header of view 
    ViewGroup headerView = (ViewGroup)getLayoutInflater().inflate(R.layout.list_header,expListView,false); 
    // Add that view to Header 
    your_expandale_listView.addHeaderView(headerView);