默认情况下如何打开展开可扩展列表视图组

时间:2016-09-08 14:12:35

标签: android expandablelistview android-scrollview

您好我是初学者在Android和我的应用程序中我需要在Scrollview上添加Expandable Listview

我已经添加并实现了如下功能,以便在Scrollview上添加ExpandableListview,现在它正常运行

但根据我的Case默认情况下,Expandable ListView组需要扩展我该怎么做

请一位有人帮助我

main.xml中

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/toolbar_layout">
        </include>

        <ExpandableListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:groupIndicator="@null" />

    </LinearLayout>

</ScrollView>

MainActivity:

public class MainActivity extends ExpandableListActivity {

    ArrayList<AvailableTimingsBean> availableTimingsBeanArrayList;
    ExpandableListView expandableListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        expandableListView = (ExpandableListView) findViewById(android.R.id.list);

        availableTimingsBeanArrayList = new ArrayList<>();
        AvailableTimingsBean availableTimingsBean = new AvailableTimingsBean();
        availableTimingsBean.setFromDay("Mon");
        availableTimingsBean.setToDay("Thurs");

        TimingsBean timingsBean = new TimingsBean();
        timingsBean.setFromTime("10:00AM");
        timingsBean.setToTime("12:00AM");
        ArrayList<TimingsBean> timingsBeanArrayList = new ArrayList<>();
        timingsBeanArrayList.add(timingsBean);

        availableTimingsBean.setTimings(timingsBeanArrayList);
        availableTimingsBeanArrayList.add(availableTimingsBean);

        MyExpandableAdapter adapter = new MyExpandableAdapter(this,availableTimingsBeanArrayList);
        expandableListView.setAdapter(adapter);

        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                setListViewHeight(parent,groupPosition);
                return false;
            }
        });
    }

    /**
     * @param listView
     * @param group
     */
    private void setListViewHeight(ExpandableListView listView,
                                   int group) {

        ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();

        int totalHeight = 0;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                View.MeasureSpec.EXACTLY);
        for (int i = 0; i < listAdapter.getGroupCount(); i++) {
            View groupItem = listAdapter.getGroupView(i, false, null, listView);
            groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

            totalHeight += groupItem.getMeasuredHeight();

            if (((listView.isGroupExpanded(i)) && (i != group))
                    || ((!listView.isGroupExpanded(i)) && (i == group))) {

                for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {

                    View listItem = listAdapter.getChildView(i, j, false, null,
                            listView);

                    listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

                    totalHeight += listItem.getMeasuredHeight();
                }
            }
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        int height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
        if (height < 10)
            height = 200;
        params.height = height;
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}

适配器:

public class MyExpandableAdapter extends BaseExpandableListAdapter {

    private LayoutInflater inflater;
    private ArrayList<AvailableTimingsBean> availableTimingsBeanArrayList;
    private Context context;

    public MyExpandableAdapter(Context context, ArrayList<AvailableTimingsBean> availableTimingsBeanArrayList) {

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
        this.availableTimingsBeanArrayList = availableTimingsBeanArrayList;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        try {

            System.out.println("getChildView");

            final TimingsBean childObj = availableTimingsBeanArrayList.
                    get(groupPosition).getTimings().get(childPosition);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.adapter_available_timings_child_items_layout, null);
            }

            TextView fromTime = (TextView) convertView.findViewById(R.id.fromTime_id);
            TextView toTime = (TextView) convertView.findViewById(R.id.toTime_id);
            fromTime.setText(childObj.getFromTime());
            toTime.setText(childObj.getToTime());

            return convertView;

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        try {

            System.out.println("getGroupView");
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.adapter_available_timings_header_items, null);
            }

            TextView fromDay = (TextView) convertView.findViewById(R.id.fromday_textview);
            TextView toDay = (TextView) convertView.findViewById(R.id.today_textview);
            fromDay.setText(availableTimingsBeanArrayList.get(groupPosition).getFromDay());
            toDay.setText(availableTimingsBeanArrayList.get(groupPosition).getToDay());

            return convertView;

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        return availableTimingsBeanArrayList.get(groupPosition).getTimings().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public int getGroupCount() {
        return availableTimingsBeanArrayList.size();
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

将此添加到Adapter类中的getGroupView方法。

View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);
return v;