我在Xamarin android中有一个GridView,我希望能够使用grid.IsInEditMode
编辑它的行,但它是只读的,所以我找不到怎么做这一点。
答案 0 :(得分:0)
在android中编辑网格视图行
要自定义GridView
的行为以控制显示哪些行,您必须实现BaseAdapter
的子类,覆盖以下四项:
Here就是一个例子:
public class MyAdapter : BaseAdapter<TableItem> {
List<TableItem> items;
Activity context;
public MyAdapter(Activity context, List<TableItem> items) : base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override TableItem this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = context.LayoutInflater.Inflate(Resource.Layout.CustomView, null);
view.FindViewById<TextView>(Resource.Id.Text1).Text = item.Heading;
view.FindViewById<TextView>(Resource.Id.Text2).Text = item.SubHeading;
view.FindViewById<ImageView>(Resource.Id.Image).SetImageResource(item.ImageResourceId);
return view;
}
}
您可以编写自定义布局,并在GetView()
方法中将其用作GridView
的行布局。
CustomView.axml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="@drawable/CustomSelector">
<LinearLayout
android:id="@+id/Text"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip">
<TextView
android:id="@+id/Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF7F3300"
android:textSize="20dip"
android:textStyle="italic" />
<TextView
android:id="@+id/Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textColor="#FF267F00"
android:paddingLeft="100dip" />
</LinearLayout>
<ImageView
android:id="@+id/Image"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="5dp"
android:src="@drawable/icon"
android:layout_alignParentRight="true" />
</RelativeLayout>
将其用于GridView
:
gridView = FindViewById<GridView>(Resource.Id.grid);
gridView .Adapter = new MyAdapter(this, tableItems);
更新:
如何使其可编辑,就像当我单击一行时输入对话框出现,当我输入一些文本并单击确定时,行被编辑并获得已保存
每当您点击一行时,您都可以获得GridView
项,然后您可以使用您想要的任何内容更新文本,之后,您可以使用adapter.NotifyDataSetChanged()刷新GridView
}。正如你所说,要“拯救”。
以下是一个例子:
listView.ItemClick += OnListItemClick;
protected void OnListItemClick(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
{
var listView = sender as ListView;
TableItem t = tableItems[e.Position];
EditText inputServer = new EditText(this);
inputServer.Focusable = true;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("Dialog Title")
.SetMessage("Dialog Message")
.SetIcon(Resource.Drawable.Icon)
.SetView(inputServer)
.SetNegativeButton("Cancel", (sender2, e2) =>
{
Toast.MakeText(this, "Cancel!", ToastLength.Short).Show();
})
.SetPositiveButton("Change the text", (sender2, e2) =>
{
string inputName = inputServer.Text.ToString();
t.Heading = inputName;
Toast.MakeText(this, "Text changed!", ToastLength.Short).Show();
});
builder.Show();
adapter.NotifyDataSetChanged();
}