编写利用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中给出 ...
随机推荐
- GNU project C
gcc - GNU project C and C++ compiler gcc [option] file... preprocessing compila ...
- Node.js回调概念
什么是回调? 回调是一个异步等效的功能.在完成特定任务回调函数被调用. Node大量使用了回调.Node的所有的API都支持回调这样的一种方式. 例如,一个函数读取一个文件可能开始读取文件,并使得下一 ...
- PHP实现单击“添加”按钮增加一行表单项,并将所有内容插入到数据库中
PHP实现单击“添加”按钮增加一行表单项,并将所有内容插入到数据库中 效果图: html+jquery: <html> <head> <meta http-equiv=& ...
- POJ 1651 Multiplication PuzzleDP方法:
有N张写有数字的卡片排成一行,按一定次序从中拿走N-2张(第1张和最后一张不能拿),每次只拿一张,取走一张卡片的同时,会得到一个分数,分值的计算方法是:要拿的卡片,和它左右两边的卡片,这三张卡片上数字 ...
- 贪心 BZOJ 3671:[Noi2014]随机数生成器
Description Input 第 1行包含5个整数,依次为 x_0,a,b,c,d ,描述小H采用的随机数生成算法所需的随机种子.第2行包含三个整数 N,M,Q ,表示小H希望生成一个1到 ...
- C#中object sender,EventHandler e有个毛作用
button1_Click(object sender,EventHandler e) { Button button=(Button)sender; button.Text="text p ...
- Combination Sum III —— LeetCode
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- getElementById返回的是什么?串讲HTML DOM
1. getElementById()返回的是什么? 这个函数使用的最普遍,但是你有没有深入探究下,这个函数究竟返回的是什么么?我们来一起看看. var mydivEle = document.get ...
- Change ugly fonts in Firefox (KDE)
When you use KDE the fonts in Firefox are ugly. This is the fix: Open with Kate: /home/yourusername/ ...
- 《Euclidea3》-Eta-07
Q: 分析:考虑到充分利用三等分和角度的信息,这里我们只需做出一个36°的角即可. 考虑一个顶角是36°的等腰三角形.如下图. 设AD=a1,CD=a2,根据相似,易得a1:a2=(√5-1)/2. ...