编写利用Fragment创建新闻列表
1、创建新闻实体类News,代码如下:
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;
}
}
2、新建news_item.xml,用于新闻列表子项的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:textSize="18sp"
android:paddingLeft="10dp"
android:paddingTop="15dp"
android:paddingRight="10dp"
android:paddingBottom="15dp"
/>
</LinearLayout>
3、新建适配器NewsAdapter,代码如下:
public class NewsAdapter extends ArrayAdapter<News> {
private int resourceId;
public NewsAdapter(Context context, int textViewResourceId, List<News> objects) {
super(context, textViewResourceId, objects);
this.resourceId = textViewResourceId;
}
@Override
public 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;
}
}
4、新建news_content_frag.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/visibility_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="invisible"
>
<TextView
android:id="@+id/news_titles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:gravity="center"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ff666666"
/>
<TextView
android:id="@+id/news_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:padding="15dp"
/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#ff666666"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
5、新建NewsContentFragment,代码如下:
public class NewsContentFragment extends Fragment{
private View rootView ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.news_content_frag, container, false);
return rootView;
}
public void reflush(String newsTitle, String newsContent){
View visibilityLayout = rootView.findViewById(R.id.visibility_layout);
visibilityLayout.setVisibility(View.VISIBLE);
TextView title = (TextView) rootView.findViewById(R.id.news_titles);
TextView content = (TextView) rootView.findViewById(R.id.news_content);
title.setText(newsTitle);//刷新新闻的标题
content.setText(newsContent);//刷新新闻的内容
}
}
6、新建new_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.studytest.part4.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
7、新建NewsContentActivity,代码如下:
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);
}
@Override
protected 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.reflush(newsTitle, newsContent);
}
}
8、新建news_title_frag.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/title_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
9、新建NewsTitleFragment
public class NewsTitleFragment extends Fragment {
private ListView newsTitleListView;
private List<News> news_list;
private NewsAdapter newsAdapter;
private Boolean isTwoPane;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
news_list = getNews();
newsAdapter = new NewsAdapter(activity, R.layout.news_item, news_list);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.new_title_frag, container, false);
newsTitleListView = (ListView) rootView.findViewById(R.id.title_list_view);
newsTitleListView.setAdapter(newsAdapter);
newsTitleListView.setOnItemClickListener(oicl);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(getActivity().findViewById(R.id.news_content_layout) != null){
isTwoPane = true;
}else {
isTwoPane = false;
}
}
private OnItemClickListener oicl = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
News news = news_list.get(position);
if(isTwoPane){
NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
newsContentFragment.reflush(news.getTitle(), news.getContent());
}else {
//单页模式,直接启动NewsContentActivity
NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent());
}
}
};
private List<News> getNews() {
List<News> newsList = new ArrayList<News>();
News news1 = new News();
news1.setTitle("hello world");
news1.setContent(
"iadh do fj kjew skjf jflf jlfa flepr oiirrl ;l;ijr' llrklwe lrew klwpur 9ieokk ;ajht lfj jkrhl jlrejlpsfyey yurehsjf fejw jjrw jlsfj ");
newsList.add(news1);
News news2 = new News();
news2.setTitle("hello world2");
news2.setContent(
"22222 22 iadh do fj kjew skjf jflf jlfa flepr oiirrl ;l;ijr' llrklwe lrew klwpur 9ieokk ;ajht lfj jkrhl jlrejlpsfyey yurehsjf fejw jjrw jlsfj ");
newsList.add(news2);
return newsList;
}
}
10、新建activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/news_title_fragment"
android:name="com.example.studytest.part4.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
上面的代码表示在单页模式下,只会加载一个新闻标题的碎片,然后新建layout-sw600dp文件夹,在这个文件夹下再新建activity_main.xml
11、新建layout-sw600dp,并新建activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/news_title_fragment"
android:name="com.example.studytest.part4.NewsTitleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/news_content_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" >
<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.studytest.part4.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout >
可以看出,在双页模式下可以同时引入两个碎片,并将新闻内容的碎片放在了一个FrameLayout布局下, 而这个布局的id正是news_content_layout,因此能够找到这个id就是双页模式,否则就是单页模式。
- 利用Fragment创建动态UI 之 Fragment之间的通信
为了可以复用一个fragment,所以在定义fragment的时候,要把它定义为一个完全独立和模块化,它有它自己的layout和行为.当你定义好了这些可复用的fragment,可以把他们和activi ...
- vue ui九宫格、底部导航、新闻列表、跨域访问
一. 九宫格 九宫格:在mint-ui组件库基于vue框架 mui不是基于vue框架 只是css/js文件 (1)官方网站下载安装包 (2)copy css js fonts[字体图标] src/l ...
- Angular2快速入门-2.创建一个新闻列表
背景: 我们想通过一个例子,展示下Angular2 怎么绑定页面,怎么创建Component, 例子:我们创建一个新闻列表,当点击新闻列表中某一条新闻的时候,展示出该条新闻的详细信息, 在详细信息中可 ...
- 【JavaWeb】MVC案例之新闻列表
MVC案例之新闻列表 作者:白宁超 2016年6月6日15:26:30 摘要:本文主要针对javaweb基本开发之MVC案例的简单操作,里面涉及mysql数据库及表的创建,以及jsp页面和servle ...
- Oracle Sales Cloud:管理沙盒(定制化)小细节1——利用公式创建字段并显示在前端页面
Oracle Sales Cloud(Oracle 销售云)是一套基于Oracle云端的CRM管理系统.由于 Oracle 销售云是基于 Oracle 云环境的,它与传统的管理系统相比,显著特点之一便 ...
- 利用Oracle创建表空间和用户
本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6217152.html 第一步,创建表空间 以SYS/sys账户和SYSDBA身份登录 ...
- 利用navicat创建存储过程、触发器和使用游标的简单实例
利用navicat创建存储过程.触发器和使用游标的简单实例 标签: navicat存储过程触发器mysql游标 2013-08-03 21:34 15516人阅读 评论(1) 收藏 举报 分类: 数 ...
- Android 用Fragment创建一个选项卡
本文结合之前的动态创建fragment来进行一个实践,来实现用Fragment创建一个选项卡 本文地址:http://www.cnblogs.com/wuyudong/p/5898075.html,转 ...
- sharepoint2010 创建自定义列表
转:http://boke.25k5.com/kan77298.html 如何创建自定义列表 首先了解创建自定义列表中涉及到的几个名词:栏.内容类型. ①栏:栏即列.字段(Field),MSDN中给出 ...
随机推荐
- [BZOJ 1257] [CQOI2007] 余数之和sum 【数学】
题目链接:BZOJ - 1257 题目分析 首先, a % b = a - (a/b) * b,那么答案就是 sigma(k % i) = n * k - sigma(k / i) * i ( ...
- Popular Deep Learning Tools – a review
Popular Deep Learning Tools – a review Deep Learning is the hottest trend now in AI and Machine Lear ...
- open和fopen的区别(转)
转载自:http://www.cnblogs.com/joeblackzqq/archive/2011/04/11/2013010.html open和fopen的区别: 1.缓冲文件系统缓 冲文件系 ...
- Linux Kernel ‘kvm_set_memory_region()’函数本地提权漏洞
漏洞名称: Linux Kernel ‘kvm_set_memory_region()’函数本地提权漏洞 CNNVD编号: CNNVD-201306-343 发布时间: 2013-06-20 更新时间 ...
- 《ACM国际大学生程序设计竞赛题解I》——6.8
Poj1068: Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in ...
- python列表操作总结
list是python中非常重要的类型/数据结构,总结如下: 符号说明 l:列表 l2:新列表 e:元素 index:位置 方法: 原地修改: l.append(e) l.insert(index, ...
- javaweb项目的优化 - 几番思念
简单地来看一个浏览器用户访问的流程: 浏览器->服务器->返回结果显示 这么简单地看,可能想得到的优化手段很少,常见的可能就是优化sql,加快数据库处理:加个缓存,加快返回:使用静态文件 ...
- Gridview导出到Excel
#region #导出到Excel protected void Button2_Click(object sender, EventArgs e) { Respons ...
- ASP.NET中Get和Post的用法
单form的提交有两种方式,一种是get的方法,一种是post 的方法.看下面代码,理解ASP.NET Get和Post两种提交的区别: < form id="form1" ...
- Hibernate知识点总结
Hibernate配置二级缓存: --- 使用EhCache 1.hibernate.cfg.xml中配置二级缓存 <hibernate-configuration> <ses ...