ExpandableListView始终处于折叠状态

时间:2012-11-14 08:59:16

标签: android android-layout android-listview expandablelistview

我正在尝试使用ExpandableListView构建基本的Android应用程序。将数据放入视图后,我只看到组,如果我点击它们不扩展的组。

这是我的代码,有人可以帮忙吗?

适配器



    public class PhraseAdapter extends BaseExpandableListAdapter {

        private LayoutInflater inflater;

        private List data;

        public PhraseAdapter(Context context) {
            inflater = LayoutInflater.from(context);

        }

        public void setData(List data) {
            this.data = data;
        }

        public View inflate(int viewId) {
            View view = inflater.inflate(viewId, null);
            return view;
        }

        public void updateView(List categories) {
            setData(categories);
            notifyDataSetChanged();
        }

        static class ViewHolder {
            TextView name;
        }

        static class ViewChildHolder {
            TextView phonetix;
            TextView orginal;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return data.get(groupPosition);
        }

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

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            final ViewChildHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.phrases__extra_item, null);
                holder = new ViewChildHolder();
                holder.phonetix = (TextView) convertView
                        .findViewById(R.id.phrases_phonetix);
                holder.orginal = (TextView) convertView
                        .findViewById(R.id.phrases_original);

                convertView.setTag(holder);
                convertView.setClickable(true);

            } else {
                holder = (ViewChildHolder) convertView.getTag();
            }

            holder.phonetix.setText(data.get(groupPosition).getPhonetix());
            holder.orginal.setText(data.get(groupPosition).getOriginal());
            convertView.setFocusableInTouchMode(true);
            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return data.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return data.size();
        }

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

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

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.phrases_item, null);
                holder = new ViewHolder();
                holder.name = (TextView) convertView
                        .findViewById(R.id.phrases_text);
                convertView.setTag(holder);
                convertView.setClickable(true);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.name.setText(data.get(groupPosition).getName());
            return convertView;

        }

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

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

PhrasesActivity



    public class PhrasesActivity extends Activity {
        private PhraseAdapter phraseAdapter;
        private PhraseDao dao;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.phrases);
            dao = new PhraseDao();


            final ExpandableListView mainListView = (ExpandableListView) findViewById(R.id.phrases_list);
            phraseAdapter = new PhraseAdapter(this);
            phraseAdapter.setData(dao.getPhrasesByCategoryId(1));
            mainListView.setAdapter(phraseAdapter);

        }
    }

简单的xml布局

phrases.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >



<ExpandableListView
    android:id="@+id/phrases_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:groupIndicator="@null"
    android:clickable="true" />

</LinearLayout>

phrases_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center_vertical|left"
android:orientation="vertical"
android:paddingBottom="0dip"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:paddingTop="0dip" >

<TextView
    android:id="@+id/phrases_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="0dip"
    android:paddingLeft="0dip"
    android:paddingRight="0dip"
    android:paddingTop="0dip" >
</TextView>

</LinearLayout>

phrases__extra_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center_vertical|left"
android:orientation="vertical"
android:paddingBottom="0dip"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:paddingTop="0dip" >

<TextView
    android:id="@+id/phrases_phonetix"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="0dip"
    android:paddingLeft="0dip"
    android:paddingRight="0dip"
    android:paddingTop="0dip" >
</TextView>

<TextView
    android:id="@+id/phrases_original"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="0dip"
    android:paddingLeft="0dip"
    android:paddingRight="0dip"
    android:paddingTop="0dip" >
</TextView>

</LinearLayout>

感谢

3 个答案:

答案 0 :(得分:6)

嗨,我认为以下代码可以帮助您。这可以防止列表视图崩溃。

    int count = exadapter.getGroupCount();

    for (int i = 0; i < count; i++)
    {
        expListView.expandGroup(i);
    }

在您的expandablelist视图中使用上面的代码。解决您的问题

答案 1 :(得分:1)

要默认展开所有群组,请添加以下行。

((ExpandableListView) parent).expandGroup(groupPosition);

使用getGroupView()方法。

这必须在if子句if (convertView == null)中完成,否则您将无法折叠视图。

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

        if (convertView == null) 
        {
            ((ExpandableListView) parent).expandGroup(groupPosition);

            convertView = inflater.inflate(R.layout.phrases_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.phrases_text);
            convertView.setTag(holder);
            convertView.setClickable(true);
        } 
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(data.get(groupPosition).getName());
        return convertView;
    }

答案 2 :(得分:0)

public int getChildrenCount(int groupPosition) {
 return 1;
 }

这将返回1作为孩子。那可能是个问题。而不是它返回子元素的数量