单击ExpandableListView中的子项以打开活动

时间:2019-04-15 04:28:54

标签: java expandablelistview subitem

我有一些源代码,允许 Expandablelistview 显示子项

我想在单击父列表中的每个 subItem (例如DnB1,DnB2,Digger1,Digger 2等)时如何调用活动的建议。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mExpandingList = findViewById(R.id.expanding_list_main);
    createItems();

}

private void createItems() {
    addItem("Drill and Blast", new String[]{"DnB1", "DnB2", "DnB3", "DnB4"}, R.color.pink, R.drawable.drill);
    addItem("Excavator and Shovel", new String[]{"Digger1", "Digger2", "Digger3", "Digger4"}, R.color.blue, R.drawable.excavator);
    addItem("Dragline", new String[]{"Dragline1", "Dragline2", "Dragline3", "Dragline4"}, R.color.purple, R.drawable.dragline);
    addItem("Haulage", new String[]{"Haulage1", "Haulage2", "Haulage3", "Haulage4"}, R.color.yellow, R.drawable.truck);
    addItem("Dozer push", new String[]{"Dozer1", "Dozer2", "Dozer3", "Dozer4"}, R.color.orange, R.drawable.dozerpush);
    addItem("Safety", new String[]{"Safety1", "Safety2", "Safety3", "Safety4"}, R.color.green, R.drawable.safety);

}

private void addItem(String title, String[] subItems, int colorRes, int iconRes) {
    //Let's create an item with R.layout.expanding_layout
    final ExpandingItem item = mExpandingList.createNewItem(R.layout.expanding_layout);


    //If item creation is successful, let's configure it
    if (item != null) {
        item.setIndicatorColorRes(colorRes);
        item.setIndicatorIconRes(iconRes);

        //It is possible to get any view inside the inflated layout. Let's set the text in the item
        ((TextView) item.findViewById(R.id.title)).setText(title);

        //We can create items in batch.
        item.createSubItems(subItems.length);
        for (int i = 0; i < item.getSubItemsCount(); i++) {
            //Let's get the created sub item by its index
            final View view = item.getSubItemView(i);

            //Let's set some values in
            configureSubItem(item, view, subItems[i]);
        }

    }

}
private void configureSubItem(final ExpandingItem item, final View view, String subTitle) {
    ((TextView) view.findViewById(R.id.sub_title)).setText(subTitle);

}


}

0 个答案:

没有答案