Created
August 12, 2017 12:20
-
-
Save the-cybersapien/55168d66271b65bfd2e6950f613a30f9 to your computer and use it in GitHub Desktop.
RecyclerELE Code Sample Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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