package info.nissiy.gridlayoutsample;

import android.content.res.Resources;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity
        implements SwipeRefreshLayout.OnRefreshListener, EndlessScrollListener.OnLoadMoreListener {
    private static Handler handler = new Handler();
    private SwipeRefreshLayout swipeRefreshLayout;
    private Resources res;
    private PhotoGridAdapter adapter;
    private EndlessScrollListener scrollListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        res = getResources();

        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle(getString(R.string.app_name));
        }

        int spanCount = 2;
        if (Util.isLandscape(getApplicationContext())) {
            spanCount = 3;
        }

        adapter = new PhotoGridAdapter(this, new ArrayList<>(), spanCount);
        GridWithProgressLayoutManager layoutManager = new GridWithProgressLayoutManager(this, spanCount, adapter);
        scrollListener = new EndlessScrollListener(1, layoutManager);
        scrollListener.setOnLoadMoreListener(this);

        int unitMargin = res.getDimensionPixelSize(R.dimen.unit_margin);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addOnScrollListener(scrollListener);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, unitMargin));
        recyclerView.setAdapter(adapter);

        swipeRefreshLayout.setColorSchemeResources(R.color.color_primary);
        swipeRefreshLayout.setOnRefreshListener(this);
        loadData(1);
    }

    @Override
    public void onLoadMore(int currentPage) {
        loadData(currentPage);
    }

    private void loadData(final int page) {
        // Set the Stub for display the ProgressBar
        final ProgressStub progressStub = new ProgressStub();
        if (page > 1) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    adapter.add(progressStub);
                }
            });
        }

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Remove the Stub
                if (page > 1) {
                    adapter.remove(progressStub);
                }

                if (swipeRefreshLayout.isRefreshing()) {
                    swipeRefreshLayout.setRefreshing(false);
                }

                int startNum = 10 * (page - 1);
                int endNum = 10 * page - 1;
                for (; startNum <= endNum; startNum++) {
                    int suffix = startNum % 8;
                    int drawableResId = res.getIdentifier("photo" + suffix, "drawable", getPackageName());
                    adapter.add(new Photo(drawableResId));
                }
            }
        }, 2000);
    }

    @Override
    public void onRefresh() {
        adapter.clear();
        scrollListener.onRefresh();

        loadData(1);
    }

}

@@ -2506,22 +2506,16 @@
         
-final 
 Progress
@@ -2607,275 +2607,86 @@
 ost(
-new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    adapter.add(progressStub);%0A                %7D%0A            %7D);%0A        %7D%0A%0A        handler.postDelayed(new Runnable() %7B%0A            @Override%0A            public void run()
+() -%3E adapter.add(progressStub));%0A        %7D%0A%0A        handler.postDelayed(() -%3E
  %7B%0A 
@@ -2696,20 +2696,16 @@
         
-  
-  
 // Remov
@@ -2723,28 +2723,24 @@
             
-    
 if (page %3E 1
@@ -2751,36 +2751,32 @@
                 
-   
- 
 adapter.remove(p
@@ -2797,34 +2797,27 @@
             
-    %7D%0A%0A   
+%7D%0A%0A
             
@@ -2812,25 +2812,24 @@
             
- 
 if (swipeRef
@@ -2853,28 +2853,24 @@
 eshing()) %7B%0A
-    
             
@@ -2910,28 +2910,24 @@
 ing(false);%0A
-    
             
@@ -2937,28 +2937,24 @@
             
-    
 int startNum
@@ -2973,20 +2973,16 @@
 e - 1);%0A
-    
         
@@ -3025,20 +3025,16 @@
         
-    
 for (; s
@@ -3078,28 +3078,24 @@
             
-    
 int suffix =
@@ -3121,28 +3121,24 @@
             
- 
-   
 int drawable
@@ -3220,36 +3220,32 @@
                 
-    
 adapter.add(new 
@@ -3271,26 +3271,8 @@
 ));%0A
-                %7D%0A
     

use lambda