ExpandableListView 忽略 setSelectedChild()

发布于 2024-11-25 17:03:56 字数 4523 浏览 4 评论 0原文

我尝试使用 setSelectedChild(int groupPosition, int childPosition, boolean ShouldExpandGroup) 在 ExpandableListView 中选择一个项目,但没有任何反应。

这是我测试它的原始活动:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class StartupActivity extends Activity {

    private static final String[] _items = new String[] {"Oops", "Wham", "Bam"};

    private ExpandableListView _explist;
    private TextView _txt_hello;
    private Button _btn_sel;
    private MyExplistAdapter _explist_adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         _explist = (ExpandableListView) findViewById(R.id.explistMain);     
         _txt_hello = (TextView) findViewById(R.id.txtHello);  
         _btn_sel = (Button) findViewById(R.id.btnAction);

         _btn_sel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                selectElement();
            }            
         });
    }

    @Override
    protected void onResume() {
        super.onResume();

        _explist_adapter = new MyExplistAdapter();
        _explist.setAdapter(_explist_adapter);
        _explist.expandGroup(0);
    }

    private void selectElement() {
        boolean success = _explist.setSelectedChild(0, 1, true);
        long sel = _explist.getSelectedId();

        _txt_hello.setText((success ? "success" : "failure") + " " + sel);
        //setSelectedChild() returns true
        //but getSelectedId() returns -1 and nothing is actually selected
    }

    public class MyExplistAdapter extends BaseExpandableListAdapter {

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

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

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new TextView(StartupActivity.this);
            }
            TextView txt_view = (TextView) convertView;
            txt_view.setText(_items[childPosition]);
            txt_view.setHeight(50);

            return txt_view;
        }

        @Override
        public int getChildrenCount(int groupPosition) {

            return _items.length;
        }

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

        @Override
        public int getGroupCount() {
            return 1;
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            TextView txt_view = new TextView(StartupActivity.this);
            txt_view.setBackgroundColor(Color.GRAY);
            txt_view.setHeight(50);
            return txt_view;
        }

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

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

    }
}

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello"
        android:id="@+id/txtHello" />
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/btnAction"
        android:text="Select"></Button>
    <ExpandableListView android:layout_width="match_parent"
        android:id="@+id/explistMain" android:layout_height="fill_parent"></ExpandableListView>
</LinearLayout>

如何使其工作?

I try to select an item in ExpandableListView using setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup), but nothing happens.

Here's the primitive activity I tested it with:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class StartupActivity extends Activity {

    private static final String[] _items = new String[] {"Oops", "Wham", "Bam"};

    private ExpandableListView _explist;
    private TextView _txt_hello;
    private Button _btn_sel;
    private MyExplistAdapter _explist_adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         _explist = (ExpandableListView) findViewById(R.id.explistMain);     
         _txt_hello = (TextView) findViewById(R.id.txtHello);  
         _btn_sel = (Button) findViewById(R.id.btnAction);

         _btn_sel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                selectElement();
            }            
         });
    }

    @Override
    protected void onResume() {
        super.onResume();

        _explist_adapter = new MyExplistAdapter();
        _explist.setAdapter(_explist_adapter);
        _explist.expandGroup(0);
    }

    private void selectElement() {
        boolean success = _explist.setSelectedChild(0, 1, true);
        long sel = _explist.getSelectedId();

        _txt_hello.setText((success ? "success" : "failure") + " " + sel);
        //setSelectedChild() returns true
        //but getSelectedId() returns -1 and nothing is actually selected
    }

    public class MyExplistAdapter extends BaseExpandableListAdapter {

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

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

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new TextView(StartupActivity.this);
            }
            TextView txt_view = (TextView) convertView;
            txt_view.setText(_items[childPosition]);
            txt_view.setHeight(50);

            return txt_view;
        }

        @Override
        public int getChildrenCount(int groupPosition) {

            return _items.length;
        }

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

        @Override
        public int getGroupCount() {
            return 1;
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            TextView txt_view = new TextView(StartupActivity.this);
            txt_view.setBackgroundColor(Color.GRAY);
            txt_view.setHeight(50);
            return txt_view;
        }

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

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

    }
}

The layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello"
        android:id="@+id/txtHello" />
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/btnAction"
        android:text="Select"></Button>
    <ExpandableListView android:layout_width="match_parent"
        android:id="@+id/explistMain" android:layout_height="fill_parent"></ExpandableListView>
</LinearLayout>

How can I make it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩人痒 2024-12-02 17:03:56

我只是在解决一个问题,其中 setSelectedChild(int, int, true) 没有像它所说的那样扩展组。我通过使用非常直观的方法来解决这个问题,即调用 ExpandableListView.expandGroup(int);

这个答案是由一位同事给我的,所以如果有帮助的话,我会将道具传递给她。

I was just wrestling with an issue in which setSelectedChild(int, int, true) did not expand the group as it says it does. I got around it by using the strikingly intuitive method of calling ExpandableListView.expandGroup(int);

This answer was given to me by a co-worker, so I'll pass props on to her if it helps.

怪我入戏太深 2024-12-02 17:03:56

对我来说,诀窍是

_explist.expandGroup(0);
_explist.setSelectedChild(1,0, true);

for me the trick was

_explist.expandGroup(0);
_explist.setSelectedChild(1,0, true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文