成都工业学院计算机工程学院《移动云计算编程技术》·实验报告实验名称实验四制作一个简易版的新闻应用实验时间2016.4.14学生姓名王亮班级1305022学号32指导教师杨雅志批阅教师成绩一、实验目的回顾前面所学的Fragment知识,通过这个这个实验来做到灵活运用Fragment知识。二、实验要求熟悉Fragment的生命周期。了解当前Activtity怎么根据当前屏幕的大小来加载不同的布局文件。三、实验原理Android中根据屏幕大小的不同来加载不同的布局文件四、实验过程1.创建工程a)Projectname:fragmentpracticeb)Packagename:com.example.fragmentpracticec)Activityname:MainActivityd)Applicationname::fragmentpracticee)MinSDKVersion:32编辑布局文件在res目录下新建一个/fragmentpractice/res/layout-sw600dp目录在该目录下新建一个activity_main.xml布局文件LinearLayoutxmlns:android=:layout_width=match_parentandroid:layout_height=match_parentfragmentandroid:id=@+id/news_title_fragmentandroid:name=com.example.fragmentbestpractice.NewsTitleFragmentandroid:layout_width=0dpandroid:layout_height=match_parentandroid:layout_weight=1/FrameLayoutandroid:id=@+id/news_content_layoutandroid:layout_width=0dpandroid:layout_height=match_parentandroid:layout_weight=3fragmentandroid:id=@+id/news_content_fragmentandroid:name=com.example.fragmentbestpractice.NewsContentFragmentandroid:layout_width=match_parentandroid:layout_height=match_parent//FrameLayout/LinearLayout在layout目录下新建一个同名的activity_main.xml布局文件LinearLayoutxmlns:android=:layout_width=match_parentandroid:layout_height=match_parentfragmentandroid:id=@+id/news_title_fragmentandroid:name=com.example.fragmentbestpractice.NewsTitleFragmentandroid:layout_width=match_parentandroid:layout_height=match_parent//LinearLayout新建一个News类packagecom.example.fragmentpractice;publicclassNews{privateStringtitle;privateStringcontent;publicStringgetTitle(){returntitle;}publicvoidsetTitle(Stringtitle){this.title=title;}publicStringgetContent(){returncontent;}publicvoidsetContent(Stringcontent){this.content=content;}}新建一个NewsAdapter类packagecom.example.fragmentpractice;importjava.util.List;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.ArrayAdapter;importandroid.widget.TextView;publicclassNewsAdapterextendsArrayAdapterNews{privateintresourceId;publicNewsAdapter(Contextcontext,inttextViewResourceId,ListNewsobjects){super(context,textViewResourceId,objects);resourceId=textViewResourceId;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){Newsnews=getItem(position);Viewview;if(convertView==null){view=LayoutInflater.from(getContext()).inflate(resourceId,null);}else{view=convertView;}TextViewnewsTitleText=(TextView)view.findViewById(R.id.news_title);newsTitleText.setText(news.getTitle());returnview;}}新建一个NewsContentActivity类packagecom.example.fragmentpractice;importandroid.app.Activity;importandroid.content.Context;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.Window;publicclassNewsContentActivityextendsActivity{publicstaticvoidactionStart(Contextcontext,StringnewsTitle,StringnewsContent){Intentintent=newIntent(context,NewsContentActivity.class);intent.putExtra(news_title,newsTitle);intent.putExtra(news_content,newsContent);context.startActivity(intent);}@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.news_content);StringnewsTitle=getIntent().getStringExtra(news_title);StringnewsContent=getIntent().getStringExtra(news_content);NewsContentFragmentnewsContentFragment=(NewsContentFragment)getFragmentManager().findFragmentById(R.id.news_content_fragment);newsContentFragment.refresh(newsTitle,newsContent);}}新建NewsContentFragment类packagecom.example.fragmentpractice;importandroid.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.TextView;publicclassNewsContentFragmentextendsFragment{privateViewview;@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){view=inflater.inflate(R.layout.news_content_frag,container,false);returnview;}publicvoidrefresh(StringnewsTitle,StringnewsContent){ViewvisibilityLayout=view.findViewById(R.id.visibility_layout);visibilityLayout.setVisibility(View.VISIBLE);TextViewnewsTitleText=(TextView)view.findViewById(R.id.news_title);TextViewnewsContentText=(TextView)view.findViewById(R.id.news_content);newsTitleText.setText(newsTitle);newsContentText.setText(newsContent);}}新建NewsTitleFragment类packagecom.example.fragmentpractice;importjava.util.ArrayList;importjava.util.List;importandroid.app.Activity;importandroid.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.AdapterView;importandroid.widget.AdapterView.OnItemClickListener;importandroid.widget.ListView;publicclassNewsTitleFragmentextendsFragmentimplementsOnItemClickListener{privateListViewnewsTitleListView;privateListNewsnewsList;privateNewsAdapteradapter;privatebooleanisTwoPane;@OverridepublicvoidonAttach(Activ