1

Situation: I'm having a list of countries. Within this lists I'll have a list of cities. I want to represent a cardview for each country that shows the cities.

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

This is working and I get a cardview for each country. But what is the best way to get the list of cities in the cardview of the country?

Working example

In Xamarin.Forms you can group items. I don't think this is possible with RecyclerView? Example: https://dzone.com/articles/xamarin-forms-grouping-enabled-listview

7
  • Hi, hope this helps! 1. thing: Currently, you are setting the ItemCount to "2" - for each of your countries - but this mean, that you will only draw "2" cells. You need this to include both "header/country" cells, and "city" cells. Commented Feb 5, 2018 at 13:31
  • 2. override "GetItemViewType(int position)" and define wheter it is a Header or a City (indicate with an integer or enum) Commented Feb 5, 2018 at 14:57
  • 3. In "RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)" you will create the viewholder based on the viewtype. Commented Feb 5, 2018 at 14:58
  • 4. in "OnBindViewHolder(RecyclerView.ViewHolder holder, int position)" you cast to Header or City (based on the type of the holder). And then you populate the holder with data. Commented Feb 5, 2018 at 15:02
  • 4.1: but the data you put in is like: "cell at pos 0" -> Country name belgium... "cell at pos 2" -> City data Ghent... "cell at pos 3" -> Country name Germany... Commented Feb 5, 2018 at 15:04

1 Answer 1

2

I'm having a list of countries. Within this lists I'll have a list of cities. I want to represent a cardview for each country that shows the cities.

You could use ExpandableListView to implement this feature:

A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.

Here is the example.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your comment. I think this will help me a lot. But can you also implement more levels than only 2? In my real world application I need 3 levels of data. Grouping - detail - and a list of a the detail.
@JeffreyM, this example is 3 levels of data, :).
@JeffreyM, and you could refer to this: stackoverflow.com/questions/42627161/…, hope this ca help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.