ExpandableListView仅在特定Button上展开?

时间:2013-01-29 16:52:04

标签: android listview button expandablelistview expand

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

一样展开列表

First picture show the expand action (now), second show the expand action how it should be...

3 个答案:

答案 0 :(得分:18)

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

如果要通过单击特定的Button或其他View来展开/折叠组,则必须在Adapter类的getGroupView方法中获取该按钮。然后,使用您onClick的{​​{1}}方法,将Button投射到parent,或者在创建适配器时在构造函数中传递List的引用。

我更喜欢第一种方法。这是代码,假设您有一个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

还有另一种方法,但是相当糟糕的方法,那就是你在创建@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; } 的类中复制整个Adapter类并设置ExpandableListView。但是不要这样做。 认真! ;)

答案 1 :(得分: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;
}

在适配器上我有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 ...

答案 2 :(得分: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时隐藏在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));
        }
    });
  .....

现在你要做的就是在master fragment class

中正确初始化ExpandableListAdapter
expListAdapter = new ExpandableListAdapter(getActivity(), this.rootCategory,     
    new OnCategoryActionListener());
expListView = (ExpandableListView) getActivity().findViewById(R.id.categories_list);
expListView.setAdapter(expListAdapter);