编写利用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创建新闻列表的更多相关文章

  1. 利用Fragment创建动态UI 之 Fragment之间的通信

    为了可以复用一个fragment,所以在定义fragment的时候,要把它定义为一个完全独立和模块化,它有它自己的layout和行为.当你定义好了这些可复用的fragment,可以把他们和activi ...

  2. vue ui九宫格、底部导航、新闻列表、跨域访问

    一.  九宫格 九宫格:在mint-ui组件库基于vue框架 mui不是基于vue框架 只是css/js文件 (1)官方网站下载安装包 (2)copy css js fonts[字体图标] src/l ...

  3. Angular2快速入门-2.创建一个新闻列表

    背景: 我们想通过一个例子,展示下Angular2 怎么绑定页面,怎么创建Component, 例子:我们创建一个新闻列表,当点击新闻列表中某一条新闻的时候,展示出该条新闻的详细信息, 在详细信息中可 ...

  4. 【JavaWeb】MVC案例之新闻列表

    MVC案例之新闻列表 作者:白宁超 2016年6月6日15:26:30 摘要:本文主要针对javaweb基本开发之MVC案例的简单操作,里面涉及mysql数据库及表的创建,以及jsp页面和servle ...

  5. Oracle Sales Cloud:管理沙盒(定制化)小细节1——利用公式创建字段并显示在前端页面

    Oracle Sales Cloud(Oracle 销售云)是一套基于Oracle云端的CRM管理系统.由于 Oracle 销售云是基于 Oracle 云环境的,它与传统的管理系统相比,显著特点之一便 ...

  6. 利用Oracle创建表空间和用户

    本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6217152.html 第一步,创建表空间 以SYS/sys账户和SYSDBA身份登录 ...

  7. 利用navicat创建存储过程、触发器和使用游标的简单实例

    利用navicat创建存储过程.触发器和使用游标的简单实例 标签: navicat存储过程触发器mysql游标 2013-08-03 21:34 15516人阅读 评论(1) 收藏 举报  分类: 数 ...

  8. Android 用Fragment创建一个选项卡

    本文结合之前的动态创建fragment来进行一个实践,来实现用Fragment创建一个选项卡 本文地址:http://www.cnblogs.com/wuyudong/p/5898075.html,转 ...

  9. sharepoint2010 创建自定义列表

    转:http://boke.25k5.com/kan77298.html 如何创建自定义列表 首先了解创建自定义列表中涉及到的几个名词:栏.内容类型. ①栏:栏即列.字段(Field),MSDN中给出 ...

随机推荐

  1. C# 关于NULL 可空值类型 ? 和空接操作符??

    作者 陈嘉栋(慕容小匹夫) C#引入了可空值类型的概念.在介绍究竟应该如何使用可空值类型之前,让我们先来看看在基础类库中定义的结构--System.Nullable<T>.以下代码便是Sy ...

  2. 关于javascript

    Client-Language-----------------------JavaScript/Object-C/Java/C# HTML5 DOM/Template/Data/Ajax/Regul ...

  3. Jump

    hdu4862:http://acm.hdu.edu.cn/showproblem.php?pid=4862 题意:给你n*m的方格,每个方格中有一个数(0---9),然后你每次可以选择一个点开始,这 ...

  4. 用STRACE解决公司真实故障一例

    这是相关分析文档.为了职业操守,已修改相关公司敏感信息~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ 关于论坛每五分钟左右,会有warning.html跳转出现的原因调查 (warning. ...

  5. 使用spring-amqp结合使用rabbitmq

    maven 依赖包配置如下: <dependencies> <dependency> <groupId>org.springframework.amqp</g ...

  6. android 随手记 自定义广播通知栏

    自定义通知栏图标?不是很简单么.自定义布局都不在话下! 是的,有xml布局文件当然一切都很简单,如果不给你布局文件用呢? 听我慢慢道来! 首先怎么创建一个通知呢? 1.new 一个 Notificat ...

  7. 【动态规划】【最短路】Codeforces 710E Generate a String

    题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...

  8. firefox HackBar组件模拟请求POST请求

    组件下载地址:https://addons.mozilla.org/zh-CN/firefox/addon/hackbar/

  9. CLOSE-UP FORMALWEAR_意大利进口_2015秋冬_男装发布会_西装图片系列_男装西装设计资料_WeArTrends时尚资讯网_国内最专业的服装设计资讯网站

    CLOSE-UP FORMALWEAR_意大利进口_2015秋冬_男装发布会_西装图片系列_男装西装设计资料_WeArTrends时尚资讯网_国内最专业的服装设计资讯网站 CLOSE-UP FORMA ...

  10. poj1065

    题目大意: 木棍(好吧,承认确实做过这个题,嘎嘎) 有一堆木棍大约有n根,木棍的长度和重量都预先知道,这些木棍会在一个木工机械上一个接一个的处理,这需要一些时间,称为设置时间,为机械准备处理一根木头, ...