1 page.title= 2 page.tags=tv, detailsfragment 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="#details-presenter"></a></li> 13 <li><a href="#details-fragment"></a> 14 <li><a href="#activity"></a></li> 15 <li><a href="#item-listener"></a></li> 16 </ol> 17 </div> 18 </div> 19 20 <p> 21 <a href="{@docRoot}tools/support-library/features.html#v17-leanback">v17 leanback </a> 22 </p> 23 24 <p> 25 {@link android.support.v17.leanback.app.DetailsFragment} 26 </p> 27 28 <p class="note"> 29 <strong>:</strong> {@link android.support.v17.leanback.app.DetailsFragment} {@link android.support.v17.leanback.app.BrowseFragment} <em></em> {@link android.support.v17.leanback.app.DetailsFragment} 2 <a href="{@docRoot}training/basics/fragments/fragment-ui.html#Replace"> UI </a> 30 </p> 31 32 33 <h2 id="details-presenter"></h2> 34 35 <p> 36 Leanback {@link android.support.v17.leanback.widget.AbstractDetailsDescriptionPresenter} {@link android.support.v17.leanback.widget.AbstractDetailsDescriptionPresenter#onBindDescription onBindDescription()} 37 </p> 38 39 <pre> 40 public class DetailsDescriptionPresenter 41 extends AbstractDetailsDescriptionPresenter { 42 43 @Override 44 protected void onBindDescription(ViewHolder viewHolder, Object itemData) { 45 MyMediaItemDetails details = (MyMediaItemDetails) itemData; 46 // In a production app, the itemData object contains the information 47 // needed to display details for the media item: 48 // viewHolder.getTitle().setText(details.getShortTitle()); 49 50 // Here we provide static data for testing purposes: 51 viewHolder.getTitle().setText(itemData.toString()); 52 viewHolder.getSubtitle().setText("2014 Drama TV-14"); 53 viewHolder.getBody().setText("Lorem ipsum dolor sit amet, consectetur " 54 + "adipisicing elit, sed do eiusmod tempor incididunt ut labore " 55 + " et dolore magna aliqua. Ut enim ad minim veniam, quis " 56 + "nostrud exercitation ullamco laboris nisi ut aliquip ex ea " 57 + "commodo consequat."); 58 } 59 } 60 </pre> 61 62 63 <h2 id="details-fragment"></h2> 64 65 <p> 66 {@link android.support.v17.leanback.app.DetailsFragment} 67 </p> 68 69 <p> 70 71 </p> 72 73 <pre> 74 public class MediaItemDetailsFragment extends DetailsFragment { 75 private static final String TAG = "MediaItemDetailsFragment"; 76 private ArrayObjectAdapter mRowsAdapter; 77 78 @Override 79 public void onCreate(Bundle savedInstanceState) { 80 Log.i(TAG, "onCreate"); 81 super.onCreate(savedInstanceState); 82 83 buildDetails(); 84 } 85 86 private void buildDetails() { 87 ClassPresenterSelector selector = new ClassPresenterSelector(); 88 // Attach your media item details presenter to the row presenter: 89 DetailsOverviewRowPresenter rowPresenter = 90 new DetailsOverviewRowPresenter(new DetailsDescriptionPresenter()); 91 92 selector.addClassPresenter(DetailsOverviewRow.class, rowPresenter); 93 selector.addClassPresenter(ListRow.class, 94 new ListRowPresenter()); 95 mRowsAdapter = new ArrayObjectAdapter(selector); 96 97 Resources res = getActivity().getResources(); 98 DetailsOverviewRow detailsOverview = new DetailsOverviewRow( 99 "Media Item Details"); 100 101 // Add images and action buttons to the details view 102 detailsOverview.setImageDrawable(res.getDrawable(R.drawable.jelly_beans)); 103 detailsOverview.addAction(new Action(1, "Buy $9.99")); 104 detailsOverview.addAction(new Action(2, "Rent $2.99")); 105 mRowsAdapter.add(detailsOverview); 106 107 // Add a Related items row 108 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter( 109 new StringPresenter()); 110 listRowAdapter.add("Media Item 1"); 111 listRowAdapter.add("Media Item 2"); 112 listRowAdapter.add("Media Item 3"); 113 HeaderItem header = new HeaderItem(0, "Related Items", null); 114 mRowsAdapter.add(new ListRow(header, listRowAdapter)); 115 116 setAdapter(mRowsAdapter); 117 } 118 } 119 </pre> 120 121 122 <h3 id="activity"></h3> 123 124 <p> 125 {@link android.support.v17.leanback.app.DetailsFragment} {@link android.content.Intent} 126 </p> 127 128 <p> 129 {@link android.support.v17.leanback.app.DetailsFragment} 130 </p> 131 132 <pre> 133 <!-- file: res/layout/details.xml --> 134 135 <fragment xmlns:android="http://schemas.android.com/apk/res/android" 136 <strong>android:name="com.example.android.mediabrowser.MediaItemDetailsFragment"</strong> 137 android:id="@+id/details_fragment" 138 android:layout_width="match_parent" 139 android:layout_height="match_parent" 140 /> 141 </pre> 142 143 <p> 144 145 </p> 146 147 <pre> 148 public class DetailsActivity extends Activity 149 { 150 @Override 151 public void onCreate(Bundle savedInstanceState) { 152 super.onCreate(savedInstanceState); 153 <strong>setContentView(R.layout.details);</strong> 154 } 155 } 156 </pre> 157 158 <p> 159 Leanback 160 </p> 161 162 <pre> 163 <application> 164 ... 165 166 <activity android:name=".DetailsActivity" 167 android:exported="true" 168 <strong>android:theme="@style/Theme.Leanback"/></strong> 169 170 </application> 171 </pre> 172 173 174 <h3 id="item-listener"></h3> 175 176 <p> 177 {@link android.support.v17.leanback.app.DetailsFragment} {@link android.support.v17.leanback.widget.OnItemViewClickedListener} {@link android.support.v17.leanback.app.BrowseFragment} 178 </p> 179 180 <p> 181 182 </p> 183 184 <pre> 185 public class BrowseMediaActivity extends Activity { 186 ... 187 188 @Override 189 protected void onCreate(Bundle savedInstanceState) { 190 ... 191 192 // create the media item rows 193 buildRowsAdapter(); 194 195 // add a listener for selected items 196 mBrowseFragment.OnItemViewClickedListener( 197 new OnItemViewClickedListener() { 198 @Override 199 public void onItemClicked(Object item, Row row) { 200 System.out.println("Media Item clicked: " + item.toString()); 201 Intent intent = new Intent(BrowseMediaActivity.this, 202 DetailsActivity.class); 203 // pass the item information 204 intent.getExtras().putLong("id", item.getId()); 205 startActivity(intent); 206 } 207 }); 208 } 209 } 210 </pre> 211