:
Add both views as children of the same layout (item_example.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<View
android:id="@+id/v_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<View
android:id="@+id/v_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
</LinearLayout>
and decide which one to show at runtime depending on your desired condition:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View exampleView = convertView;
if (exampleView == null) {
LayoutInflater inflater = fragment.getActivity().getLayoutInflater();
exampleView = inflater.inflate(R.layout.item_example, parent, false);
}
View view1 = exampleView.findViewById(R.id.v_first);
View view2 = exampleView.findViewById(R.id.v_second);
if () {
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
} else {
view2.setVisibility(View.VISIBLE);
view1.setVisibility(View.GONE);
}
return exampleView;
}
source
share