I have a listView which shows a TextView and a Button. On clicking the button, I want to change the layout to a totally different layout. Here is my attempt at the same, but it doesn't show any changes (adapter class) :
public class ListViewAdapter extends ArrayAdapter<String> {
ArrayList<String> list;
public ListViewAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<String> objects) {
super(context, resource, objects);
this.list = objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull final ViewGroup parent) {
final View[] listItemView = {convertView};
if(listItemView[0] == null) {
listItemView[0] =
LayoutInflater.from(getContext()).inflate(R.layout.list_element_main, parent, false);
}
TextView textView = (TextView) listItemView[0].findViewById(R.id.main_text_view);
textView.setText(list.get(position));
Button button = (Button) listItemView[0].findViewById(R.id.switch_layout_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//switch layout:
//replace R.layout.list_element_main with
//R.layout_list_element_switch)
listItemView[0] = LayoutInflater.from(getContext()).inflate(R.layout.list_element_switch, parent, false);
ListViewAdapter.this.notifyDataSetChanged();
}
});
return listItemView[0];
}
}
The new Layout has multiple elements in it and I'm using it elsewhere too, so I wanted to re-use that layout instead of adding the same thing again in this layout (list_element_main) playing around with its visibility (gone
and visible
). Is there some way to achieve this?