我尝试使用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>
我怎样才能让它发挥作用?
答案 0 :(得分:4)
我正在努力解决setSelectedChild(int, int, true)
没有扩大该群体的问题。我通过使用非常直观的方法调用ExpandableListView.expandGroup(int);
这个答案是由同事给我的,所以如果有帮助的话,我会把道具传给她。
答案 1 :(得分:0)
对我而言,诀窍是
_explist.expandGroup(0);
_explist.setSelectedChild(1,0, true);