Custom ListView with different row layouts to be inflated depending on webservice response

前端 未结 1 678
抹茶落季
抹茶落季 2021-01-29 16:31

I have ListView.

I have 3 sets of views:

  1. Imageview
  2. VideoView
  3. Textview.

I have a webserver which returns images, videos and

1条回答
  •  [愿得一人]
    2021-01-29 17:01

    Associate each webservice response with ID 0-for Images, 1-for Texts and 2-for Videos. Store ID in a ArrayList. Then Override getItemViewType() and getViewTypeCount(). Inflate appropriate views in getView().

       @Override
    
        public int getItemViewType(int position) 
    
        {
    
        int type;
    
        if (ID.get(position)== 0){
            type = TYPE_ITEM1;  //type 0 for image
        } else if (ID.get(position) == 1){
            type = TYPE_ITEM2; //type 1 for text
        }else {
            type = TYPE_ITEM3; //type 2 for videos
        }
        return type;
        }
    @Override
    public int getViewTypeCount() {
        return 3;    //three different layouts to be inflated
    }
    

    0 讨论(0)
提交回复
热议问题