c# - 在 ViewFlipper 中将视图添加到 LinearLayout
问题描述
我正在尝试将一些 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>
任何帮助将不胜感激!
解决方案
推荐阅读
- regex - 正则表达式 - 如何忽略以逗号结尾的数字
- c# - 由于我不能在 C# 中使用带有隐式变量的 lambda 函数,我可以在这里替换什么?
- soap - METRO SOAP Web 服务不适用于 Tomcat 9 和 Java 11
- postgresql - 在 postgresql 中使用替换更新语句
- validation - 关于表单验证
- amazon-web-services - Snowflake:数据从 AWS 美国拆分到 AWS 澳大利亚
- rest - 如何创建正确的 API URL 来获取数据
- python - 如何在python中格式化数字?
- python - PyLint:模块“main_app”中没有名称“新文件”
- mysql - Spring Boot JPA:如何将字段映射到数据类型不固定的列