Copyright notice: This article is the original translation of the blogger, please indicate the source.
Recommendation: welcome attention I create Android TV Jane books project, regularly to share some AndroidTv related content: www.jianshu.com/c/37efc6e97…
ErrorFragment is another Leanback library supported layout. ###ErrorActivity New -> Java Class -> Name: ErrorActivity
- We don’t use a layout file to display the ErrorActivity. It basically just attaches the ErrorFragment.
package com.corochann.androidtvapptutorial;
import android.app.Activity;
import android.os.Bundle;
public class ErrorActivity extends Activity {
private static final String TAG = ErrorActivity.class.getSimpleName();
private ErrorFragment mErrorFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testError();
}
private void testError() { mErrorFragment = new ErrorFragment(); getFragmentManager().beginTransaction().add(R.id.main_browse_fragment, mErrorFragment).commit(); }}Copy the code
###ErrorFragment New -> Java Class -> Name: ErrorFragment
- Note that we don’t use the layout to show the ErrorFragment here
ErrorFragment is android. Support. V17. Leanback. App. ErrorFragment. The subclass.
package com.corochann.androidtvapptutorial;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
/**
* This class demonstrates how to extend ErrorFragment
*/
public class ErrorFragment extends android.support.v17.leanback.app.ErrorFragment {
private static final String TAG = ErrorFragment.class.getSimpleName();
private static final boolean TRANSLUCENT = true;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setTitle(getResources().getString(R.string.app_name));
setErrorContent();
}
void setErrorContent() {
setImageDrawable(getActivity().getDrawable(R.drawable.lb_ic_sad_cloud));
setMessage(getResources().getString(R.string.error_fragment_message));
setDefaultBackground(TRANSLUCENT);
setButtonText(getResources().getString(R.string.dismiss_error));
setButtonClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { getFragmentManager().beginTransaction().remove(ErrorFragment.this).commit(); }}); }}Copy the code
### Modify androidmanifest.xml to declare ErrorActivity
<application>
...
<activity android:name=".ErrorActivity" />
</application>
Copy the code
Add code to start ErrorActivity
I will change the name of the GridItemPresenter “Item 1” – > “ErrorFragment” Item and I will start the ErrorActivity by clicking on the “ErrorFragment” Item.
Modify MainFragment to start ErrorActivity with intent. MainFragment.java
private void loadRows() {... //gridRowAdapter.add("ITEM 1");
gridRowAdapter.add("ErrorFragment"); . }Copy the code
@Override
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
// each time the item is clicked, code inside here will be executed.
if (item instanceof Movie) {
Movie movie = (Movie) item;
Log.d(TAG, "Item: " + item.toString());
Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtra(DetailsActivity.MOVIE, movie);
getActivity().startActivity(intent);
} else if (item instanceof String){
if (item == "ErrorFragment") { Intent intent = new Intent(getActivity(), ErrorActivity.class); startActivity(intent); }}}Copy the code
res/values/strings.xml
<resources>
<string name="app_name">AndroidTVappTutorial</string>
<string name="title_activity_main">MainActivity</string>
<string name="hello_world">Hello world! </string> <string name="action_settings">Settings</string>
<string name="title_activity_details">DetailsActivity</string>
<string name="error">Error</string> <! -- Error messages --> <string name="video_error_media_load_timeout">Media loading timed out</string>
<string name="video_error_server_inaccessible">Media server was not reachable</string>
<string name="video_error_unknown_error">Failed to load video</string>
<string name="error_fragment_message">An error occurred</string>
<string name="dismiss_error">Dismiss</string>
<string name="oops">Oops</string>
</resources>
Copy the code
By clicking the “ErrorFragment” item, ErrorFragment will display an error message with an image.
- AOSP project, SpinnerFragment also implements display loading animation. But I’ve covered the minimal implementation/use of ErrorFragment here. Source code is ongithub.NextPlaybackOverlayActivity and PlaybackOverlayFragment–Android TV App Development Tutorial 7, will introduce you to the video playback control section. Pay attention to wechat public number, regularly recommend mobile development related articles for you.