简体   繁体   English

Android ExpandableListView没有扩展

[英]Android ExpandableListView not expanding

I'm making an expandable list view for cities but it wont expand. 我正在为城市制作一个可扩展的列表视图,但它不会扩展。 I have another one almost exactly like this one but this one wont expand and I've spent a lot of time trying to see what's wrong and cant afford to waste any more can someone please point out the problem? 我有另外一个几乎完全像这个,但这个不会扩大,我花了很多时间试图看到什么是错的,不能再浪费,有人可以指出问题吗?

I also called expandGroup(0) but it still didnt't expanded (as if there is nothing to expand). 我也调用了expandGroup(0),但它仍然没有扩展(好像没有什么可扩展的)。

XML XML

<ExpandableListView
                        android:id="@+id/judet"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="10dp"
                        android:choiceMode="singleChoice"
                        android:groupIndicator="@null"
                        android:smoothScrollbar="true" >
                    </ExpandableListView>

in my main class 在我的主要班级

final AdapterJudet adapterJudet = new AdapterJudet(getApplicationContext());
ExpandableListView judet = (ExpandableListView) findViewById(R.id.judet);
judet.setAdapter(adapterJudet);
judet.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

                AdapterJudet.selected = AdapterJudet.judete[childPosition];
                judet.collapseGroup(0);
                return false;
            }
        });

Adapter class 适配器类

public class AdapterJudet extends BaseExpandableListAdapter {

    Context ctx;
    static String [] judete = {"Alba","Arad","Arges","Bacau","Bihor","Bistrita-Nasaud","Botosani","Brasov","Braila","Buzau","Caras-Severin","Cluj","Constanta","Covasna","Dambovita","Dolj","Galati","Giurgiu","Gorj","Hargita","Hunedoara","Ialomita","Maramures","Mehedinti","Mures","Neamt","Olt","Prahova","Satu-Mare","Salaj","Sibiu","Teleorman","Timis","Tulcea","Valcea","Vaslui","Vrancea","Bucuresti","Ilfov","Calarasi","Iasi","Suceava"};
    static String selected = "Judet";
    static int judetID = -1;

    public AdapterJudet (Context ctx){
        this.ctx = ctx;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.item_layout, parent, false);
        }

        TextView itemName = (TextView) v.findViewById(R.id.itemName);

        itemName.setText(judete[childPosition]);
        judetID = childPosition;
        return v;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return judete.length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.group_layout, parent, false);
        }

        TextView groupName = (TextView) v.findViewById(R.id.groupName);

        groupName.setText(selected);

        return v;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

I know it's been sometime since this question and I hope this helps someone looking for an answer. 我知道这个问题已经有一段时间了,我希望这有助于有人寻找答案。 When you have clickable widgets (like button, checkbox), set the following property for them: 当您有可点击的小部件(如按钮,复选框)时,为它们设置以下属性:

android:focusable="false"

I'm guessing what happens is that these widgets steal the focus (although this matters only if input is coming in from a keyboard or something like that) and automatically blocks the implicit click listeners (like group expand, collapse happen implicitly without setting a click listener). 我猜测会发生什么事情是这些小部件窃取了焦点(虽然只有输入来自键盘或类似的东西才有意义)并自动阻止隐式点击监听器(如组展开,崩溃发生隐式而不设置点击监听器)。

If you still want to maintain the focusable property for the widget, the only alternative at this time seems to be to find the individual element in the getView() method of the adapter object and set an explicit click listener. 如果您仍想维护窗口小部件的可聚焦属性,那么此时唯一的替代方法似乎是在适配器对象的getView()方法中找到单个元素并设置显式单击侦听器。

I know it has been almost an year since you asked this question.... 我知道你问这个问题差不多已经一年了....

I was facing the same problem. 我遇到了同样的问题。 Check to see if you have any "Clickable" widgets in the layout that you are expanding for the group view. 检查您要为组视图展开的布局中是否有任何“可点击”小部件。 I had a button in my group view layout and it was not passing the click to the group. 我的组视图布局中有一个按钮,它没有将点击传递给组。 Removing the button solve my problem. 删除按钮解决了我的问题。

Use it for your ExpandableListView . 将它用于ExpandableListView

list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return  list.isGroupExpanded(groupPosition) ? list.collapseGroup(groupPosition) : list.expandGroup(groupPosition);
            }
        });

I remember having a similar problem a few months ago, Try changing 我记得几个月前遇到过类似的问题,请尝试改变

android:layout_height="wrap_content"

to

android:layout_height="match_parent"

为我工作android:descendantFocusability =“blocksDescendants”在父视图中添加上面的行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM