我在LinearLayout中实现了ExpandableListView,但是当我运行应用程序时,我得到了这个:
首先,标题出现两次是正常的吗?
当我点击上部标题时,我得到了扩展视图,这就是我想要它的方式:
如果我触摸第二个标题(在我看来,它不应该存在),它会消失,让上标题单独(就像我想要的那样):
我不知道为什么会这样。我想只有一个标题,我希望修改大小。以下是包含相关代码的文件:
ExpandableListView所在的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_sv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" >
...
<ExpandableListView
android:id="@+id/learnabletm"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
</LinearLayout>
expandable_list_view_child.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/child"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>
expandable_list_view_group.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:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="18dp" />
</LinearLayout>
ExpAdapter.java
public class ExpAdapter extends BaseExpandableListAdapter {
public static final String ARG_POS = "pos";
private Context myContext;
Bundle data;
publicExpAdapter(Context context, Bundle data) {
myContext = context;
this.data = data;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.expandable_list_view_child, null);
}
String text = "";
for(int i = 0; i < bytes.length; i++){
for(int j = 0; j < 8; j++){
text = text + " " + (i*8+j+1) + "\n";
}
}
TextView child = (TextView) convertView.findViewById(R.id.child);
child.setText(text);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@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) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.expandable_list_view_group, null);
}
TextView group = (TextView) convertView.findViewById(R.id.group);
group.setText("Numbers");
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
最后调用ExpandableListView的片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
expList = (ExpandableListView) rootView.findViewById(R.id.learnabletm);
width = args.getInt(ARG_WIDTH);
expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
expList.setAdapter(new ExpAdapter(getActivity(), data));
expList.expandGroup(ExpandableListView.PACKED_POSITION_TYPE_CHILD);
expList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener(){
@Override
public void onGroupExpand(int groupPosition){
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expList.getLayoutParams();
param.height = expList.getHeight();
expList.setLayoutParams(param);
expList.refreshDrawableState();
}
});
expList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener(){
@Override
public void onGroupCollapse(int groupPosition){
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expList.getLayoutParams();
param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
expList.setLayoutParams(param);
expList.refreshDrawableState();
}
});
expList.setOnChildClickListener(new ExpandableListView.OnChildClickListener(){
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id){
Log.d("OnChildClickListener", "OK");
return false;
}
});
}