2012-09-27 3 views
1

동적으로 ListView 높이를 확장하는 방법?

public class ExpandableListView extends ListView { 

    private android.view.ViewGroup.LayoutParams params; 
    private int old_count = 0; 

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

    @Override 
    protected void onDraw(Canvas canvas) { 
     if (getCount() != old_count) { 
      old_count = getCount(); 
      params = getLayoutParams(); 
      params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0); 
      System.out.println("params h "+params.height+" "+params); 
      setLayoutParams(params); 
     } 

     super.onDraw(canvas); 
    } 

} 

로 layout.xml,

<com.jems.realtimedata.ExpandableListView 
       android:id="@+id/listView1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"  
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginTop="25dp" 
       android:background="@color/list_back" 
       android:cacheColorHint="#00000000" 
       android:divider="@drawable/line" 
       android:paddingLeft="7dp" 
       android:paddingRight="7dp" 
       android:paddingTop="10dp" 
       android:scrollbars="none" /> 

하지만, 제대로 표시되지 목록의 마지막 항목, 내가 같이 exbandable의 ListView를 사용하고, 목록보기 높이를 증가합니다. 나는 유틸리티 클래스도 사용했지만 list.Any 다른 솔루션에 대한 변경 사항은 view.Please 확장이 문제를 해결하는 데 도움이되지 않습니다.

답변

2

변화 라인 이것에

params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0); 

:

params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() + 1 : 0); 

단 한 줄짜리 항목이있는 목록에서만 작동합니다.

관련 문제