RecyclerView中的Xamarin.Android多个通用列表

时间:2018-02-05 10:05:13

标签: c# xamarin android-recyclerview xamarin.android

情况:我有一份国家名单。在这个列表中,我将列出一系列城市。我想代表每个显示城市的国家的cardview。

Carview1

Cardview2

Country.cs

public class Country
{
    public int CountryId { get; set; }
    public string Description { get; set; }
    public string Flag { get; set; }
    public IList<City> Cities { get; set; }
}

City.cs

public class City
{
    public int CityId { get; set; }
    public string Description { get; set; }
    public int Population { get; set; }
}

MainActivity.cs

public class MainActivity : Activity
{
    public IList<Country> Countries = new List<Country>();
    public IList<City> CitiesBelgium = new List<City>();
    public IList<City> CitiesGermany = new List<City>();

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    CountryAdapter countryAdapter;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        PopulateList(); //this will populate the desired lists in this example

        recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.SetLayoutManager(layoutManager);

        countryAdapter = new CountryAdapter(Countries);
        recyclerView.SetAdapter(countryAdapter);
    }
}

CountryAdapter.cs

public class CountryAdapter : RecyclerView.Adapter
{
    IList<Country> countries;
    public override int ItemCount
    {
        get { return countries.Count; }
    }

    public CountryAdapter(IList<Country> countries)
    {
        this.countries = countries;
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        CountryViewHolder vh = holder as CountryViewHolder;
        vh.Id.Text = countries[position].CountryId.ToString();
        vh.Description.Text = countries[position].Description;
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = LayoutInflater.From(parent.Context).
                Inflate(Resource.Layout.CountryCardView, parent, false);
        CountryViewHolder vh = new CountryViewHolder(itemView);
        return vh;
    }
}

CountryViewHolder.cs

public class CountryViewHolder : RecyclerView.ViewHolder
{
    public TextView Id { get; private set; }
    public TextView Description { get; private set; }

    public CountryViewHolder(View itemView) : base(itemView)
    {
        Id = itemView.FindViewById<TextView>(Resource.Id.textViewId);
        Description = itemView.FindViewById<TextView>(Resource.Id.textViewDescription);
    }
}

这是有效的,我得到了每个国家的cardview。 但是获取该国卡片视图的城市列表的最佳方法是什么?

Working example

在Xamarin.Forms中,您可以对项目进行分组。我不认为这可以使用RecyclerView吗?示例:https://dzone.com/articles/xamarin-forms-grouping-enabled-listview

1 个答案:

答案 0 :(得分:2)

  

我有一份国家/地区列表。在此列表中,我将列出一系列城市。我想代表每个显示城市的国家的cardview。

您可以使用ExpandableListView来实现此功能:

  

一个视图,显示垂直滚动的两级列表中的项目。这与ListView的不同之处在于允许两个级别:可以单独展开以显示其子级的组。这些项来自与此视图关联的ExpandableListAdapter。

这是example