我有一个包含listView的主布局,一个EditText和一个按钮。还有另一个名为row.axml的文件,其中包含一个TextView和一个ImageView。我创建了一个类AddItem.cs(Fragment),它在Main.axml中包含的listView中添加一个项目。但我不明白如何调用Additem.cs类来在列表视图中添加一个新项目,并在EditText中输入文本。使用按钮使用片段膨胀列表视图
Main.Axml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView android:id="@+id/in"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText android:id="@+id/edit_text_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="bottom"
/>
<Button android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
/>
</LinearLayout>
Row.Axml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/singleContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/singleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/bubble_green"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="@android:color/background_dark" />
</LinearLayout>
MainActivity:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
ImageButton btAdd = FindViewById<ImageButton> (Resource.Id.btnAdd);
_ListOfTexts = FindViewById<ListView> (Resource.Id.in);
btAdd.Click += delegate(object sender, EventArgs e) {
string txtAdded = FindViewById<EditText> (Resource.Id.edit_text_out).Text;
//add new row
};
}
个
Additem.cs
public class AddItem: Fragment
{
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return base.OnCreateView (inflater, container, savedInstanceState);
}
}
http://warting.github.io/AndroidChatBubbles/
“你怎么称呼班级”是什么意思? – Gumbo 2014-09-01 19:46:19
@Gumbo。使类AddItem添加项目从Main类(MainActivity)的列表视图 – 2014-09-01 19:54:08
因此,片段'AddItem'包含一个列表视图,你想添加项目从片段父Activity?你有什么试过/哪些代码不工作? – Gumbo 2014-09-01 19:58:07