Implementing Pull to Refresh for RecyclerView

apply plugin: 'com.android.application'

//...

dependencies {
    // ...
    compile 'com.android.support:support-v4:25.3.1'
}
allprojects {
    repositories {
        // add below
        maven { url "https://maven.google.com" }
    }
}
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <android.support.v7.widget.RecyclerView
      android:id="@+id/rvItems"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true" />

</android.support.v4.widget.SwipeRefreshLayout>
public class TimelineActivity extends Activity {
    private SwipeRefreshLayout swipeContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Only ever call `setContentView` once right at the top
        setContentView(R.layout.activity_main);
        // Lookup the swipe container view
        swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
        // Setup refresh listener which triggers new data loading
        swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Your code to refresh the list here.
                // Make sure you call swipeContainer.setRefreshing(false)
                // once the network request has completed successfully.
                fetchTimelineAsync(0);
            } 
        });
        // Configure the refreshing colors
        swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright, 
                android.R.color.holo_green_light, 
                android.R.color.holo_orange_light, 
                android.R.color.holo_red_light);
    }

    public void fetchTimelineAsync(int page) {
        // Send the network request to fetch the updated data
        // `client` here is an instance of Android Async HTTP
        // getHomeTimeline is an example endpoint.
        client.getHomeTimeline(new JsonHttpResponseHandler() {
            public void onSuccess(JSONArray json) {
                // Remember to CLEAR OUT old items before appending in the new ones
                adapter.clear();
                // ...the data has come back, add new items to your adapter...
                adapter.addAll(...);
                // Now we call setRefreshing(false) to signal refresh has finished
                swipeContainer.setRefreshing(false);
            }

            public void onFailure(Throwable e) {
                Log.d("DEBUG", "Fetch timeline error: " + e.toString());
            }
        });
    }
}

References
http://guides.codepath.com/android/implementing-pull-to-refresh-guide

Save scroll position of RecyclerView in Android

Method 1 :

@onPause

long currentVisiblePosition = 0; // this variable should be static in class
currentVisiblePosition = ((LinearLayoutManager)rv.getLayoutManager()).findFirstCompletelyVisibleItemPosition();

@onResume

((LinearLayoutManager) rv.getLayoutManager()).scrollToPosition(currentVisiblePosition);
currentVisiblePosition = 0;

Method 2 :

protected void onSaveInstanceState(Bundle state) {
     super.onSaveInstanceState(state);

     // Save list state
     mListState = mLayoutManager.onSaveInstanceState();
     state.putParcelable(LIST_STATE_KEY, mListState);
}
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);

    // Retrieve list state and list/item positions
    if(state != null)
        mListState = state.getParcelable(LIST_STATE_KEY);
}

if we are in fragment onRestoreInstanceState is not available so we can use this :

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
        // Retrieve list state and list/item positions
    if(state != null)
        mListState = state.getParcelable(LIST_STATE_KEY);
    }
}

Then update the LayoutManager (for example in onResume() or after loading recyclerView):

@Override
protected void onResume() {
    super.onResume();

    if (mListState != null) {
        mLayoutManager.onRestoreInstanceState(mListState);
    }
}

References
https://stackoverflow.com/questions/36568168/how-to-save-scroll-position-of-recyclerview-in-android
https://stackoverflow.com/questions/27816217/how-to-save-recyclerviews-scroll-position-using-recyclerview-state
https://stackoverflow.com/questions/43857824/kotlin-static-methods-and-variables
https://stackoverflow.com/questions/28236390/recyclerview-store-restore-state-between-activities

Create fullscreen Activity in Android

public class ActivityName extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // remove title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
    }
}

AndroidManifest.xml

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.AppCompat.Light.NoActionBar"/>

References
https://developer.android.com/training/system-ui/index.html
https://stackoverflow.com/questions/2868047/fullscreen-activity-in-android

Pre Caching LayoutManager for RecyclerView

PreCachingLayoutManager.java

public class PreCachingLayoutManager extends LinearLayoutManager {
    private static final int DEFAULT_EXTRA_LAYOUT_SPACE = 600;
    private int extraLayoutSpace = -1;
    private Context context;
 
    public PreCachingLayoutManager(Context context) {
        super(context);
        this.context = context;
    }
 
    public PreCachingLayoutManager(Context context, int extraLayoutSpace) {
        super(context);
        this.context = context;
        this.extraLayoutSpace = extraLayoutSpace;
    }
 
    public PreCachingLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
        this.context = context;
    }
 
    public void setExtraLayoutSpace(int extraLayoutSpace) {
        this.extraLayoutSpace = extraLayoutSpace;
    }
 
    @Override
    protected int getExtraLayoutSpace(RecyclerView.State state) {
        if (extraLayoutSpace > 0) {
            return extraLayoutSpace;
        }
        return DEFAULT_EXTRA_LAYOUT_SPACE;
    }
}

References
https://androiddevx.wordpress.com/2014/12/05/recycler-view-pre-cache-views/

Working with Glide, Image Loader Library for Android

build.gradle

repositories {
  mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
  compile 'com.github.bumptech.glide:glide:4.0.0'
  compile 'com.android.support:support-v4:25.3.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'
}

ProGaurd

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

# for DexGuard only
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule

Generated API

MyAppGlideModule.java

package com.example.myapp;
   
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
   
@GlideModule
public final class MyAppGlideModule extends AppGlideModule {}

Kotlin

apply plugin: 'kotlin-kapt'
kapt 'com.github.bumptech.glide:compiler:4.0.0'

Use :

            GlideApp.with(context)
                    .load(image.getFile())
                    .override(measuredWidth,measuredWidth)
                    .centerCrop()
                    .placeholder(context.getResources().getDrawable(R.mipmap.image_placeholder))
                    .into(holder.imageViewThumbnail);

References
http://bumptech.github.io/glide/
https://github.com/bumptech/glide
http://bumptech.github.io/glide/doc/generatedapi.html
https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

Android Requesting Permissions at Run Time

public class ThumbnailFragment extends Fragment {

    private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1000;
    private List<ImageGroup> imageGroupList;
    private RecyclerView recyclerViewImages;
    private ThumbnailPageAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;

    public ThumbnailFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_thumbnail, container, false);

        layoutManager = new LinearLayoutManager(getContext());

        adapter = new ThumbnailPageAdapter();

        recyclerViewImages = (RecyclerView) view.findViewById(R.id.recyclerViewImages);
        recyclerViewImages.setLayoutManager(layoutManager);
        recyclerViewImages.setAdapter(adapter);

        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                // if we are in activity
/*                ActivityCompat.requestPermissions(getActivity(),
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);*/

                // if we are in fragment
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
        else {

            // if we have permission we can run our permission related task
            ListFiles();
        }

        return view;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // permission-related task you need to do.

                    // this will run just after granting permission
                    // not always

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ListFiles();
                        }
                    });

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    public void ListFiles()
    {
        final Path path = new Path();
        final Search search = new Search();
        final AddFileHelper addFileHelper = new AddFileHelper(adapter);

        search.setOnListFilesListener(new Search.OnListFilesListener() {
            @Override
            public void onNewFileFound(File file) {
                addFileHelper.addNewFile(file);
            }

            @Override
            public void onListCompleted(List<File> result) {
                //addFileHelper.close();
            }
        });


        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                search.listFiles(path.getDCIMFile());
            }
        });

        thread.start();
    }
}

References
https://developer.android.com/training/permissions/requesting.html

Android Recyclerview GridLayoutManager column spacing by ItemDecoration

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
  private int space;

  public SpacesItemDecoration(int space) {
    this.space = space;
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, 
      RecyclerView parent, RecyclerView.State state) {
    outRect.left = space;
    outRect.right = space;
    outRect.bottom = space;

    // Add top margin only for the first item to avoid double space between items
    if (parent.getChildLayoutPosition(view) == 0) {
        outRect.top = space;
    } else {
        outRect.top = 0;
    }
  }
}
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
mRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

References
https://stackoverflow.com/questions/28531996/android-recyclerview-gridlayoutmanager-column-spacing