Dynamic Item Size for TKListView

6 posts, 0 answers
  1. Burzin
    Burzin avatar
    4 posts
    Member since:
    Sep 2015

    Posted 10 Mar Link to this post

    I want to create a ListView which will have a multiline label. This label will have n number of lines. So, I want the ListView to adapt its ItemSize according to the height of Label. How can I achieve this? 

    Please provide code in C# as I am using Xamarin.
  2. Sophi
    Admin
    Sophi avatar
    99 posts

    Posted 15 Mar Link to this post

    Hi,

    You can easily do that with the following steps:

    1. Set the dynamicItemSize property of the list layout to true
    2.
    Provide a custom cell for the listview by inheriting from TKListViewCell

    You can check out our sdk examples, we have example for creating list with variable item size, here is link to the repo.

    Regards,
    Sophi
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Philipp
    Philipp avatar
    2 posts
    Member since:
    Sep 2015

    Posted 25 Jul Link to this post

    Can't find this property in TKListViewLinearLayout. Is their any way to get a dynamic height in Xamarin? 

    I'm using 2016.2.708.1 and latest Xamarin.

  5. Sophi
    Admin
    Sophi avatar
    99 posts

    Posted 28 Jul Link to this post

    Hi Philipp,

    Did you cast your layout object to TKListViewLinearLayout before accessing dynamicItemSize property.
    Consider the following snippet.
    var layout = list.Layout as TKListViewLinearLayout;
    layout.DynamicItemSize = true;
    In case this does not cover your scenario, write back any time.

    Regards,
    Sophi
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Philipp
    Philipp avatar
    2 posts
    Member since:
    Sep 2015

    Posted 28 Jul in reply to Sophi Link to this post

    How do I set the height of the single cell. The change of height in the CustomCell has no effect. She remains at an altitude of 1000. 
  7. Sophi
    Admin
    Sophi avatar
    99 posts

    Posted 02 Aug Link to this post

    Hi Phillip,

    You can set explicit height or dynamic size of the labels with a custom cell. The idea behind implementing dynamic layouts is that you are setting the dynamicItemSize property to true in order to notify the TKListView that the provided cell will be with dynamic layout. So you need to create a custom cell and specify it's layout logic. Good example is one of our SDK examples, we have created a cell with dynamic size based on the height of the textLabel. Consider following snippet.
    class ListViewDynamicSizeCell: TKListViewCell {
     
        var label: UILabel?
     
        override init(frame: CGRect) {
            super.init(frame: frame)
             
            label = UILabel(frame: CGRect.zero)
            label!.translatesAutoresizingMaskIntoConstraints = false
            label!.numberOfLines = 0
            self.addSubview(label!)
             
            let views = [ "v": label! ]
            self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[v]-10-|", options:NSLayoutFormatOptions(rawValue:0), metrics: nil, views: views))
            self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[v]-10-|", options:NSLayoutFormatOptions(rawValue:0), metrics: nil, views: views))
        }
     
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
         
        override func layoutSubviews() {
            self.label!.preferredMaxLayoutWidth = self.bounds.size.width-20
            super.layoutSubviews()
        }
    }

    In case this is not what you want, please send us some screenshots of the desired result or provide us with a bit more detailed description of what you are trying to achieve.

    Regards,
    Sophi
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft banner