2016-05-12 8 views
0

私は左側のスライドメニューでAndroidアプリを開発していますこの画像のように、いくつかのLinearLayoutと途中でExpandableListviewが含まれていアンドロイドExpandableListView完全な高さを拡張していない

enter image description here

ExpandableListviewの子ビューが含まれていますa TextViewListView以下の場合、ListViewの可視性はデフォルトではなくなります。 TextViewをクリックすると、ListViewが表示されます。

親ビューのExpandableListviewをクリックすると、リストの高さも拡大されてコンテンツがラップされるという問題があります。しかし、子ビュー(TextView)をクリックすると、リストの高さは変更されず、最後の項目を表示するためにスクロールする必要があります。私はリストを必要とし、この問題を解決するために

enter image description here

私を助けてください、問題の

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); 
} 

スクリーンショット:

は、私はこのコードを使用し、ScrollView内ExpandableListviewの高さを設定するには常にそのコンテンツをラップする。

ありがとうございます。

答えて

0

アニメーションの終了を展開すると、 コールこの:

listView.smoothScrollTo(x,y); 

あなたはlistView.getScrollYとYを見つけることができます - 画面上のMAXY私はリストビューが常に表示されることを意味

+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); 
関連する問題