Skip to content

Instantly share code, notes, and snippets.

@the-cybersapien
Created August 12, 2017 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-cybersapien/55168d66271b65bfd2e6950f613a30f9 to your computer and use it in GitHub Desktop.
Save the-cybersapien/55168d66271b65bfd2e6950f613a30f9 to your computer and use it in GitHub Desktop.
RecyclerELE Code Sample Gist
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks < List < GuardianStory >> {
/*Adapter for the stories*/
private RecyclerELEAdapter RAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loaderManager = getLoaderManager();
//Create RecyclerView
RecyclerView storyListView = (RecyclerView) findViewById(R.id.stories_list);
storyListView.setLayoutManager(new LinearLayoutManager(this));
// Initialize the loading View
View loadingView = getLayoutInflater().inflate(R.layout.loading_view, storyListView, false);
// Initialize the error View
View errorView = getLayoutInflater().inflate(R.layout.error_view, storyListView, false);
// Initialize the empty View
// In this case, we’re going to just modify the text in the error_view.xml
View emptyView = getLayoutInflater().inflate(R.layout.error_view, storyListView, false);
((TextView) emptyView.findViewById(R.id.errorHint)).setText(“No Stories available!”);
stories = new ArrayList < > ();
// Initialize the guardianStoryAdapter
GuardianStoryAdapter guardianStoryAdapter = new GuardianStoryAdapter(this, stories);
// Initialize the RecyclerELE
RAdapter = new RecyclerELEAdapter(guardianStoryAdapter, emptyView, loadingView, errorView);
storyListView.setAdapter(RAdapter);
startLoad();
}
private void startLoad() {
//Create the connectivity Manager and NetworkInfo objects to check for Network connectivity
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo  != null && networkInfo.isConnected()) {
//Initialize the loader. Pass the itn ID constant defined above and pass in null
loaderManager.initLoader(STORY_LOADER_ID, null, this);
} else {
RAdapter.setCurrentView(RecyclerELEAdapter.VIEW_ERROR);
}
}
@Override
public Loader < List < GuardianStory >> onCreateLoader(int id, Bundle args) {
RAdapter.setCurrentView(RecyclerELEAdapter.VIEW_LOADING);
}
@Override
public void onLoadFinished(Loader < List < GuardianStory >> loader, List < GuardianStory > guardianStoryList) {
if (guardianStoryList  != null &&  !guardianStoryList.isEmpty()) {
stories.addAll(guardianStoryList);
RAdapter.setCurrentView(RecyclerELEAdapter.VIEW_NORMAL);
RAdapter.notifyDataSetChanged();
} else {
RAdapter.notifyDataSetChanged();
RAdapter.setCurrentView(RecyclerELEAdapter.VIEW_ERROR);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment