6

我正在尝试使用 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;
        }
    }

短语活动



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

短语.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>

短语_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>

短语__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>

谢谢

4

3 回答 3

8

嗨,我认为下面的代码可以帮助你。这可以防止折叠列表视图。

    int count = exadapter.getGroupCount();

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

在您的可扩展列表视图中使用上面的代码。它可以解决您的问题

于 2014-01-07T08:48:08.937 回答
1
public int getChildrenCount(int groupPosition) {
 return 1;
 }

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

于 2012-11-14T09:26:44.173 回答
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;
    }
于 2014-01-07T09:04:50.647 回答