Android的碎片基础教程 [英] Android Fragment Basics Tutorial
问题描述
所以我一直停留在第三个教程的关于Android开发者网站片段了解几天。我只是不明白如何应用填充数据,当我运行在平板电脑上(大屏幕布局)的应用程序。我能理解数据是如何被填充一个小屏幕(手机屏幕)上。
如何在更大的屏幕列表填充数据?
下面是整个项目由 Android.com 教程的链接。
MainActivity类别
公共类MainActivity扩展FragmentActivity
实现HeadlinesFragment.OnHeadlineSelectedListener { / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
//这里,系统将决定哪news_article布局将根据屏幕大小使用。将利用布局,如果小或布局的大,如果是大的。
的setContentView(R.layout.news_articles); //检查活动是否使用与布局版本
//将fragment_container的FrameLayout。如果是这样,我们必须添加的第一个片段
//这个检查是确定要使用哪种布局,无论是小屏幕或大屏幕。
// fragment_container使用的FrameLayout的小屏幕。
// fragment_container是的FrameLayout在news_article的ID为小屏幕。
如果(findViewById(R.id.fragment_container)!= NULL){ //但是,如果我们被从previous状态恢复,
//那么我们不需要做任何事情,应该返回要不然
//我们可以重叠的片段结束。
如果(savedInstanceState!= NULL){
返回;
} //创建ExampleFragment的一个实例
HeadlinesFragment firstFragment =新HeadlinesFragment(); //如果这项活动开始由一个Intent特殊说明,
//通过意向的群众演员的片段作为参数
firstFragment.setArguments(getIntent()getExtras()); //片段添加到fragment_container的FrameLayout
getSupportFragmentManager()调用BeginTransaction()
。新增(R.id.fragment_container,firstFragment).commit();
}
} 公共无效onArticleSelected(INT位置){
//用户选择的文章的标题从HeadlinesFragment //捕获从活动布局的文章片段
ArticleFragment articleFrag =(ArticleFragment)
。getSupportFragmentManager()findFragmentById(R.id.article_fragment); 如果(articleFrag!= NULL){
//如果文章FRAG是可用的,我们在两窗格布局... //在ArticleFragment调用一个方法来更新其内容
articleFrag.updateArticleView(位置); }其他{
//如果断枝不可用,我们是在一个窗格布局和必须交换断枝... //创建片段,并给它一个参数所选文章
ArticleFragment newFragment =新ArticleFragment();
捆绑ARGS =新包();
args.putInt(ArticleFragment.ARG_POSITION,位置);
newFragment.setArguments(参数);
FragmentTransaction交易= getSupportFragmentManager()调用BeginTransaction()。 //替换无论是在fragment_container认为这个片段,
//和事务添加到背堆栈,以便用户可以导航回
transaction.replace(R.id.fragment_container,newFragment);
transaction.addToBackStack(NULL); //提交事务
器transaction.commit();
}
}
}
HeadLineFragment
公共类HeadlinesFragment扩展ListFragment {// Container活动必须实现此接口使FRAG可以传递消息
公共接口OnHeadlineSelectedListener {
通过调用HeadlinesFragment / **当选择列表项* /
公共无效onArticleSelected(INT位置);
} OnHeadlineSelectedListener mCallback; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); //我们需要使用不同的列表项布局设备年纪比蜂窝
INT布局= Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB?
android.R.layout.simple_list_item_activated_1:android.R.layout.simple_list_item_1; //创建列表视图的阵列适配器,使用存有头条阵列
setListAdapter(新ArrayAdapter<串GT;(getActivity(),布局,Ipsum.Headlines)); } @覆盖
公共无效调用onStart(){
super.onStart(); //当两个窗格布局,设置列表视图突出显示选中的列表项
//(我们在onStart中,因为在列表视图可在点做到这一点。)
如果(getFragmentManager()。findFragmentById(R.id.article_fragment)!= NULL){
。getListView()setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
} @覆盖
公共无效onAttach(活动活动){
super.onAttach(活动); //这将确保集装箱活动实施
//回调接口。如果不是,它抛出一个异常。
尝试{
mCallback =(OnHeadlineSelectedListener)活性;
}赶上(抛出ClassCastException E){
抛出新ClassCastException异常(activity.toString()
+必须实现OnHeadlineSelectedListener);
}
} @覆盖
公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
//通知所选项目的父活动
mCallback.onArticleSelected(位置); //设置项为选中的两窗格布局中突出时,
getListView()setItemChecked(位置,真)。
}
}
布局小屏幕
news_article.xml
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / fragment_container
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
布局bigscreen
news_article.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向> <片段机器人:名字=com.example.android.fragments.HeadlinesFragment
机器人:ID =@ + ID / headlines_fragment
机器人:layout_weight =1
机器人:layout_width =0dp
机器人:layout_height =match_parent/> <片段机器人:名字=com.example.android.fragments.ArticleFragment
机器人:ID =@ + ID / article_fragment
机器人:layout_weight =2
机器人:layout_width =0dp
机器人:layout_height =match_parent/>< / LinearLayout中>
注意两个布局中的位置。
大屏幕为片剂斌(文件夹) RES /布局大/ main.xml中
,而小屏幕布局是通用 RES /布局/ main.xml中
由于在java询问的findViewById为null我们知道,如果该设备是大屏幕或正常布局。
ArticleFragment articleFrag =(ArticleFragment)getSupportFragmentManager()findFragmentById(R.id.article_fragment)。
如果(articleFrag!= NULL){
/ *不为空,因为我们是在res /布局大* /
}其他{
/ *我们是在单一视图疼痛/ RES /布局/ ... * /
}
当你调用的setContentView(INT);
他们系统处理您加载基于DPI的箱提供给设备的最佳布局提供
So I've been stuck on the third tutorial on the Android Developer Site about Fragments for few days. I just can't understand how the app populates data when I run the app on a tablet (big screen layout). I can understand how the data is being populated on a smaller screen (phone screen).
How does the bigger screen list populate with data?
Here is a link of the whole project from Android.com tutorials.
MainActivity Class
public class MainActivity extends FragmentActivity
implements HeadlinesFragment.OnHeadlineSelectedListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here, the system will decide which news_article layout it will use based on the screen size. Will use layout if small or layout-large if it's big.
setContentView(R.layout.news_articles);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
//This check is to determine which layout to be used, either small screen or big screen.
//fragment_container used FrameLayout for small screens.
//fragment_container is the id of FrameLayout in news_article for small screen.
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create an instance of ExampleFragment
HeadlinesFragment firstFragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Capture the article fragment from the activity layout
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// If the frag is not available, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
}
HeadLineFragment
public class HeadlinesFragment extends ListFragment {
// The container Activity must implement this interface so the frag can deliver messages
public interface OnHeadlineSelectedListener {
/** Called by HeadlinesFragment when a list item is selected */
public void onArticleSelected(int position);
}
OnHeadlineSelectedListener mCallback;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We need to use a different list item layout for devices older than Honeycomb
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
// Create an array adapter for the list view, using the Ipsum headlines array
setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
}
@Override
public void onStart() {
super.onStart();
// When in two-pane layout, set the listview to highlight the selected list item
// (We do this during onStart because at the point the listview is available.)
if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
mCallback.onArticleSelected(position);
// Set the item as checked to be highlighted when in two-pane layout
getListView().setItemChecked(position, true);
}
}
Layout for small screen news_article.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Layout for bigscreen news_article.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Notice the placement of the two layouts.
Large screen is in a tablet bin (folder) res/layout-large/main.xml
while small screen layout is in generic res/layout/main.xml
Since the java asks if the findViewById is null we know if the device is a large screen or normal layout.
ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
/* not null because we are in res/layout-large */
} else {
/* we are in single pain view /res/layout/... */
}
When you call setContentView(int);
they system handles loading the best layout you provided for the device based on the DPI bins provided.
这篇关于Android的碎片基础教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!