getItemViewType
и getViewTypeCount
позволяет не беспокоиться о том, какая вьюшка нам приходит — она придёт либо верная, либо null в метод getView
. notifyDataSetChanged()
):listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
EditableAdapter adapter = (EditableAdapter) parent.getAdapter();
adapter.addToEditPositions(position);
adapter.notifyDataSetChanged();
}
});
public class EditableAdapter extends BaseAdapter {
private static final int NORMAL_STATE = 0;
private static final int EDIT_STATE = 1;
private List<Integer> mEditPositions = new ArrayList<Integer>();
@Override
public View getView(int position, View convertView, ViewGroup parent){
switch (getItemViewType(position)){
case NORMAL_STATE:
return getNormalView(position, convertView, parent);
case EDIT_STATE:
return getEditView(position, convertView, parent);
}
return null;
}
@Override
public int getItemViewType(int position){
return mEditPositions.contains(position) ? EDIT_STATE : NORMAL_STATE;
}
@Override
public int getViewTypeCount(){
return 2;
}
private View getNormalView(int position, View convertView, ViewGroup parent){
if (convertView == null){
convertView = View.inflate(convertView.getContext(), R.layout.normal_view, null);
}
...
return convertView;
}
private View getEditView(int position, View convertView, ViewGroup parent){
if (convertView == null){
convertView = View.inflate(convertView.getContext(), R.layout.edit_view, null);
}
...
return convertView;
}
@Override
public int getCount(){
...
}
@Override
public Object getItem(int position){
...
}
@Override
public long getItemId(int position){
...
}
}