1 page.title= 2 page.tags=tv, browsefragment, presenter, backgroundmanager 3 4 trainingnavtop=true 5 6 @jd:body 7 8 <div id="tb-wrapper"> 9 <div id="tb"> 10 <h2></h2> 11 <ol> 12 <li><a href="#layout"> </a></li> 13 <li><a href="#lists"> </a></li> 14 <li><a href="#background"></a></li> 15 </ol> 16 17 </div> 18 </div> 19 20 <p> 21 TV 22 </p> 23 24 <p> 25 <a href="{@docRoot}tools/support-library/features.html#v17-leanback">v17 leanback </a> 26 </p> 27 28 29 <h2 id="layout"> </h2> 30 31 <p> 32 leanback {@link android.support.v17.leanback.app.BrowseFragment} {@link android.support.v17.leanback.app.BrowseFragment} 33 </p> 34 35 <pre> 36 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 37 android:layout_width="match_parent" 38 android:layout_height="match_parent" 39 android:orientation="vertical" 40 > 41 42 <fragment 43 <strong>android:name="android.support.v17.leanback.app.BrowseFragment"</strong> 44 android:id="@+id/browse_fragment" 45 android:layout_width="match_parent" 46 android:layout_height="match_parent" 47 /> 48 </LinearLayout> 49 </pre> 50 51 <p> 52 {@link android.support.v17.leanback.app.BrowseFragment} {@link android.support.v17.leanback.app.BrowseFragment} 53 </p> 54 55 <pre> 56 public class BrowseMediaActivity extends Activity { 57 58 public static final String TAG ="BrowseActivity"; 59 60 protected BrowseFragment mBrowseFragment; 61 62 @Override 63 protected void onCreate(Bundle savedInstanceState) { 64 super.onCreate(savedInstanceState); 65 setContentView(R.layout.browse_fragment); 66 67 final FragmentManager fragmentManager = getFragmentManager(); 68 <strong>mBrowseFragment = (BrowseFragment) fragmentManager.findFragmentById( 69 R.id.browse_fragment);</strong> 70 71 // Set display parameters for the BrowseFragment 72 mBrowseFragment.setHeadersState(BrowseFragment.HEADERS_ENABLED); 73 mBrowseFragment.setTitle(getString(R.string.app_name)); 74 mBrowseFragment.setBadgeDrawable(getResources().getDrawable( 75 R.drawable.ic_launcher)); 76 mBrowseFragment.setBrowseParams(params); 77 78 } 79 } 80 </pre> 81 82 83 <h2 id="lists"> </h2> 84 85 <p> 86 {@link android.support.v17.leanback.app.BrowseFragment} 87 </p> 88 89 <p> 90 {@link android.support.v17.leanback.widget.Presenter} 91 </p> 92 93 <pre> 94 public class StringPresenter extends Presenter { 95 private static final String TAG = "StringPresenter"; 96 97 public ViewHolder onCreateViewHolder(ViewGroup parent) { 98 TextView textView = new TextView(parent.getContext()); 99 textView.setFocusable(true); 100 textView.setFocusableInTouchMode(true); 101 textView.setBackground( 102 parent.getContext().getResources().getDrawable(R.drawable.text_bg)); 103 return new ViewHolder(textView); 104 } 105 106 public void onBindViewHolder(ViewHolder viewHolder, Object item) { 107 ((TextView) viewHolder.view).setText(item.toString()); 108 } 109 110 public void onUnbindViewHolder(ViewHolder viewHolder) { 111 // no op 112 } 113 } 114 </pre> 115 116 <p> 117 {@link android.support.v17.leanback.app.BrowseFragment} 1 {@code StringPresenter} 118 </p> 119 120 <pre> 121 private ArrayObjectAdapter mRowsAdapter; 122 private static final int NUM_ROWS = 4; 123 124 @Override 125 protected void onCreate(Bundle savedInstanceState) { 126 ... 127 128 buildRowsAdapter(); 129 } 130 131 private void buildRowsAdapter() { 132 mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter()); 133 134 for (int i = 0; i < NUM_ROWS; ++i) { 135 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter( 136 new StringPresenter()); 137 listRowAdapter.add("Media Item 1"); 138 listRowAdapter.add("Media Item 2"); 139 listRowAdapter.add("Media Item 3"); 140 HeaderItem header = new HeaderItem(i, "Category " + i, null); 141 mRowsAdapter.add(new ListRow(header, listRowAdapter)); 142 } 143 144 mBrowseFragment.setAdapter(mRowsAdapter); 145 } 146 </pre> 147 148 <p> 149 <a href="http://github.com/googlesamples/androidtv-leanback">Android TV</a> 150 </p> 151 152 <h2 id="background"></h2> 153 154 <p> 155 TV 156 </p> 157 158 <p> 159 Leanback TV {@link android.support.v17.leanback.app.BackgroundManager} TV 160 </p> 161 162 <pre> 163 protected void updateBackground(Drawable drawable) { 164 BackgroundManager.getInstance(this).setDrawable(drawable); 165 } 166 </pre> 167 168 <p> 169 {@link android.support.v17.leanback.widget.OnItemViewSelectedListener} 170 </p> 171 172 <pre> 173 protected void clearBackground() { 174 BackgroundManager.getInstance(this).setDrawable(mDefaultBackground); 175 } 176 177 protected OnItemViewSelectedListener getDefaultItemViewSelectedListener() { 178 return new OnItemViewSelectedListener() { 179 @Override 180 public void onItemSelected(Object item, Row row) { 181 if (item instanceof Movie ) { 182 URI uri = ((Movie)item).getBackdropURI(); 183 updateBackground(uri); 184 } else { 185 clearBackground(); 186 } 187 } 188 }; 189 } 190 </pre> 191 192 <p class="note"> 193 <strong>:</strong> 1 194 </p> 195