问题描述
我尝试使用setSelectedChild(INT groupPosition,诠释childPosition,布尔shouldExpandGroup)选择在ExpandableListView一个项目,但没有任何反应。
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;
}
}
}
布局文件:
<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?
推荐答案
我只是有一个问题,摔跤,其中 setSelectedChild(INT,INT,真)
不扩大组,因为它说它。我得到了解决它通过使用电话的醒目直观的方法 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.
这篇关于ExpandableListView忽略setSelectedChild()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!