Я написал демо об этом, это работает GIF.
Есть мой код 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;
}
xamlиз ListviewItem
<?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