只有在特定按钮上展开可扩展列表吗?

14

我正在尝试创建一个类似于Spotify的可扩展列表(ExpandableListView)。但是,我不知道如何禁用LinearLayout以使其不像按钮一样操作(展开列表)。我创建了一张图片,它应该描述我喜欢的东西。我希望能够像正常交互一样处理点击文本/图像(父级)。右侧按钮的点击应该像Spotify一样展开列表...

第一张图片显示展开操作(当前),第二张图片显示应该的展开操作...


8
如果你处理onGroupClick并且返回true,则该组不会展开。如果你返回false,并且该组具有> 0个子项,则该组将展开。 - Gaurav Vashisth
那么你认为我应该实现ClickListener接口,只需返回true并在另一个ClickListener上注册按钮并手动扩展组?没有官方的方法来指定按钮作为展开资源吗? - mkl
现在我能够检测到对组的点击和右侧图像按钮的点击。但是我将图像按钮设置为仅接收视图作为参数的onClickListener,那么我如何知道应该展开或折叠哪个组? - mkl
请提供您的代码,我是在要求您在活动中实现可扩展列表的OnChildClickListener和OnGroupClickListener。 - Gaurav Vashisth
3个回答

21

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

如果您想通过单击特定的


2
它正在扩展,但不会崩溃。 - Saad Bilal

3
这是ChannelList的onCreate方法,它扩展了ListFragment。如果调用该Fragment,它会存储数据并生成一个新的适配器。
@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方法...

0

虽然不是特别优雅的解决方案,但是它帮了我一个大忙:

我保留了原始的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时,Than偷偷地添加了callBack对象,并将其连接到“category_name”的onClick事件上(适用于子元素和组元素)。
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));
        }
    });
  .....

现在你需要做的就是在主片段类中正确初始化可扩展列表适配器。
expListAdapter = new ExpandableListAdapter(getActivity(), this.rootCategory,     
    new OnCategoryActionListener());
expListView = (ExpandableListView) getActivity().findViewById(R.id.categories_list);
expListView.setAdapter(expListAdapter);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接