Android-如何在xamarin的列表视图中使用OnItemLongClick

时间:2016-12-09 11:32:38

标签: android xamarin.android

Android - 如何在Xamarin的列表视图中使用OnItemLongClick

我正在使用Xamarin Android开发应用程序。在该应用程序中,我正在显示自定义列表。我想使用

显示弹出窗口
listView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)

在弹出窗口中,我需要显示两个选项Update和delete。并根据用户选择我想进一步处理。 任何人都可以给我一些我可以使用的例子吗?

1 个答案:

答案 0 :(得分:1)

  

在弹出窗口中,我需要显示两个选项Update和delete。根据用户的选择,我想进一步处理。

当使用ListView更新项目时,您需要通知NotifyDataSetChanged()更改其视图。例如:

Main.axml:

<ListView
    android:id="@+id/lv"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />

MainActivity:

public class MainActivity : Activity
{
    public static List<string> items = new List<string>();
    public static MainAdapter adapter;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        for (int i = 0; i < 10; i++)
        {
            items.Add("Item " + i);
        }
        adapter = new MainAdapter(this, items);

        ListView lv = (ListView)FindViewById(Resource.Id.lv);
        lv.Adapter = adapter;

        lv.ItemLongClick += Lv_ItemLongClick;
    }

    private void Lv_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
    {
        var t = items[e.Position];
        Android.Widget.Toast.MakeText(this, t, Android.Widget.ToastLength.Short).Show();
        Console.WriteLine("Clicked on " + t);
        //show dialog and set params
        FragmentTransaction ft = FragmentManager.BeginTransaction();
        ListViewEditFragment dialog = new ListViewEditFragment();
        Bundle args = new Bundle();
        args.PutString("item", t);
        dialog.Arguments = args;
        dialog.Show(ft, "TAG");
    }
}

对话框布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <Button
      android:id="@+id/btn_item_delete"
      android:text="delete this item"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content" />
  <Button
      android:id="@+id/btn_item_edit"
      android:text="edit this item"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content" />
  <EditText
      android:id="@+id/item_editor"
      android:layout_height="50dp"
      android:layout_width="300dp"
      android:enabled="false" />
  <Button
      android:id="@+id/btn_commit"
      android:text="Commit"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content" />
</LinearLayout>

我的ListViewEditFragment用于对话:

public class ListViewEditFragment : DialogFragment
{
    private EditText ediotr;

    private string item;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        var args = Arguments;
        item = args.GetString("item");
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate(Resource.Layout.ListViewEditPopup, container, false);

        Button btnDelete = (Button)view.FindViewById(Resource.Id.btn_item_delete);
        btnDelete.Click += BtnDelete_Click;

        Button btnEdit = (Button)view.FindViewById(Resource.Id.btn_item_edit);
        btnEdit.Click += BtnEdit_Click;

        ediotr = (EditText)view.FindViewById(Resource.Id.item_editor);
        ediotr.Text = item;

        Button btnCommit = (Button)view.FindViewById(Resource.Id.btn_commit);
        btnCommit.Click += BtnCommit_Click;
        return view;
    }

    private void BtnDelete_Click(object sender, EventArgs e)
    {
        MainActivity.items.Remove(item);
        MainActivity.adapter.NotifyDataSetChanged();
        this.Dismiss();
    }

    private void BtnEdit_Click(object sender, EventArgs e)
    {
        ediotr.Enabled = true;
    }

    private void BtnCommit_Click(object sender, EventArgs e)
    {
        var index = MainActivity.items.FindIndex(s => s == item);
        MainActivity.items[index] = ediotr.Text;
        MainActivity.adapter.NotifyDataSetChanged();
        this.Dismiss();
    }
}

渲染此演示的图像:

enter image description here