《移动云计算编程》实验四.docx_第1页
《移动云计算编程》实验四.docx_第2页
《移动云计算编程》实验四.docx_第3页
《移动云计算编程》实验四.docx_第4页
《移动云计算编程》实验四.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

成都工业学院计算机工程学院移动云计算编程技术实验报告实验名称实验四 制作一个简易版的新闻应用实验时间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布局文件在layout目录下新建一个同名的activity_main.xml布局文件新建一个News类package com.example.fragmentpractice;public class News private String title;private String content;public String getTitle() return title;public void setTitle(String title) this.title = title;public String getContent() return content;public void setContent(String content) this.content = content;新建一个NewsAdapter类package com.example.fragmentpractice;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.TextView;public class NewsAdapter extends ArrayAdapter private int resourceId;public NewsAdapter(Context context, int textViewResourceId, List objects) super(context, textViewResourceId, objects);resourceId = textViewResourceId;Overridepublic View getView(int position, View convertView, ViewGroup parent) News news = getItem(position);View view;if (convertView = null) view = LayoutInflater.from(getContext().inflate(resourceId, null); else view = convertView;TextView newsTitleText = (TextView) view.findViewById(R.id.news_title);newsTitleText.setText(news.getTitle();return view;新建一个NewsContentActivity类package com.example.fragmentpractice;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.Window;public class NewsContentActivity extends Activity public static void actionStart(Context context, String newsTitle,String newsContent) Intent intent = new Intent(context, NewsContentActivity.class);intent.putExtra(news_title, newsTitle);intent.putExtra(news_content, newsContent);context.startActivity(intent);Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.news_content);String newsTitle = getIntent().getStringExtra(news_title);String newsContent = getIntent().getStringExtra(news_content);NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);newsContentFragment.refresh(newsTitle, newsContent);新建NewsContentFragment类package com.example.fragmentpractice;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class NewsContentFragment extends Fragment private View view;Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) view = inflater.inflate(R.layout.news_content_frag, container, false);return view;public void refresh(String newsTitle, String newsContent) View visibilityLayout = view.findViewById(R.id.visibility_layout);visibilityLayout.setVisibility(View.VISIBLE);TextView newsTitleText = (TextView) view.findViewById(R.id.news_title);TextView newsContentText = (TextView) view.findViewById(R.id.news_content);newsTitleText.setText(newsTitle);newsContentText.setText(newsContent);新建NewsTitleFragment类package com.example.fragmentpractice;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;public class NewsTitleFragment extends Fragment implements OnItemClickListener private ListView newsTitleListView;private List newsList;private NewsAdapter adapter;private boolean isTwoPane;Overridepublic void onAttach(Activity activity) super.onAttach(activity);newsList = getNews();adapter = new NewsAdapter(activity, R.layout.news_item, newsList);Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) View view = inflater.inflate(R.layout.news_title_frag, container, false);newsTitleListView = (ListView) view.findViewById(R.id.news_title_list_view);newsTitleListView.setAdapter(adapter);newsTitleListView.setOnItemClickListener(this);return view;Overridepublic void onActivityCreated(Bundle savedInstanceState) super.onActivityCreated(savedInstanceState);if (getActivity().findViewById(R.id.news_content_layout) != null) isTwoPane = true; else isTwoPane = false;Overridepublic void onItemClick(AdapterView parent, View view, int position,long id) News news = newsList.get(position);if (isTwoPane) NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);newsContentFragment.refresh(news.getTitle(), news.getContent(); else NewsContentActivity.actionStart(getActivity(), news.getTitle(),news.getContent();private List getNews() List newsList = new ArrayList();News news1 = new News();news1.setTitle(Succeed in College as a Learning Disabled Student);news1.setContent

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论