我目前有一个由自定义BaseExpandableListAdapter对象处理的ExpandableListView(请参阅下面的代码段)。该列表由Plate-objects填充,其中标题显示在组级别,然后展开的项目显示有关Plate的一些信息,例如make。
我遇到的麻烦是我需要从顶部向列表中动态添加项目,即在项目的ArrayList中插入新项目att位置0。我现在遇到的问题是,如果位置1的项目被扩展,然后我添加一个新项目,“展开状态”仍然位于位置1,即使该项目现在位于第2位。先前在位置0的项目现在被扩展。现在位于第2位的项目已关闭。
当我向阵列添加新项目时,如何控制未打开的项目?我不应该直接在数组中添加新项吗?或者我是否需要覆盖notifyDataSetChanged()或其他东西?
添加项目的代码片段(按钮侦听器):
int _position = this.recent_list.indexOf( new_plate );
if ( _position == -1 ) {
this.recent_list.add( 0, new_plate );
} else {
this.recent_list.get( _position ).duplicate( ); //Just adds info that it exists twice
}
this.recent_list_adapter.notifyDataSetChanged( );
这是自定义适配器:
public class RecentExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<Plate> list_data;
public RecentExpandableListAdapter( Context context, List<Plate> list_data ) {
this.context = context;
this.list_data = list_data;
}
@Override
public Object getGroup( int plate_position ) {
return this.list_data.get( plate_position );
}
@Override
public long getGroupId( int plate_position ) {
return plate_position;
}
@Override
public int getGroupCount( ) {
return this.list_data.size( );
}
@Override
public Object getChild( int plate_position, int child_posititon ) {
return this.list_data.get( plate_position );
}
@Override
public long getChildId( int plate_position, int child_position ) {
return child_position;
}
@Override
public int getChildrenCount( int plate_position ) {
return 1;
}
@Override
public View getGroupView( int plate_position, boolean is_expanded, View convert_view, ViewGroup parent ) {
//The supplied view may be null, so we check if we need a new one
if ( convert_view == null ) {
LayoutInflater _inflater = ( LayoutInflater ) this.context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
convert_view = _inflater.inflate( R.layout.list_recent_group, parent, false );
}
TextView list_header = ( TextView ) convert_view.findViewById( R.id.label_recent_group_name );
String header_title = getGroup( plate_position ).toString( );
list_header.setTypeface( null, Typeface.BOLD );
list_header.setText( header_title );
return convert_view;
}
@Override
public View getChildView( int plate_position, final int child_position, boolean is_last_child, View convert_view, ViewGroup parent ) {
//The supplied view may be null, so we check if we need a new one
if ( convert_view == null ) {
LayoutInflater infal_inflater = ( LayoutInflater ) this.context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
convert_view = infal_inflater.inflate(R.layout.list_recent_plate_info, parent, false );
}
TextView _make = ( TextView ) convert_view.findViewById( R.id.label_recent_plate_make_value );
String child_text = getChild( plate_position, child_position ).toString( );
_make.setText( child_text );
return convert_view;
}
@Override
public boolean hasStableIds( ) {
return false;
}
@Override
public boolean isChildSelectable( int plate_position, int child_position ) {
return true;
}
}
答案 0 :(得分:1)
刚才有我自己!
@Override
public long getGroupId( int plate_position ) {
return plate_position;
}
此方法仅返回位置,但当更改为对实际项目的引用时,它现在正常处理扩展状态。我把方法改为:
@Override
public long getGroupId( int plate_position ) {
return this.list_data.get( plate_position ).hashCode( );
}