在您的 RecyclerView.Adapter 子类中,我们将其称为 customRAdapter,在您重写的 OnCreateViewHolder 方法中膨胀一个布局,该方法在垂直方向的 LinearLayout 中包含三个 EditText。接下来,向 customRAdapter 类添加一个公共方法,该方法返回一个字符串数组,其中包含三个 EditText 中的文本。从 RecyclerView 外部单击您的按钮时,请引用返回字符串数组的 customRAdapter 实例的方法,您的问题就解决了。
要在重写的 OnCreateViewHolder 方法 (LAYOUT_NAME.axml) 中扩展您的 XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/holo_blue_dark"
android:id="@+id/main_framelayout">
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/EditText1"
/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/EditText2"
/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/EditText3"
/>
</LinearLayout>
重写的 OnCreateViewHolder 方法本身:
public override RecyclerView.ViewHolder OnCreateViewHolder (ViewGroup parent, int viewType)
{
layoutinflater = LayoutInflater.From (context);
view = layoutinflater.Inflate (Resource.Layout.LAYOUT_NAME, parent, false);
return new customRecyclerHolder (view);
}
在您的 RecyclerView.ViewHolder 子类中有以下内容:
public EditText et1 {
get;
private set;
}
public EditText et2 {
get;
private set;
}
public EditText et3 {
get;
private set;
}
public customRecyclerHolder(View view):base(view){
et1 = view.FindViewById<EditText>(Resource.Id.EditText1);
et2 = view.FindViewById<EditText>(Resource.Id.EditText2);
et3 = view.FindViewById<EditText>(Resource.Id.EditText3);
}
获取字符串数组的示例(在您的 customRAdapter 类中):
customViewHolder holder;
public override void OnBindViewHolder (RecyclerView.ViewHolder holder, int position)
{
....
this.holder = holder as customViewHolder; //get the reference to the current viewholder
}
//call this method from your button code to retrieve the strings.
public string[] getEditTextStrings(){
return new string[]{ holder.et1.Text, holder.et2.Text, holder.et3.Text };
}
仅供参考,这一切都在 C# 中。 Java中的原理和几乎所有代码都差不多。