无孩子的ExpandableListView

时间:2019-01-05 19:26:45

标签: java android

晚上好,我是android的新手,并且是第一次使用ExpandableListView。 我创建了一个可扩展的listView,对我来说很好用。关于我的应用程序,我正在创建一个应用程序,让我所在城市的市民可以找到有关不同地点的信息。我有以下列表组和列表项:

列表组: 咖啡厅和餐厅 孩子们: 咖啡店 餐馆

列表组: 电影 儿童: 0

列表组: 博物馆 儿童: 0

如您所见,我仅对一个列表组有childds,对于其他所有列表组我将具有0 childds。 (我真的很想学习如何使用可扩展列表视图,这就是为什么我不使用现成的NavigationDrawer模板的原因)

因此,当用户单击列表组箭头时,箭头不会打开片段,只有当用户单击列表项(子项)时,它才会打开片段。

如果列表组没有子项,则1)它不应该带有箭头(我认为android:groupIndicator可能是2)它应该打开一个片段,然后单击组标题...

private void addDrawersItem(){
    adapter = new CustomExpandableListAdapter(this,lstTitle,lstChild);
    expandableListView.setAdapter(adapter);
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            getSupportActionBar().setTitle(lstTitle.get(groupPosition)); //set title for toolbar when opens
        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {
            getSupportActionBar().setTitle("EDMTDev");
        }
    });
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

            //change fragment when click on item

            String selectedItem = ((List)(lstChild.get(lstTitle.get(groupPosition)))).get(childPosition).toString();
            getSupportActionBar().setTitle(selectedItem);

            if(items[0].equals(lstTitle.get(groupPosition)))
                navigationManager.showFragment(selectedItem);
            else
                throw new IllegalArgumentException("Not supported fragment");

            mDrawerLayout.closeDrawer(GravityCompat.START);
            return false;
        }
    });
}

private void genData(){
    List<String> title = Arrays.asList("Cafe & Restaurants");
    List<String> childItem = Arrays.asList("Cafe","Restaurants");

   lstChild = new TreeMap<>();
   lstChild.put(title.get(0),childItem);

   lstTitle = new ArrayList<>(lstChild.keySet());

}

private void initItems(){
    items = new String[]{"Cafe & Restaurants"};


}

MyAdapter:

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {


private Context context;
private List<String> listTitle;
private Map<String,List<String>> listItem;

public CustomExpandableListAdapter(Context context, List<String> listTitle, Map<String, List<String>> listItem) {
    this.context = context;
    this.listTitle = listTitle;
    this.listItem = listItem;
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return listItem.size()+1;
    }

@Override
public Object getGroup(int groupPosition) {
    return listTitle.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return listItem.get(listTitle.get(groupPosition)).get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String title = (String)getGroup(groupPosition);
    if(convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.list_group,null);
    }
    TextView txtTitle = ( TextView )convertView.findViewById(R.id.listTitle);
    txtTitle.setTypeface(null,Typeface.BOLD);
    txtTitle.setText(title);
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    String title = (String)getChild(groupPosition,childPosition);
    if(convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null);
    }
    TextView txtChild = ( TextView )convertView.findViewById(R.id.expandableListItem);
    txtChild.setText(title);
    return convertView;

}

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

三个屏​​幕截图,显示我的应用程序外观:

enter image description here

enter image description here

enter image description here

我只需要说明如何获得结果...非常少的明智信息。先感谢您。感谢您的帮助。

0 个答案:

没有答案