问题
On app startup, all children of the LinearLayout (LL) are rendered. But, with the same code when coming from another activity, no children of the LL are visible, nevertheless included in the view tree. See:
The activity_main.xml
is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/notes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:background="@color/accent"> <!-- BG Color to quickly see if it is drawn -->
<!-- LinearLayout is removed or created in NoteRenderer#all-->
</LinearLayout>
</RelativeLayout>
The code which draws at least on app start the children is
public void all(ArrayList<Note> notes) {
LinearLayout linearLayout = main.findViewById(R.id.notes);
linearLayout.setBackgroundColor(main.getColor(R.color.accent));
linearLayout.setVisibility(View.VISIBLE);
for (int i = 0; !noneFound && i < notes.size(); i++) {
LayoutInflater.from(main).inflate(R.layout.note, linearLayout, true);
links.add(new NoteViewLink(notes.get(i), (RelativeLayout) linearLayout.getChildAt(linearLayout.getChildCount() - 1), i));
}
// This is what I have already tried
// linearLayout.setVisibility(View.VISIBLE);
// linearLayout.setVisibility(View.GONE);
// linearLayout.setVisibility(View.VISIBLE);
// linearLayout.invalidate();
// linearLayout.refreshDrawableState();
// linearLayout.requestLayout();
// linearLayout.forceLayout();
}
In the class rendering the above layout I just do:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I know the children are in the view tree since my debugging method with the LL as starting point prints the children, which are correctly there:
D: |— 1/8 RelativeLayout 2131296449
D: |— 1/4 MaterialCheckBox 2131296332
D: |— 2/4 MaterialTextView 2131296548
D: |— 3/4 MaterialTextView 2131296532
D: |— 4/4 AppCompatImageButton 2131296463
D: |— 2/8 RelativeLayout 2131296449
D: |— 1/4 MaterialCheckBox 2131296332
D: |— 2/4 MaterialTextView 2131296548
D: |— 3/4 MaterialTextView 2131296532
D: |— 4/4 AppCompatImageButton 2131296463
D: |— 3/8 RelativeLayout 2131296449
D: |— 1/4 MaterialCheckBox 2131296332
D: |— 2/4 MaterialTextView 2131296548
D: |— 3/4 MaterialTextView 2131296532
D: |— 4/4 AppCompatImageButton 2131296463
D: |— 4/8 RelativeLayout 2131296449
D: |— 1/4 MaterialCheckBox 2131296332
D: |— 2/4 MaterialTextView 2131296548
D: |— 3/4 MaterialTextView 2131296532
D: |— 4/4 AppCompatImageButton 2131296463
D: |— 5/8 RelativeLayout 2131296449
D: |— 1/4 MaterialCheckBox 2131296332
D: |— 2/4 MaterialTextView 2131296548
D: |— 3/4 MaterialTextView 2131296532
D: |— 4/4 AppCompatImageButton 2131296463
D: |— 6/8 RelativeLayout 2131296449
D: |— 1/4 MaterialCheckBox 2131296332
D: |— 2/4 MaterialTextView 2131296548
D: |— 3/4 MaterialTextView 2131296532
D: |— 4/4 AppCompatImageButton 2131296463
D: |— 7/8 RelativeLayout 2131296449
D: |— 1/4 MaterialCheckBox 2131296332
D: |— 2/4 MaterialTextView 2131296548
D: |— 3/4 MaterialTextView 2131296532
D: |— 4/4 AppCompatImageButton 2131296463
D: |— 8/8 RelativeLayout 2131296449
D: |— 1/4 MaterialCheckBox 2131296332
D: |— 2/4 MaterialTextView 2131296548
D: |— 3/4 MaterialTextView 2131296532
D: |— 4/4 AppCompatImageButton 2131296463
I'm happy about any suggestion or hint, first I had the LL in a ScrollView, but then the LL did not got drawn... maybe something with the rendering thread?
来源:https://stackoverflow.com/questions/62103972/children-of-linearlayout-not-drawn-when-returning-from-another-activity