Last active
August 12, 2017 12:19
-
-
Save the-cybersapien/8353e3449ab781c6f3abe3433fd596cb to your computer and use it in GitHub Desktop.
RecyclerELE Code Samples
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 GuardianStoryAdapter guardianStoryAdapter; | |
/*Create the Recycler View*/ | |
private RecyclerView storyListView; | |
/*Reference to the progress Bar*/ | |
private ProgressBar progressBar; | |
/*Get a reference to the LoaderManager to intereact with the Loaders*/ | |
private LoaderManager loaderManager; | |
/*Reference to the error(hint) TextView*/ | |
private TextView hintView; | |
/*Reference the stories.*/ | |
List < GuardianStory > stories; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
loaderManager = getLoaderManager(); | |
//Initialize Progress bar | |
progressBar = (ProgressBar) findViewById(R.id.progressBar); | |
//Initialize TextView | |
hintView = (TextView) findViewById(R.id.errorHint); | |
//Create RecyclerView | |
storyListView = (RecyclerView) findViewById(R.id.stories_list); | |
storyListView.setVisibility(View.GONE); | |
//Create GuardianStory list; | |
stories = new ArrayList < > (); | |
//get GuardianStoryAdapter | |
guardianStoryAdapter = new GuardianStoryAdapter(this, stories); | |
storyListView.setAdapter(guardianStoryAdapter); | |
storyListView.setLayoutManager(new LinearLayoutManager(this)); | |
startLoad(); | |
} | |
/** | |
* This method starts the loader to get news Stories, | |
* if there is no connection, it shows the hintView stating the error | |
*/ | |
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 { | |
hintView.setText(R.string.no_internet); | |
progressBar.setVisibility(View.GONE); | |
hintView.setVisibility(View.VISIBLE); | |
storyListView.setVisibility(View.GONE); | |
} | |
} | |
/** | |
* On Create Loader handles the setting of the View to loading and then goes on to initiate the network call i the background. | |
*/ | |
@Override | |
public Loader < List < GuardianStory >> onCreateLoader(int id, Bundle args) { | |
progressBar.setVisibility(View.VISIBLE); | |
hintView.setVisibility(View.GONE); | |
. | |
. | |
. | |
return new StoryLoader(this, URI()); | |
} | |
/** | |
* OnLoadFinish Method handles the loading of stories in the list, | |
* If the list is empty, it shows the error view | |
*/ | |
@Override | |
public void onLoadFinished(Loader < List < GuardianStory >> loader, List < GuardianStory > guardianStoryList) { | |
guardianStoryAdapter.clearData(); | |
if (guardianStoryList != null && !guardianStoryList.isEmpty()) { | |
stories.addAll(guardianStoryList); | |
guardianStoryAdapter.notifyDataSetChanged(); | |
storyListView.setVisibility(View.VISIBLE); | |
progressBar.setVisibility(View.GONE); | |
hintView.setVisibility(View.GONE); | |
} else { | |
storyListView.setVisibility(View.GONE); | |
progressBar.setVisibility(View.GONE); | |
hintView.setVisibility(View.VISIBLE); | |
hintView.setText(R.string.error_getting_data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment