14

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

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

4

3 回答 3

20

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

如果要通过单击特定Button或其他来展开/折叠组,则必须在 Adapter 类View中的方法中获取该 Button 。getGroupView然后,在onClick您的方法中Button,要么转换parentExpandableListView,要么在创建适配器时在构造函数中传递 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;
}

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

于 2014-07-18T15:09:35.063 回答
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 ...

于 2013-01-30T14:19:37.287 回答
0

Not overly elegant solution but it helped me out:

I've preserved the original groupIndicator. I liked the behavior as it was.

On the groupItem layout I simply blocked the space with empty view so that the original groupIndicator was still clickable:

<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>

Than sneaked in callBack object while creating ExpandableListAdapter and hooked it onClick on the "category_name" (for both child and group elements)

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));
        }
    });
  .....

All you have to do now is to initialize properly ExpandableListAdapter in master fragment class

expListAdapter = new ExpandableListAdapter(getActivity(), this.rootCategory,     
    new OnCategoryActionListener());
expListView = (ExpandableListView) getActivity().findViewById(R.id.categories_list);
expListView.setAdapter(expListAdapter);
于 2014-01-06T00:26:58.467 回答