0

Want to do

Is it able to display 2 kinds of lists in a ListView?

Now, I display 1 pattern of log(lists) but I have to display one more new pattern on same page. Therefore I need to display 2 patterns of lists on same page.I was using ListView and BaseAdapter but I have no idea to show 2 pattarns.

If a data includes C, shows pattern1 and if includes G, shows pattern2.

【image】

ーーーーーー

A、B、C

D、E、F

ーーーーーー

A、B、G

D、H、I、J

ーーーーーー

A、B、C

D、E、F

ーーーーーー

A、B、C

D、E、F

ーーーーーー

A、B、G

D、H、I、J

ーーーーーー

Jason
  • 86,222
  • 15
  • 131
  • 146
yuki yuki
  • 45
  • 6

1 Answers1

0

You can try to use RecyclerView to achieve this.

When RecyclerView has multiple ViewHolders, we usually override GetItemViewType method.

getItemViewType(int position)

This method's default implementation will always return 0, indicating that there is only 1 type of view. In your case, it is not so, and so you will need find a way to assert which row corresponds to which view type.

Besides,when we should notice the viewType parameter of following method:

public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)

According to the view type, we'll need to inflate the correct layout resource and create our view holder accordingly. The RecyclerView will handle recycling different view types in a way which avoids clashing of different view types.

For example: (assume you can match your viewholders with your object's field Type)

private  const int LAYOUT_ONE = 0;
private  const int LAYOUT_TWO = 1;

method GetItemViewType:

public override int GetItemViewType(int position)
{
    if (items[position].Type == 0)
        return LAYOUT_ONE;
    else
        return LAYOUT_TWO;
}

method OnCreateViewHolder

public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = null;
        switch (viewType)
        {
            case LAYOUT_ONE:
                view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.customview_user_writepostbar, parent, false);
                return new CreatePostViewHolder(view);

            case LAYOUT_TWO:
                view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.customview_postregular, parent, false);
                return new PostRegularViewHolder(view);

        }
    }

method OnBindViewHolder

public override void 
        OnBindViewHolder (RecyclerView.ViewHolder holder, int position)
    {
        int type = GetItemViewType(position);

        switch (type)
        {
            case LAYOUT_ONE:
                CreatePostViewHolder vh2 = holder as CreatePostViewHolder;
                vh2.userFirstName.Text = UserFirstName + ", share something inspiring!";
                break;
            case LAYOUT_TWO:
                PostRegularViewHolder vh = holder as PostRegularViewHolder;
                // other code
                break;
            default:
                break;
        }
    }
Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
  • Thank you for polite answer! I have a question about inside of GetItemViewType(int position) . **items[position].Type** makes error because of **.Type**. You mean **position == 0** ? – yuki yuki Oct 21 '22 at 09:38
  • The `Type` is a field of an item. I divide the different layout types according to this field. You can also divide different layout types based on a field of your item model. – Jessie Zhang -MSFT Oct 24 '22 at 02:23
  • You can refer to this thread for how to use `RecyclerView`: [Trying to learn how to use getItemViewType() with RecyclerView](https://stackoverflow.com/questions/46698897/trying-to-learn-how-to-use-getitemviewtype-with-recyclerview) . – Jessie Zhang -MSFT Oct 24 '22 at 02:27
  • Thank you so much!!! Finally I understand the meaning of Type and I did it. – yuki yuki Oct 25 '22 at 03:05