2016-05-12 3 views
0

Я разработки Android приложения с меню левой слайд содержит некоторые LinearLayout и ExpandableListview в середине, как это изображениеAndroid ExpandableListView внутри ScrollView не расширить полную высоту

enter image description here

Вид ребенок ExpandableListview содержит a TextView и ListView ниже, видимость ListView по умолчанию отсутствует. При нажатии на кнопку TextView будет видна ListView.

Проблема заключается в том, что при нажатии на родительский вид ExpandableListview высота списка также расширяется, чтобы обернуть его содержимое. Но когда вы нажимаете на дочерний вид (также TextView), список не меняется, и мне нужно прокрутить вниз, чтобы увидеть последний элемент.

Чтобы установить высоту ExpandableListview внутри ScrollView, я использовал этот код:

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 - MAXY на экране Seen

+0

Нет, я имею в виду, что ListView всегда показывать весь элемент и не нужно прокручивать. – 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); 
Смежные вопросы