我正在尝试使用 ExpandableListView 构建基本的 android 应用程序。将数据放入视图后,我只看到组,如果我单击组,它们不会展开。
这是我的代码,有人可以帮忙吗?
适配器
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;
}
}
短语活动
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>
短语_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>
谢谢