Android ExpandableListView是否仅在特定按钮上展开?

Android ExpandableListView是否仅在特定按钮上展开?,android,listview,button,expandablelistview,expand,Android,Listview,Button,Expandablelistview,Expand,我正在尝试创建一个像Spotify一样的可扩展列表视图。。。但我不知道如何禁用LinearLayout,使其像按钮一样工作(展开列表)。我创建了一个图像,该图像应该描述我喜欢的内容。我希望能够将点击文本/图像(父对象)作为正常交互处理。点击右键应该会像Spotify一样展开列表 这是从扩展ListFragment的ChannelList创建的onCreate。如果片段被调用,它将存储数据并生成一个新的适配器 @Override public View onCreateView(Layout

我正在尝试创建一个像Spotify一样的可扩展列表视图。。。但我不知道如何禁用LinearLayout,使其像按钮一样工作(展开列表)。我创建了一个图像,该图像应该描述我喜欢的内容。我希望能够将点击文本/图像(父对象)作为正常交互处理。点击右键应该会像Spotify一样展开列表


这是从扩展ListFragment的ChannelList创建的onCreate。如果片段被调用,它将存储数据并生成一个新的适配器

@Override   
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{                               
    m_ListView = new ExpandableListView(getActivity());
    m_ListView.setId(android.R.id.list);

    m_ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    m_ListView.setMultiChoiceModeListener(m_MultiChoiceModeListener);

    m_ChannelListAdapter = new ChannelListAdapter(getActivity(), this);
    m_ListView.setAdapter(m_ChannelListAdapter);
    m_ListView.setGroupIndicator(null);
    m_ListView.setOnGroupClickListener(this);

    return m_ListView;
}
在适配器上,我有一个getGroupView方法,它如下所示:

public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

    if (view == null) 
    {
        view = inflater.inflate(R.layout.layout_channelwithimage, viewGroup, false);
    }

    TextView textView = (TextView) view.findViewById(R.id.labelwithimage);
    textView.setText(getGroup(i).toString());

    ImageButton imbu = (ImageButton) view.findViewById(R.id.imageButton);
    //imbu.setOnClickListener(this);    
    imbu.setFocusable(false);

    return view;
}

因此,如果我在适配器中注册ImageButton,它将从适配器调用onClick。但在onClick中,我不知道单击了哪个组。。。如果我没有在任何侦听器上注册该按钮,它将不会从通道列表调用onGroupClick…

不是非常优雅的解决方案,但它帮助了我:

我保留了原来的groupIndicator。我喜欢这种行为

在groupItem布局上,我只是用空视图阻塞了空间,这样原始的groupIndicator仍然可以单击:

<LinearLayout> 
....

<!--just dummy space underneath the default expand_collapse group icon - this way original expand collapse behavior is preserved on the icon-->
<View
        android:layout_width="25dp"
        android:layout_height="match_parent">
</View>

<!-- text view will have actionListener -->
<TextView
        android:id="@+id/category_name"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        .... /> 
.... 
</LinearLayout>
现在需要做的就是在主片段类中正确初始化ExpandableListAdapter

expListAdapter = new ExpandableListAdapter(getActivity(), this.rootCategory,     
    new OnCategoryActionListener());
expListView = (ExpandableListView) getActivity().findViewById(R.id.categories_list);
expListView.setAdapter(expListAdapter);

这是一个有点老的问题,但这个答案可能会帮助一些人

如果您想通过单击特定的
按钮
或其他一些
视图
来展开/折叠组,则必须在适配器类的
getGroupView
方法中获取该按钮。然后,在您的
按钮的
onClick
方法中,您可以将
parent
转换为
ExpandableListView
,或者在创建适配器时在构造函数中传递列表的引用

我喜欢第一种方法。下面是代码,假设您有一个
TextView
和一个
ImageView
,即箭头。我还添加了更改箭头状态

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

    String headerTitle = (String) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.left_drawer_list_group, parent, false);
    }

    TextView listHeaderText = (TextView) convertView
            .findViewById(R.id.left_menu_list_header_text);
    ImageView listHeaderArrow = (ImageView) convertView.findViewById(R.id.left_menu_list_header_arrow);

    listHeaderText.setText(headerTitle);

    //Set the arrow programatically, so we can control it
    int imageResourceId = isExpanded ? android.R.drawable.arrow_up_float : android.R.drawable.arrow_down_float;
    listHeaderArrow.setImageResource(imageResourceId);

    listHeaderArrow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(isExpanded) ((ExpandableListView) parent).collapseGroup(groupPosition);
            else ((ExpandableListView) parent).expandGroup(groupPosition, true);

        }
    });

    return convertView;
}
此外,您希望在
onGroupClick
listener中禁用展开/折叠

@Override
public boolean onGroupClick(ExpandableListView parent, View v,
        int groupPosition, long id) {

    //Do some other stuff, but you shall not expand or collapse

    return true;
}
还有另一种方法,但它相当糟糕,那就是在创建
ExpandableListView
并设置
Adapter
的类中复制整个
Adapter
类。但不要这样做。
真的!;)

如果处理onGroupClick并返回true,则组不会扩展。如果返回false且组具有>0个子级,则组会扩展。因此,您认为我应该实现ClickListener界面,只返回true并在另一个ClickListener上注册按钮,然后手动扩展组吗?没有正式的方法指定一个按钮作为扩展资源吗?现在我能够检测到对组的点击和对右边imagebutton的点击。但是我将适配器中的imagebutton设置为onClickListener,它只获取作为参数的视图。。。那么,我如何知道应该展开或折叠哪个组呢?请给出您的代码,我要求您在活动中实现OnChildClickListener,OnGroupClickListener的可展开列表。它正在展开,但没有折叠
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
        int groupPosition, long id) {

    //Do some other stuff, but you shall not expand or collapse

    return true;
}