You can't do this only in XML. You'll need a little Java. You were on the right track with the layout weights.
Skeleton XML:
<LinearLayout
android:orientation="vertical">
<RecyclerView
android:id="@+id/recView1"
android:layout_height="0dp"
android:layout_weight="1"
/>
<RecyclerView
android:id="@+id/recView2"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
The next part needs to be done in Java.
Add a data observer to both adapters, and make sure to override all the methods and have them call onChanged()
. Then, inside onChanged()
, retrieve the count of the current adapter and set the RecyclerView's visibility appropriately.
Here's an example for the first one:
recView1.getAdapter().registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
recView1.setVisibility(recView1.getAdapter().getItemCount() > 0 ? View.VISIBLE : View.GONE);
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
onChanged();
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
onChanged();
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
onChanged();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
onChanged();
}
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
onChanged();
}
}
In practice, you should probably extract both observers to variables, so that you can unregister them when the containing Activity/Window/whatever is destroyed.