2016-05-12 73 views
0

我正在開發的左滑動菜單的Android應用程序包含了一些LinearLayout和中間ExpandableListview這樣的形象的Android ExpandableListView內滾動型不會擴大全高

enter image description here

ExpandableListview子視圖包含一個TextView和一個ListView下面,ListView的可見性在默認情況下沒有了。當點擊TextView時,將會看到ListView

問題是,當單擊ExpandableListview的父視圖時,列表的高度也會展開以包裹它的內容。但是當點擊子視圖(也是TextView)時,列表'高度不會改變,我必須向下滾動才能看到最後一個項目。

要設置ExpandableListview的高度內滾動型,我用這個代碼:

public static void setListViewHeightBasedOnChildren(ListView listView) { 
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) { 
     // pre-condition 
     return; 
    } 

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom(); 
    for (int i = 0; i < listAdapter.getCount(); i++) { 
     View listItem = listAdapter.getView(i, null, listView); 
     if(listItem instanceof SecondLevelExpandableListView) 
      Log.e("abc", "SecondLevelExpandableListView"); 
     if (listItem instanceof ViewGroup) { 
      Log.e("abc", "ViewGroup"); 
      listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     } 
     listItem.measure(0, 0); 
     totalHeight += listItem.getMeasuredHeight(); 
    } 

    ViewGroup.LayoutParams params = listView.getLayoutParams(); 
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
    listView.setLayoutParams(params); 
} 

的問題截圖

enter image description here

請幫我解決這個問題,我需要的資源列表總是要包裝它的內容。

在此先感謝。

回答

0

當您展開動畫結束, 調用此:

listView.smoothScrollTo(x,y); 

你可以找到listView.getScrollY在y - 美星在屏幕上看到

+0

不,我的意思是,列表視圖始終顯示所有項目,不需要滾動。 – Glenn

+0

那麼你可以再試一次你的問題是什麼? –

+0

我的問題是,當我點擊Texview以可見下面的列表時,ExpanableListview的高度不會擴展到包裝它的內容。我希望ExpandableListview的高度也像我點擊它的父視圖時一樣擴展。 – Glenn

0
Use the Snippet for ExpandableListView 

    import android.content.Context; 
import android.util.AttributeSet; 
import android.view.ViewGroup; 
import android.widget.ExpandableListView; 

public class ExpandableHeightListView extends ExpandableListView { 

    boolean expanded = false; 

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

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

    public ExpandableHeightListView(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public boolean isExpanded() { 
     return expanded; 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     // HACK! TAKE THAT ANDROID! 
     if (isExpanded()) { 
      // Calculate entire height by providing a very large height hint. 
      // But do not use the highest 2 bits of this integer; those are 
      // reserved for the MeasureSpec mode. 
      int expandSpec = MeasureSpec.makeMeasureSpec(
        Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
      super.onMeasure(widthMeasureSpec, expandSpec); 

      ViewGroup.LayoutParams params = getLayoutParams(); 
      params.height = getMeasuredHeight(); 
     } else { 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     } 
    } 

    public void setExpanded(boolean expanded) { 
     this.expanded = expanded; 
    } 
} 



**** 
use <classpath for ExpandableHeightListView 
attribs 
/> 

***** 
ExpandableHeightListView expListView = (ExpandableHeightListView)findViewById(id);