android - ExpandableListView 总是折叠的

标签 android android-layout android-listview expandablelistview

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

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

适配器



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

短语 Activity



    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>

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>

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

谢谢

最佳答案

您好,我认为下面的代码对您有帮助。这防止折叠 ListView 。

    int count = exadapter.getGroupCount();

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

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

关于android - ExpandableListView 总是折叠的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13375702/

相关文章:

android - 如何改变移动物体的位置 - Box2D

android-layout - 不明白在哪里使用Void函数和Return类型函数

android在ListView项中实现多次点击事件

java - 如果我使用不同的 ViewItemType,我应该使用 viewHolders 吗?

java - Anko doAsync 调用不执行

android - 添加Google Play Services 9.0.0后Dex文件超过64k

android - Play 商店 : revert app bundle to use apk

android - 可滚动的 ListView 和一个 TextView

android - 具有 visibility=gone 元素的膨胀布局的性能影响

java - android 开始时出现奇怪的错误