Android Xamarin中ListView中的按钮
创始人
2024-08-19 12:30:22
0

在Android Xamarin中,可以通过自定义适配器来实现ListView中的按钮。以下是一个简单的示例代码:

  1. 首先,创建一个自定义适配器类,继承自BaseAdapter:
public class MyListAdapter : BaseAdapter
{
    private List items;
    private Context context;

    public MyListAdapter(Context context, List items)
    {
        this.context = context;
        this.items = items;
    }

    public override string this[int position] => items[position];

    public override int Count => items.Count;

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

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;

        if (view == null)
        {
            LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
            view = inflater.Inflate(Resource.Layout.list_item, null);
        }

        TextView text = view.FindViewById(Resource.Id.text);
        Button button = view.FindViewById
  1. 在布局文件中定义ListView和list_item布局:

list_item.axml:




    

    

activity_main.axml:




    


  1. 在MainActivity.cs中使用自定义适配器设置ListView的适配器:
public class MainActivity : AppCompatActivity
{
    private ListView listView;
    private List items;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        listView = FindViewById(Resource.Id.listView);

        items = new List
        {
            "Item 1",
            "Item 2",
            "Item 3"
        };

        MyListAdapter adapter = new MyListAdapter(this, items);
        listView.Adapter = adapter;
    }
}

通过以上代码,就可以在ListView中显示带有按钮的列表项了。当按钮被点击时,会显示一个Toast消息。你可以根据自己的需求在按钮的点击事件中进行相应的处理。

相关内容

热门资讯

轻量版八分钟!wepoke有挂... 轻量版八分钟!wepoke有挂网上德州微扑克辅助(其实真的有挂)-知乎1、用户打开应用后不用登录就可...
挂一分钟!来玩德州app服务器... 挂一分钟!来玩德州app服务器在哪里德州aa辅助(果真真的有挂)-今日头条小薇(透视辅助)致您一封信...
插件1分钟!wpk ai是有w... 插件1分钟!wpk ai是有wopoker德州真的有挂(的确真的有挂)-哔哩哔哩1、完成wopoke...
安卓版本6分钟!轰趴大菠萝十三... 安卓版本6分钟!轰趴大菠萝十三水辅助德州ai智能辅助(果然真的有挂)-知乎1、轰趴大菠萝十三水辅助系...
ai辅助五分钟!微扑克的辅助工... 您好,云扑克是否有外挂这款游戏可以开挂的,确实是有挂的,需要了解加微【487309276】很多玩家在...
新版八分钟!aapoker辅助... 新版八分钟!aapoker辅助工具wpk俱乐部有外挂(好像真的有挂)-小红书1、下载好wpk俱乐部有...
安卓版九分钟!德州之星辅助we... 安卓版九分钟!德州之星辅助wepoke ai辅助(果然真的有挂)-微博客户端;1)德州之星辅助辅助挂...
渠道八分钟!pokernow可... 渠道八分钟!pokernow可以加注德州ai辅助神器(其实真的有挂)-小红书;1、让任何用户在无需p...
最新款7分钟!gg扑克辅助we... 最新款7分钟!gg扑克辅助wepoke模拟器(果然真的有挂)-百度知乎;1、打开软件启动之后找到中间...
安装1分钟!nzt德州辅助软件... 安装1分钟!nzt德州辅助软件微扑克辅助软件(都是真的有挂)-百度知乎;1、点击下载安装,微扑克辅助...