【问题标题】:I do not understand why when selection in the ListView is disappearing我不明白为什么 ListView 中的选择消失了
【发布时间】:2019-12-18 11:44:51
【问题描述】:

我想选择 ListView 中的一项,然后按编辑按钮,该按钮对选定的 Listview 项进行处理。但是只要我从列表视图中移动鼠标,选择就会消失。 (我在VS模拟器上测试) 我确实在 Internet 上查找了示例,但它们使用了根本不存在的不同属性。

这里是axml的部分代码:

    <TableLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout4">
        <TableRow>
            <TextView
                android:text="Ingredients List :"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/textView1" />
            <Button
                android:text="Edit"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/editIngerientsButton" />
        </TableRow>
        <TableRow>
            <ListView
                android:minWidth="25px"
                android:minHeight="50px"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/ingredientsListView"
                android:layout_span="2"
                android:choiceMode="singleChoice" />
        </TableRow>
    </TableLayout>

这里是代码

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

            ingredientsListView = (ListView) FindViewById(Resource.Id.ingredientsListView);
            ingredients = new List<Ingredient>();
            arrayAdapter = new ArrayAdapter<Ingredient>(this,
                Android.Resource.Layout.SimpleListItem1, ingredients);
            ingredientsListView.Adapter = arrayAdapter;
            ingredientsListView.ChoiceMode = ChoiceMode.Single;

            editIngredientsButton = (Button) FindViewById(Resource.Id.editIngerientsButton);
            editIngredientsButton.Click += EditIngredientsButtonOnClick;
     }

【问题讨论】:

    标签: c# listview select xamarin.android


    【解决方案1】:

    您应该为 ListView 自定义 Adapter。将 selectItem 属性添加到适配器中。选中项目后,更改项目单元格的状态。

    ItemSelectedAdapter.cs

    public class ItemSelectedAdapter : BaseAdapter<string>
    {
        string[] items;
        Activity context;
        public int selectItem = -1;
    
        public ItemSelectedAdapter(Activity context, string[] items) : base()
        {
            this.context = context;
            this.items = items;
        }
        public override long GetItemId(int position)
        {
            return position;
        }
        public override string this[int position]
        {
            get { return items[position]; }
        }
        public override int Count
        {
            get { return items.Length; }
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView; // re-use an existing view, if one is available
            if (view == null) // otherwise create a new one
                view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
            view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position];
            if (selectItem == position)
            {
                view.FindViewById<TextView>(Android.Resource.Id.Text1).SetBackgroundColor(Android.Graphics.Color.Yellow);
            }
            else
            {
                view.FindViewById<TextView>(Android.Resource.Id.Text1).SetBackgroundColor(Android.Graphics.Color.Transparent);
            }
    
            return view;
        }
    
        public void setSelectItem(int selectItem)
        {
            this.selectItem = selectItem;
        }
    }
    

    SimpleListItem1.xml

    <?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">
    
        <TextView
            android:id="@+id/Text1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </LinearLayout>
    

    终于在Activity

    ListView listView;
    string[] items;
    ItemSelectedAdapter itemSelectedAdapter;
    Button editButton;
    
    
    listView = FindViewById<ListView>(Resource.Id.listView1);
    items = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" };
    itemSelectedAdapter= new ItemSelectedAdapter(this, items);
    listView.Adapter = itemSelectedAdapter;
    listView.ItemClick += ListView_ItemClick;
    
    editButton= FindViewById<Button>(Resource.Id.button2);
    editButton.Click += Button2_Click;
    
    private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        itemSelectedAdapter.setSelectItem(e.Position);
        Console.WriteLine("selected is:" + e.Position);
        itemSelectedAdapter.NotifyDataSetChanged();
    }
    
    private void Button2_Click(object sender, EventArgs e)
    {
        int selectItemID = (int)homeScreenAdapter.selectItem;
        Console.WriteLine("selected is:" + selectItemID);
        items[selectItemID] = atv_content.Text;
        homeScreenAdapter.NotifyDataSetChanged();
     }
    

    【讨论】:

    • 这不是我问的。当我按下按钮时,想要在 ListView 中进行选择。列表本身应该什么都不做。
    • @ tanya hi,选择列表单元格时,我的gif也按一个按钮。没有键理解你的问题,你可以显示图像来解释。我会chek它。 span>
    • @Tanya 我已经更新了我的 gif,如果这是你想要的,我会更新答案中的代码。
    • @Tanya Okey,我已经更新了我的答案。如果有帮助,感谢您提前标记。
    猜你喜欢
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2016-07-10
    • 1970-01-01
    相关资源
    最近更新 更多