簡體   English   中英

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

[英]Xamarin.Android multiple generic lists in RecyclerView

情況:我正在列出國家/地區。 在此列表中,我將有一個城市列表。 我想代表每個顯示城市的國家的名片視圖。

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);
    }
}

這正在起作用,我得到了每個國家的卡片視圖。 但是,獲得該國家卡片視圖中的城市列表的最佳方法是什么?

工作實例

在Xamarin.Forms中,您可以對項目進行分組。 我認為RecyclerView無法做到這一點嗎? 示例: https//dzone.com/articles/xamarin-forms-grouping-enabled-listview

我有一個國家清單。 在此列表中,我將有一個城市列表。 我想代表每個顯示城市的國家的名片視圖。

您可以使用ExpandableListView來實現此功能:

在垂直滾動的兩級列表中顯示項目的視圖。 這與ListView不同,它允許兩個級別:可單獨擴展以顯示其子級的組。 這些項目來自與此視圖關聯的ExpandableListAdapter。

這是例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM