Saturday, December 16, 2017

Android RecyclerView Add Load More Functionality

Hello,

Recently I was working on adding load more functionality on Android RecyclerView. The purpose was to load more data as soon as user scrolls to bottom and there are no records left.

Since we already have used swipe to refresh plugin other load more plugin was not working as the event was not attached and fired. If we remove swipe to remove then it worked but we needed both functionalities. So for that we did simple trick. Here is the code.

testRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        if (!recyclerView.canScrollVertically(1)) {
            Toast.makeText(getApplicationContext(),"CALL LOAD MORE FUNCTION TO LOAD MORE DATA",Toast.LENGTH_LONG).show();
        }
    }
});

So trick is very simple, we added on scroll listener to recycler view and just checked it can not scroll any more vertically that means we reached at bottom and form here we can add logic to load more data.

Hope this helps you.

Sunday, December 10, 2017

AngularJs - Get Controller Scope in Directive

Recently I was learning AngularJs 5 and where I faced problem. I have to call controller function from directive. After struggle of half an hour, I was able to solve it. In this blog I am going to explain how to do this.

First of all give id to HTML tag where you have added ng-controller directive.

<div ng-controller="MyController" id="myControllerDiv">
</div>

Now in directive or any external JavaScript code where you want to get contoller scope, use following code.

var element  = document.getElementById("myControllerDiv");

This will give us that element, we will find it's corresponding angular element.

var angularElement  = angular.element(element);

Now we will get it's scope and using that scope, we can all any function of MyController

angularElement.scope().myFunction()

This will call myFunction defined in MyController.

Hope this helps you.