“ Xamarin Android”如何设置ListView的TextCell属性?

时间:2019-09-20 08:59:58

标签: xaml xamarin.android

在Xamarin Forms中,我们可以像这样设置TextCell属性吗?

        <ListView x:Name="WorkersListView" BackgroundColor="#ECEFF1" Margin="2" ItemSelected="WorkersListView_ItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell TextColor="Black" Text="{Binding WorkerName}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

通过编写ListView.ItemTemplateDataTemplate,我们可以访问TextCell

但是我该如何在 Xamarin Android 上做到这一点?

因为它没有ListView.ItemTemplateDataTemplate元素。

这是我当前在Xamarin.Android项目中的ListView

    <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/WorkersListView"
    android:background="@android:color/darker_gray"
    android:layout_margin="2dp">
</ListView>

谢谢。

2 个答案:

答案 0 :(得分:0)

在Xamarin Forms和Xamarin Android中使用ListView是不同的

添加看起来像这样的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">  
<ListView  
    android:id="@+id/listView"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:fastScrollEnabled="true" />  
</LinearLayout>   

然后创建ListView模板

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
android:orientation="horizontal"  
android:layout_width="fill_parent"  
android:layout_height="fill_parent"  
android:minWidth="25px"  
android:minHeight="25px"  
android:paddingBottom="4dp">  
<ImageView  
    android:id="@+id/photoImageView"  
    android:layout_width="60dp"  
    android:layout_height="60dp"  
    android:layout_alignParentLeft="true"  
    android:layout_centerVertical="true"  
    android:padding="5dp" />  
<TextView  
    android:id="@+id/nameTextView"  
    android:layout_toRightOf="@id/photoImageView"  
    android:text="Name"  
    android:textAppearance="?android:attr/textAppearanceLarge"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:paddingLeft="5dp" />  
<TextView  
    android:id="@+id/departmentTextView"  
    android:layout_toRightOf="@id/photoImageView"  
    android:layout_below="@id/nameTextView"  
    android:text="Department"  
    android:textAppearance="?android:attr/textAppearanceSmall"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:paddingLeft="5dp" />  
 </RelativeLayout>   

创建如下的适配器类:

public class MyCustomListAdapter : BaseAdapter<User>  
{  
    List<User> users;  

    public MyCustomListAdapter(List<User> users)  
    {  
        this.users = users;  
    }  

    public override User this[int position]  
    {  
        get  
        {  
            return users[position];  
        }  
    }  

    public override int Count  
    {  
        get  
        {  
             return users.Count;  
        }  
    }  

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

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

        if(view==null)  
        {  
            view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.userRow, parent, false);  

            var photo = view.FindViewById<ImageView>(Resource.Id.photoImageView);  
            var name = view.FindViewById<TextView>(Resource.Id.nameTextView);  
            var department = view.FindViewById<TextView>(Resource.Id.departmentTextView);  

            view.Tag = new ViewHolder() { Photo = photo, Name = name, Department = department };  
        }  

        var holder = (ViewHolder)view.Tag;  

        holder.Photo.SetImageDrawable(ImageManager.Get(parent.Context, users[position].ImageUrl));  
        holder.Name.Text = users[position].Name;  
        holder.Department.Text = users[position].Department;  


        return view;  

    } 
}   

详细的答案在博客中: https://www.c-sharpcorner.com/article/creating-custom-listview-in-xamarin-android-app/

答案 1 :(得分:0)

Android实例中的

ListView需要Adapter才能使用行视图中包含的数据。

添加一个ListView和一个TextView控件,以分别显示项目列表和元素。使用以下代码。

<?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="#6BC6ED"  
>  
<TextView  
   android:id="@+id/Name"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"   
   android:textSize="25dp"  
   android:textColor="#FFFFFF"  
/>  
<ListView  
   android:id="@+id/demolist"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content">   
</ListView>  
</LinearLayout>  

类似于我们在Forms中所做的那样,创建一个新的模型类,其中包含ViewCell中的绑定属性。

例如

public class Students {  
    public string Name {  
        get;  
        set;  
    }  
    public int Age {  
        get;  
        set;  
    }  
}

接下来,您需要创建Adapter来将数据绑定到ListView。 Android具有用于适配器实现的内置基本适配器类,以将ListView绑定到数据源。另外,我们可以在那里获取项目位置,数量和所选项目等。 Count方法用于获取列表中的行数。

GetView方法用于为每一行返回一个View,其中填充了哪个元素。

GetItemId方法用于返回行数据属性,例如行号和值。

public class StudentAdapter: BaseAdapter < Students > {  
    public List < Students > sList;  
    private Context sContext;  
    public StudentAdapter(Context context, List < Students > list) {  
        sList = list;  
        sContext = context;  
    }  
    public override Students this[int position] {  
        get {  
            return sList[position];  
        }  
    }  
    public override int Count {  
        get {  
            return sList.Count;  
        }  
    }  
    public override long GetItemId(int position) {  
        return position;  
    }  
    public override View GetView(int position, View convertView, ViewGroup parent) {  
        View row = convertView;  
        try {  
            if (row == null) {  
                row = LayoutInflater.From(sContext).Inflate(Resource.Layout.Main, null, false);  
            }  
            TextView txtName = row.FindViewById < TextView > (Resource.Id.Name);  
            txtName.Text = sList[position].Name;  
        } catch (Exception ex) {  
            System.Diagnostics.Debug.WriteLine(ex.Message);  
        } finally {}  
        return row;  
    }  
}  

接下来,打开您的活动文件并编写以下代码,以创建项目列表以显示ListView并显示列表中的选定项目。

private ListView studentlistView;  
private List < Students > mlist;  
StudentAdapter adapter;  
protected override void OnCreate(Bundle bundle) {  
    base.OnCreate(bundle);  
    // Set our view from the "main" layout resource  
    SetContentView(Resource.Layout.Main);  
    List < Students > objstud = new List < Students > ();  
    objstud.Add(new Students {  
        Name = "Jack", Age = 26  
    });  
    objstud.Add(new Students {  
        Name = "Mary", Age = 26  
    });  
    studentlistView = FindViewById < ListView > (Resource.Id.demolist);  
    mlist = new List < Students > ();  
    mlist = objstud;  
    adapter = new StudentAdapter(this, mlist);  
    studentlistView.Adapter = adapter;  
    studentlistView.ItemClick += StudentlistView_ItemClick;  
}  

要处理选定的项目,请编写以下代码。

private void StudentlistView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) {  
    var select = mlist[e.Position].Name;  
    Android.Widget.Toast.MakeText(this, select, Android.Widget.ToastLength.Long).Show();  
}

有关更多详细信息,请检查here