在ExpandableListAdapter中嵌套ListView
如果只是单纯的ListView
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout) layoutInflater.inflate(R.layout.dialog_item2, null);
view.setPadding(0, 0, 0, 15);
TextView tvCon = (TextView)view.findViewById(R.id.con);
tvCon.setText(conte[childPosition]);
tvCon.setPadding(0, 10, 0, 10);
tvCon.setTextSize(18);
MyAdapter2 m2=new MyAdapter2(teamNum*4+groupPosition,MainActivity.this);//teamNum*4+groupPosition=which国家
MyListView conlv=(MyListView)view.findViewById(R.id.conlv);
conlv.setAdapter(m2);
return view;
}
经试验,在模拟器中只显示两项,在手机中显示1项
若改成自定义的ListView控件,则正常显示
Activity外面写MyListView的类,如下图
至于为什么要单独成类我不清楚,但是试验过,如果把Class MyListView卸载Activity里面,并不会起作用,仍然只显示两项,但写在外面则显示正常
这是今天下午学长告诉我要这样写的,具体原因以后补上
MyListView.java代码
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(android.content.Context context,
android.util.AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
布局文件中MyListView,格式<包名.MyListView>
<com.example.euro2012_0930.MyListView
android:id="@+id/conlv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.example.euro2012_0930.MyListView>
Activity.java中引用MyListView的代码
MyListView conlv=(MyListView)view.findViewById(R.id.conlv);
来个对比图(图一为改前效果,图二为改后效果)