Home > Net >  different patterns in one ListView
different patterns in one ListView

Time:10-25

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

ーーーーーー

CodePudding user response:

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;
        }
    }
  • Related