xamarin andriod popupwindow自定义列表视图itemclick事件不起作用

时间:2019-03-20 12:13:42

标签: xamarin popup native

list.ItemClick+=无法正常工作。如何在弹出窗口自定义listview上执行项目单击?在简单的listview中,itemclick事件可以工作,但是在弹出窗口中不会触发该事件。我需要获取listview项目值。

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="500dp"
  android:gravity="center"
  android:descendantFocusability="blocksDescendants"  
      android:layout_height="wrap_content"
  android:background="@android:color/background_light"
  android:weightSum="100">
  <LinearLayout
    android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_weight="10"
      android:layout_height="40dp">
  <TextView
      android:text="Vælg din afdeling"
      android:textSize="20sp"
      android:textColor="#FF222222"
      android:paddingLeft="30dp"
      android:focusable="false"
      android:focusableInTouchMode="false"
      android:layout_gravity="center"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:minWidth="25px"
      android:minHeight="25px"
      android:id="@+id/textView1" />

      </LinearLayout>
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="10">
      </LinearLayout>

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="100dp"
          android:layout_weight="50">
        <ListView
          android:minWidth="25px"
          android:minHeight="25px"
          android:focusable="false"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/listView1" />
      </LinearLayout>
  <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:layout_marginTop="35dp">
      <View
          android:layout_width="0dp"
          android:layout_height="0dp"
          android:layout_weight="1"/>

      <Button
          android:id="@+id/btnAddExpense"
          android:layout_width="wrap_content"
          android:layout_height="45dp"
          android:textColor="#61222222"
          android:background="@null"
          android:text="Annuller"
          android:layout_marginLeft="20dp"
          android:layout_gravity="right"
          android:layout_marginRight="15dp" />


      <Button
          android:id="@+id/btnok"
          android:layout_width="wrap_content"
          android:layout_height="45dp"
          android:textColor="#FFF62F5E"
          android:text="Gem"
          android:background="@null"
          android:layout_marginLeft="1dp" 
          android:layout_gravity="right"
          android:layout_marginRight="15dp" />

  </LinearLayout>
</LinearLayout>

在此功能中,我在listview上设置了项目单击事件,但是它不起作用。我该如何执行itemclick

private void DepartmentPicker_Click(object sender, EventArgs e)
{
    ButtonNext.Visibility = ViewStates.Invisible;
    GetListView.Adapter = new DepartmentListAdapter(this, departments);

    bool focusable = true;
    int width = 350;// LinearLayout.LayoutParams.WrapContent;
    int height = 450;//LinearLayout.LayoutParams.WrapContent;
    _view.FindViewById<Button>(Resource.Id.btnok).SetOnClickListener(this);
    var list= _view.FindViewById<ListView>(Resource.Id.listView1);

    popupWindow = new PopupWindow(_view, width, height, focusable);
    popupWindow.ContentView = _view;
    popupWindow.ShowAtLocation(_view, GravityFlags.CenterVertical, 0, 0);
    popupWindow.Focusable = true;
    popupWindow.Touchable = true;
    //listView.ChoiceMode = ChoiceMode.Single;
}

1 个答案:

答案 0 :(得分:2)

我编写了一个演示,它正在运行GIF。 enter image description here

我的密码为MainActivity.cs

   public class MainActivity : AppCompatActivity
{

    List<News> data;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
       SetContentView(Resource.Layout.activity_main);

        Button button1 = FindViewById<Button>(Resource.Id.button1);
        button1.Click += (o, e) =>
        {
            var popup = OnClick();
            popup.ShowAsDropDown((View)o, 0, 0);
        };

         data = new List<News>() {
            new News ("aaaaaaaa",1200),
            new News ("bbbbbbbbb",560),
            new News ("ccccccccc",158200),
            new News ("ddddddddd",900),

        };
     //   adapter = new NewsAdapter(data, this);

    }

    private PopupWindow OnClick()
    {

        PopupWindow _popupWindow = new PopupWindow(this);

       LayoutInflater inflater= (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
        View popup=inflater.Inflate(Resource.Layout.window_popup_content,null);
        ListView listView1 = popup.FindViewById<ListView>(Resource.Id.listView1);
        NewsAdapter adapter = new NewsAdapter(data, this);
        listView1.Adapter = adapter;



        listView1.ItemClick += (sender, args) =>
        {


             Toast.MakeText(this, data[args.Position].Title + "", ToastLength.Short).Show();
            _popupWindow.Dismiss();
        };

        _popupWindow.Width = ViewGroup.LayoutParams.WrapContent;
        _popupWindow.Height = ViewGroup.LayoutParams.WrapContent;
        _popupWindow.ContentView = popup;
        return _popupWindow;
    }
}

listview的适配器。

    public class NewsAdapter : BaseAdapter
{
    private List<News> data;
    private Context context;
    public override int Count
    {
        get
        {
            return data.Count;
        }
    }

    public NewsAdapter(List<News> data, Context context)
    {
        this.data = data;
        this.context = context;
    }


    public override Java.Lang.Object GetItem(int position)
    {
        return null;
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        convertView = LayoutInflater.From(context).Inflate(Resource.Layout.lv_test, parent, false);
        TextView title = convertView.FindViewById<TextView>(Resource.Id.tv_title);
        TextView pv = convertView.FindViewById<TextView>(Resource.Id.tv_pv);
        pv.Text = data[position].Pv.ToString();
        title.Text = data[position].Title;
        return convertView;

    }

ListviewItem的xaml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
<LinearLayout
    android:id="@+id/layout_content"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:orientation="horizontal"
    android:gravity="center_vertical">
    <TextView
        android:id="@+id/tv_title"
        android:layout_height="20dp"
        android:layout_width="0dp"
        android:layout_weight="5"
        android:textColor="#000000"
        android:text="aaaaaaaaa"
        android:textSize="16dp" />
    <TextView
        android:id="@+id/tv_pv"
        android:layout_height="10dp"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textColor="#808080"
        android:textSize="10dp"
        android:text="19665"

        android:gravity="right|center_vertical" />
</LinearLayout>
<View
    android:layout_height="1dp"
    android:layout_width="match_parent"
    android:background="#dedede" />
</LinearLayout> 

有我的演示。你可以参考一下。 https://github.com/851265601/ListviewPopUpWindowDemo