1 page.title= UI 2 parent.title= 3 parent.link=index.html 4 5 trainingnavtop=true 6 previous.title= 7 previous.link=screendensities.html 8 9 @jd:body 10 11 12 <!-- This is the training bar --> 13 <div id="tb-wrapper"> 14 <div id="tb"> 15 16 <h2></h2> 17 18 <ol> 19 <li><a href="#TaskDetermineCurLayout"></a></li> 20 <li><a href="#TaskReactToLayout"></a></li> 21 <li><a href="#TaskReuseFrag"></a></li> 22 <li><a href="#TaskHandleConfigChanges"></a></li> 23 </ol> 24 25 <h2></h2> 26 27 <ul> 28 <li><a href="{@docRoot}guide/practices/tablets-and-handsets.html"></a></li> 29 </ul> 30 31 <h2></h2> 32 33 <div class="download-box"> 34 <a href="http://developer.android.com/shareables/training/NewsReader.zip" class="button"> </a> 35 <p class="filename">NewsReader.zip</p> 36 </div> 37 38 39 </div> 40 </div> 41 42 <p>UI </p> 43 44 45 <h2 id="TaskDetermineCurLayout"></h2> 46 47 <p>:</p> 48 49 <pre class="prettyprint"> 50 public class NewsReaderActivity extends FragmentActivity { 51 boolean mIsDualPane; 52 53 @Override 54 public void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 setContentView(R.layout.main_layout); 57 58 View articleView = findViewById(R.id.article); 59 mIsDualPane = articleView != null && 60 articleView.getVisibility() == View.VISIBLE; 61 } 62 } 63 </pre> 64 65 <p>article</p> 66 67 <p>News Reader Android 3.0 API 11 <PH>{@link android.app.ActionBar}</PH> :</p> 68 69 <pre class="prettyprint"> 70 Button catButton = (Button) findViewById(R.id.categorybutton); 71 OnClickListener listener = /* create your listener here */; 72 if (catButton != null) { 73 catButton.setOnClickListener(listener); 74 } 75 </pre> 76 77 78 <h2 id="TaskReactToLayout"></h2> 79 80 <p>News Reader UI UI :</p> 81 82 <pre> 83 @Override 84 public void onHeadlineSelected(int index) { 85 mArtIndex = index; 86 if (mIsDualPane) { 87 /* display article on the right pane */ 88 mArticleFragment.displayArticle(mCurrentCat.getArticle(index)); 89 } else { 90 /* start a separate activity */ 91 Intent intent = new Intent(this, ArticleActivity.class); 92 intent.putExtra("catIndex", mCatIndex); 93 intent.putExtra("artIndex", index); 94 startActivity(intent); 95 } 96 } 97 </pre> 98 99 <p> :</p> 100 101 <pre> 102 final String CATEGORIES[] = { " ", "", "", "Technology" }; 103 104 public void onCreate(Bundle savedInstanceState) { 105 .... 106 if (mIsDualPane) { 107 /* use tabs for navigation */ 108 actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_TABS); 109 int i; 110 for (i = 0; i < CATEGORIES.length; i++) { 111 actionBar.addTab(actionBar.newTab().setText( 112 CATEGORIES[i]).setTabListener(handler)); 113 } 114 actionBar.setSelectedNavigationItem(selTab); 115 } 116 else { 117 /* use list navigation (spinner) */ 118 actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_LIST); 119 SpinnerAdapter adap = new ArrayAdapter<String>(this, 120 R.layout.headline_item, CATEGORIES); 121 actionBar.setListNavigationCallbacks(adap, handler); 122 } 123 } 124 </pre> 125 126 127 <h2 id="TaskReuseFrag"></h2> 128 129 <p>News Reader </p> 130 131 <p> <PH>{@link android.app.Fragment}</PH> <code>ArticleFragment</code> :</p> 132 133 {@sample development/samples/training/multiscreen/newsreader/res/layout/twopanes.xml all} 134 135 <p> <code>ArticleActivity</code>:</p> 136 137 <pre> 138 ArticleFragment frag = new ArticleFragment(); 139 getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit(); 140 </pre> 141 142 <p> XML XML </p> 143 144 <p> 1 :</p> 145 146 <p>News Reader <code>HeadlinesFragment</code> :</p> 147 148 <pre> 149 public class HeadlinesFragment extends ListFragment { 150 ... 151 OnHeadlineSelectedListener mHeadlineSelectedListener = null; 152 153 /* Must be implemented by host activity */ 154 public interface OnHeadlineSelectedListener { 155 public void onHeadlineSelected(int index); 156 } 157 ... 158 159 public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener listener) { 160 mHeadlineSelectedListener = listener; 161 } 162 } 163 </pre> 164 165 <p> :</p> 166 167 <pre> 168 public class HeadlinesFragment extends ListFragment { 169 ... 170 @Override 171 public void onItemClick(AdapterView<?> parent, 172 View view, int position, long id) { 173 if (null != mHeadlineSelectedListener) { 174 mHeadlineSelectedListener.onHeadlineSelected(position); 175 } 176 } 177 ... 178 } 179 </pre> 180 181 <p><a 182 href="{@docRoot}guide/practices/tablets-and-handsets.html"></a></p> 183 184 185 <h2 id="TaskHandleConfigChanges"></h2> 186 187 <p></p> 188 189 <p>Android 3.0 7 News Reader 2 </p> 190 191 <p> 2 :</p> 192 193 <pre> 194 public class ArticleActivity extends FragmentActivity { 195 int mCatIndex, mArtIndex; 196 197 @Override 198 protected void onCreate(Bundle savedInstanceState) { 199 super.onCreate(savedInstanceState); 200 mCatIndex = getIntent().getExtras().getInt("catIndex", 0); 201 mArtIndex = getIntent().getExtras().getInt("artIndex", 0); 202 203 // If should be in two-pane mode, finish to return to main activity 204 if (getResources().getBoolean(R.bool.has_two_panes)) { 205 finish(); 206 return; 207 } 208 ... 209 } 210 </pre> 211 212 213