AndroidAndroid TVAndroidTV开发

[译]ErrorFragmet--Android TV 应用开发

2017-05-12  本文已影响765人  wenju_song

版权声明:本文为博主原创翻译文章,转载请注明出处。

推荐:
欢迎关注我创建的Android TV 简书专题,会定期给大家分享一些AndroidTv相关的内容:
http://www.jianshu.com/c/37efc6e9799b


ErrorFragment

ErrorActivity 和 ErrorFragment

ErrorFragment是另一个Leanback库支持的布局。

ErrorActivity

New -> Java Class -> Name: ErrorActivity

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();
    }
}

ErrorFragment

New -> Java Class -> Name: ErrorFragment

ErrorFragment 是android.support.v17.leanback.app.ErrorFragment.的子类。

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();
            }
        });
    }
}

修改AndroidManifest.xml去声明ErrorActivity

  <application>

    ...

        <activity android:name=".ErrorActivity" />
    </application>

添加代码去启动ErrorActivity

我将更改GridItemPresenter“Item 1” - >“ErrorFragment”项目的名称,我将通过单击此“ErrorFragment”项来启动此ErrorActivity。

修改MainFragment,通过意图启动ErrorActivity。
MainFragment.java

 private void loadRows() {
        ...
        //gridRowAdapter.add("ITEM 1");
        gridRowAdapter.add("ErrorFragment");
        ...
    }
        @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);
                }
            }
        }

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>

编译后运行

通过点击“ErrorFragment”项,ErrorFragment将显示带有图像的错误信息。


ErrorFragment
上一篇下一篇

猜你喜欢

热点阅读