【问题标题】:ExpandableListView expand only on a specific Button?ExpandableListView 仅在特定按钮上展开?
【发布时间】:2013-01-13 07:44:30
【问题描述】:

我正在尝试创建一个像 Spotify 一样的 ExpandableListView ......但我不知道如何禁用 LinearLayout 以像按钮一样工作(展开列表)我创建了一个应该描述的图像我喜欢什么。我希望有可能将单击文本/图像(父级)作为正常交互。单击右侧按钮应该会像在 Spotify 中一样展开列表...

【问题讨论】:

  • 如果您处理 onGroupClick 并返回 true,则组不会展开。如果您返回 false 并且组有 > 0 个子项,则组会展开。
  • 那么你认为我应该实现 ClickListener 接口并返回 true 并在另一个 ClickListener 上注册 Button 并手动扩展组吗?没有官方方法可以指定按钮作为展开资源吗?
  • 现在我能够检测到对组的单击和对右侧图像按钮的单击。但是我将适配器中的 imagebutton 设置为 onClickListener ,它只将视图作为参数......那么我怎么知道我应该展开或折叠哪个组?
  • 请给出您的代码,我是要求您在活动中实现可展开列表的 OnChildClickListener、OnGroupClickListener。

标签: android listview button expandablelistview expand


【解决方案1】:

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

如果您想通过单击特定的Button 或其他一些View 来展开/折叠组,您必须在适配器类的getGroupView 方法中获取该按钮。然后,在ButtononClick 方法中,您可以将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;
}

此外,您希望在 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;
}

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

【讨论】:

  • 它的扩展但不折叠
【解决方案2】:

这是来自 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;
}

在适配器上,我有 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 中我不知道单击了哪个组...如果我没有在任何侦听器上注册按钮,它将不会从 ChannelList 调用 onGroupClick ...

【讨论】:

    【解决方案3】:

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

    我保留了原来的 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 时潜入 callBack 对象并将其钩住单击“category_name”(对于子元素和组元素)

    public class ExpandableListAdapter extends BaseExpandableListAdapter {
    public interface OnCategoryActionListener {
        boolean onCategorySelected(long categoryId);
    }
    ....
    
    public ExpandableListAdapter(Activity context, Category rootCategory,    
    OnCategoryActionListener callBack) {
        this.context = context;
        this.rootCategory = rootCategory;     
        this.callBack = callBack;
    }
    ....
    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View 
    convertView, ViewGroup parent) {
        String categoryName = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)   
              context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.category, null);
        }
        TextView item = (TextView) convertView.findViewById(R.id.category_name);
        item.setTypeface(null, Typeface.BOLD);
        item.setText(categoryName);
        item.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                callBack.onCategorySelected(getGroupId(groupPosition));
            }
        });
      .....
    

    您现在要做的就是在主片段类中正确初始化 ExpandableListAdapter

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多