This is a migrated thread and some comments may be shown as answers.

RadListView - set spanCount on screen orientation change

1 Answer 152 Views
ListView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michele
Top achievements
Rank 1
Michele asked on 10 Oct 2017, 11:20 AM

Hello,

I'm using Angular2 (Typescript), RadListView with ListViewStaggeredLayout.
I need to set the value for spanCount at runtime when the screen orientation changes.
I've set:

<ListViewStaggeredLayout tkListViewLayout
     scrollDirection="Vertical"
     [spanCount]="columns">
</ListViewStaggeredLayout>

 

and I set columns variable on screen orientation event. The function setOrientation is binded on orientationChange event:

 

import { on as applicationOn, off as applicationOff } from "application";
 
...
    ngOnInit() {  
      applicationOn("orientationChanged", (e)=>{ this.setOrientation(e); });  
    }
 
    ngOnDestroy() {
      applicationOff("orientationChanged", this.setOrientation);
    }   
 
    ngAfterViewInit() {
      this.setOrientation({newValue: 'portrait'})
    }
 
    setOrientation(e) {
      if(e.newValue=="portrait") {
        this.columns = N;
      } else {
        this.columns = M;
      }  
    }
...

 

It seems works fine but after a bit or if items are many or view is loading, it doesn't work anymore and I get the next exception:

ERROR Error: java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
JS:     android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2619)
JS:     android.support.v7.widget.RecyclerView$LayoutManager.assertNotInLayoutOrScroll(RecyclerView.java:7236)
JS:     android.support.v7.widget.StaggeredGridLayoutManager.assertNotInLayoutOrScroll(StaggeredGridLayoutManager.java:530)
JS:     android.support.v7.widget.StaggeredGridLayoutManager.setSpanCount(StaggeredGridLayoutManager.java:429)

 

How can I solve this? What's the best way to set spanCount on screen orientation change event?

 

Thanks

1 Answer, 1 is accepted

Sort by
0
Nick Iliev
Telerik team
answered on 10 Oct 2017, 12:58 PM
Hi Michele,

To change the spanCount on event you will need to redraw the ListViewStaggeredLayout so it could be measured properly. You can achieve that the following way:

Component file:
radList: RadListView;

onRadListLoaded(args) {
    this.radList = <RadListView>args.object;   
}
 
ngOnInit() { 
    this.items = this.itemService.getItems(); // using the template hello world app
 
    applicationOn("orientationChanged", (args: OrientationChangedEventData) => {
        console.log("orientationChanged");
 
        if(args.newValue == "portrait") {
            let staggeredLayout = new ListViewStaggeredLayout();
            staggeredLayout.scrollDirection = "Vertical";
            staggeredLayout.spanCount = 2;
 
            this.radList.listViewLayout = staggeredLayout;
        } else {
            let staggeredLayout = new ListViewStaggeredLayout();
            staggeredLayout.scrollDirection = "Vertical";
            staggeredLayout.spanCount = 3;
 
            this.radList.listViewLayout = staggeredLayout;
        }
    }); 
    }

And in the HTML file using the loaded event to get the reference to RadListView
<RadListView [items]="items" (loaded)="onRadListLoaded($event)">

For your convenience, I have created this demo application demonstrating where the above technique is implemented. 

Regards,
Nikolay Iliev
Progress Telerik
Did you know that you can open private support tickets which are reviewed and answered within 24h by the same team who built the components? This is available in our UI for NativeScript Pro + Support offering.
Tags
ListView
Asked by
Michele
Top achievements
Rank 1
Answers by
Nick Iliev
Telerik team
Share this question
or