ExpandableListView仅在特定Button上展开?

mkl*_*mkl 14 expand android listview button expandablelistview

好吧,我正在尝试创建一个像Spotify一样的ExpandableListView它...但我不知道如何禁用LinearLayout像一个按钮(展开列表)我已经创建了一个图像应该描述我喜欢什么.我喜欢有可能处理文本/图像(父)的点击作为正常的交互.单击右键应该像Spotify一样展开列表...

第一张图显示了展开动作(现在),第二张图展示了展开动作应该如何...

Dim*_*my3 19

这是一个有点老问题,但这个答案可能对某人有所帮助.

如果要通过单击特定Button或其他方式来展开/折叠组View,则必须getGroupView在Adapter类中的方法中获取该Button .然后,在你的onClick方法中Button,要么在你创建适配器时强制parent转换ExpandableListView,要么在构造函数中传递List的引用.

我更喜欢第一种方法.这是代码,假设你有一个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;
}
Run Code Online (Sandbox Code Playgroud)

此外,您希望在onGroupClick侦听器中禁用展开/折叠.

@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;
}
Run Code Online (Sandbox Code Playgroud)

还有另一种方法,但是相当糟糕的方法,那就是你Adapter在创建ExpandableListView和设置的类中复制整个类Adapter.但是不要这样做.认真!;)

  • 它的扩张但不坍塌 (2认同)

mkl*_*mkl 3

这是来自 ChannelList 的 onCreate,它扩展了 ListFragment。它存储数据并在调用 Fragment 时生成新的 Adapter。

@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;
}
Run Code Online (Sandbox Code Playgroud)

在适配器上,我有 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;
}
Run Code Online (Sandbox Code Playgroud)

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