1. 程式人生 > >ScrollView巢狀ListView時Bug解決以及ExpandableListView設定全部展開

ScrollView巢狀ListView時Bug解決以及ExpandableListView設定全部展開

1.ExpandableListView設定預設展

mExpandListview.setAdapter(adapter);
//設定ExpandableListView全部展開
for (int i = 0;i < adapter.getGroupCount();i++) {
    mExpandListview.expandGroup(i);
}

2.ExpandableListView設定禁止收縮

//設定不能點選收縮
mExpandListview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { return true; } });

3.GridView設定禁止滑動

ScrollView巢狀GridView時,GridView需要禁止滑動,否則會出現問題,ExpandableListView巢狀時也一樣需要

自定義一個類繼承於GridView然後重寫onMeasure()方法,最後在XML佈局中使用自定義的GridView

public class NonScrollGridView extends 
GridView { public NonScrollGridView(Context context) { super(context); } public NonScrollGridView(Context context, AttributeSet attrs) { super(context, attrs); } public NonScrollGridView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs,
defStyleAttr); } //重寫此方法可設定GridView禁止滑動 @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }

4.ListView設定禁止滑動(與GridView相同)

在ScrollView巢狀ListView時,ListView需要禁止滑動

自定義一個類繼承於ListView 然後重寫onMeasure()方法,最後在XML佈局中使用自定義的ListView

public class NoScrollListView extends ListView {
    public NoScrollListView(Context context) {
        super(context);
}

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

    public NoScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
}
    //重寫該方法可設定ListView禁止滑動
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, mExpandSpec);
}
}