It is necessary to redefine getItemViewType in the adapter and in getView to return the desired view depending on the type. Below is an example, put it blindly.
@Override public int getItemViewType(int position) { if (needSwitch) return 1; else return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(getItemViewType(position) == 0) { ViewWithoutSwitch view; if (convertView != null && convertView instanceof ViewWithoutSwitch) { view = (ViewWithoutSwitch) convertView; } else { view = new ViewWithoutSwitch(parent.getContext()); } view.bind(getItem(position)); return view; } else { ViewWithSwitch view; if (convertView != null && convertView instanceof ViewWithSwitch) { view = (ViewWithSwitch) convertView; } else { view = new ViewWithSwitch(parent.getContext()); } view.bind(getItem(position)); return view; } }
You can do differently. Make one view and add a Switch there, and through bind set the desired visibility. Be sure to reuse convertView.