BaseExpandableListAdapter - disable onclick for gr

2019-07-16 01:07发布

I'm working on implementing an ExpandableListAdapter.

Use case:

I have group items I want to add to the Expandable list with no children. Problem is that every group item can be clicked on, even if they contain no children.

Is there a way to disable/grey out the expandable button to indicate for the user that it contains no children?

At the moment I've implemented:

public void onGroupExpanded(int groupPosition) {
    if (!containsChildren(groupPosition)) {
        Toast.makeText(context, context.getString(R.string.err_no_children), Toast.LENGTH_LONG).show();
    }
}

I'd rather have a visual as mentioned above (disable/grey out the expandable button) and have this Toast to show up if they click it. Because the moment you can click the groupItem, it tries to just expand zero items, which looks silly? Or is this default good MMI[1] behavior?

[1] http://en.wikipedia.org/wiki/Man-machine_interface

2条回答
爷的心禁止访问
2楼-- · 2019-07-16 01:49

There is other way to do this by setting onGroupClickListener and returning true for the groups that dont have childs.

myExpandableList.setOnGroupClickListener(new OnGroupClickListener(){

            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id){
                // TODO Auto-generated method stub.
                if(!myAdapter.containsChildren(groupPosition)){
                    return true;
                }else{
                    return false;                   
                }
            }});
查看更多
Rolldiameter
3楼-- · 2019-07-16 01:50

I have resorted to turning off the group indicators using (in an ExpandableListActivity, do it manually on your list if you have a regular Activity):

getExpandableListView().setGroupIndicator(null);

Then in your getGroupView() method that you are implementing check to see if the group you are about to make a view for has any children (I am using a List of Lists for mine so its rather straight forward) and style the group's view accordingly (make your own expander-button maybe). You might have to play around with the parent and convertView settings to see what works best for you.

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

        convertView = inflater.inflate(
                android.R.layout.simple_expandable_list_item_1, parent,
                false);

        if (groups.get(groupPosition).getMuscles().size() == 0) {
            convertView.setEnabled(false); //Greys out group name
        } 
查看更多
登录 后发表回答