我试图将多个LinearLayouts(用作页面,转换为ViewGroups以允许我查看子视图)添加到ViewFlipper(以允许我翻阅页面)。然后,我需要在页面上添加内容。
到目前为止,所有必要的页面均已创建,但是它们为空。 我尝试了两种方法来实现这一目标-
int currentNote = 0;
int currentPage = -1;
int pagesRequired = _numberOfPagesRequired;
// I am using ViewGroups as this is the only way I can find to add child LinearLayouts (stored in their own Views) to the page
ViewGroup[] notesPageInflatedLayout = new ViewGroup[pagesRequired];
foreach (CaseNote caseNote in foundCase.Notes)
{
// If currentNote is 0 or a multiple of 10
if (currentNote % 10 == 0)
{
// Increment the currentPage
currentPage++;
// Create a new page
LayoutInflater notesPageLayoutInflater = LayoutInflater.From(this);
notesPageInflatedLayout[currentPage] = notesPageLayoutInflater.Inflate(Resource.Layout.view_notes_page, null) as ViewGroup;
// Set the TextView at the top of the page to the page number
TextView temp = notesPageInflatedLayout[currentPage].FindViewById<TextView>(Resource.Id.temp);
temp.Text = currentPage.ToString();
// Add the page to the ViewFlipper
NotesViewFlipper.AddView(notesPageInflatedLayout[currentPage]);
}
// Create content item (note)
LayoutInflater notesLayoutInflater = LayoutInflater.From(this);
View notesInflatedLayout = notesLayoutInflater.Inflate(Resource.Layout.view_note, null);
TextView nameEntryTypeTextView = notesInflatedLayout.FindViewById<TextView>(Resource.Id.nameEntryTypeTextView);
nameEntryTypeTextView.Text = "TBI - " + caseNote.EntryType;
TextView detailsTextView = notesInflatedLayout.FindViewById<TextView>(Resource.Id.detailsTextView);
detailsTextView.Text = caseNote.Details;
TextView dateTextView = notesInflatedLayout.FindViewById<TextView>(Resource.Id.dateTextView);
dateTextView.Text = caseNote.Date.ToShortDateString();
// Add content item to current page and increment note number
notesPageInflatedLayout[currentPage].AddView(notesInflatedLayout);
currentNote++;
}
...和...
int currentPage = 0;
while (foundCase.Notes.Count > 0)
{
List<CaseNote> chunk = foundCase.Notes.Take(10).ToList();
foundCase.Notes.RemoveRange(0, chunk.Count);
LayoutInflater notesPageLayoutInflater = LayoutInflater.From(this);
ViewGroup notesPageInflatedLayout = notesPageLayoutInflater.Inflate(Resource.Layout.view_notes_page, null) as ViewGroup;
TextView temp = notesPageInflatedLayout.FindViewById<TextView>(Resource.Id.temp);
temp.Text = currentPage.ToString();
NotesViewFlipper.AddView(notesPageInflatedLayout);
foreach (CaseNote caseNote in chunk)
{
LayoutInflater notesLayoutInflater = LayoutInflater.From(this);
View notesInflatedLayout = notesLayoutInflater.Inflate(Resource.Layout.view_note, null);
TextView nameEntryTypeTextView = notesInflatedLayout.FindViewById<TextView>(Resource.Id.nameEntryTypeTextView);
nameEntryTypeTextView.Text = "TBI - " + caseNote.EntryType;
TextView detailsTextView = notesInflatedLayout.FindViewById<TextView>(Resource.Id.detailsTextView);
detailsTextView.Text = caseNote.Details;
TextView dateTextView = notesInflatedLayout.FindViewById<TextView>(Resource.Id.dateTextView);
dateTextView.Text = caseNote.Date.ToShortDateString();
notesPageInflatedLayout.AddView(notesInflatedLayout);
}
currentPage++;
}
当项目少于10个时,这两种方法都可以正常工作(因此仅添加一页)。请参见下面的屏幕截图。
预期的输出(仅创建一页时可见)-
创建多个页面时输出(所有页面均为空白)-
出于好奇,我尝试将ViewFlipper更改为LinearLayout。如预期的那样,所有页面都显示一个接一个。这使我相信这可能与我构建布局的方式有关。您可以找到我的AXML-
view_notes.axml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="25dp"
android:gravity="center_horizontal">
<ViewFlipper
android:id="@+id/notesViewFlipper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- <include layout="@layout/view_notes_page"/> -->
</ViewFlipper>
<RelativeLayout
android:id="@+id/navigationRelativeLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:visibility="gone">
<Button
android:text="Previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/previousButton"
android:visibility="invisible"/>
<Button
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:id="@+id/nextButton"/>
</RelativeLayout>
<Button
android:text="Add Note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addNoteButton"/>
</LinearLayout>
</ScrollView>
view_notes_page.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/notesLinearLayout"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_marginBottom="-5dp">
<TextView
android:text="Notes"
android:textColor="#000"
android:id="@+id/temp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"/>
<!-- <include layout="@layout/view_note"/> -->
</LinearLayout>
view_note.axml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/case_type_background_top"
android:padding="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentLeft="true"
android:gravity="center_vertical">
<TextView
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/nameEntryTypeTextView"/>
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/detailsTextView"/>
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dateTextView"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="right"
android:background="@drawable/case_type_background_bottom">
<ImageButton
android:tint="@android:color/white"
android:background="@null"
android:src="@drawable/baseline_fullscreen_24"
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/viewImageButton"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"/>
<ImageButton
android:tint="@android:color/white"
android:background="@null"
android:src="@drawable/baseline_edit_24"
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/editImageButton"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"/>
<ImageButton
android:tint="@android:color/white"
android:background="@null"
android:src="@drawable/baseline_remove_circle_24"
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/removeImageButton"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"/>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="15dp"/>
</LinearLayout>
任何帮助将不胜感激!