Expandablelistview自定义指示器不可见

时间:2015-01-25 16:33:05

标签: android expandablelistview indicator

我有一个带有自定义指标的expandablelistview,但不知何故该指标未显示在expandablelistview中。

我可能忽略了什么吗?非常感谢你帮助我。

编辑:现在一切正常,低于正确的代码。

custom_arrow.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_empty="true" android:drawable="@drawable/arrow_down_black"/>
   <item android:state_expanded="true" android:drawable="@drawable/arrow_up_black"/>
   <item android:drawable="@drawable/arrow_down_black"/>
</selector>

使用expandablelistview的框架布局:

<LinearLayout
    android:id="@+id/expandablelv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/layout_bg"
    android:orientation="vertical">

    <ExpandableListView
        android:transcriptMode="alwaysScroll"
        android:id="@+id/exp"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_marginLeft="5dp">

    </ExpandableListView>
</LinearLayout>

我的代码:

 @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
//
lv = (ExpandableListView) view.findViewById(R.id.exp);
  lv.setAdapter(new ExpandableListAdapter(groups, children)); 
   lv.setGroupIndicator(this.getResources().getDrawable(R.drawable.custom_arrow));
//
}

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private final LayoutInflater inf;
    private String[] groups;
    private String[][] children;

    public ExpandableListAdapter(String[] groups, String[][] children) {
        this.groups = groups;
        this.children = children;
        inf = LayoutInflater.from(getActivity());
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = inf.inflate(R.layout.child_layout, parent, false);
            holder = new ViewHolder();

            holder.text = (TextView) convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(getChild(groupPosition, childPosition).toString());

        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inf.inflate(R.layout.parent_layout, parent, false);

            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(getGroup(groupPosition).toString());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ViewHolder {
        TextView text;
    }
}

1 个答案:

答案 0 :(得分:2)

这对我有用:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_empty="true" android:drawable="@drawable/arrow_down_black"/>
    <item android:state_expanded="true" android:drawable="@drawable/arrow_up_black"/>
    <item android:drawable="@drawable/arrow_down_black"/>
</selector>

可以在this(有点)相关问题中找到更多信息。