Fragment在Activity中跳转,实现类似新闻标题跳转新闻内容功能
1.准备的工作,新闻数据类,新闻数据适配器,适配器的布局:
News.java
package com.example.zps.fourfragmentbestpractice; /**
* Created by zps on 2015/9/1.
*/
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.java
package com.example.zps.fourfragmentbestpractice; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView; import java.util.List; /**
* Created by zps on 2015/9/1.
*/
public class NewsAdapter extends ArrayAdapter<News>{
private int resourceId;
public NewsAdapter(Context context, int resource, List<News> objects) {
super(context, resource, objects);
resourceId = resource;
} @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获取到news的数据;
TextView newsTitleText = (TextView)view.findViewById(R.id.news_item_news_title);
newsTitleText.setText(news.getTitle());
return view;
}
}
news_adapter.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_item_news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textSize="18sp"
/>
</LinearLayout>
2 . 实现新闻标题页面:
首先里面子布局用fragment实现,frament用自定义Fragment类来实现fragment布局的功能,比如跳转到另一个fragment;
然后,在总体布局的页面通过自定义的Fragment类加载fragment布局;
最后,用一个活动类来实现总布局页面;
news_title_fragment.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/news_title_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
NewsTitleFragment.java
package com.example.zps.fourfragmentbestpractice; import android.app.Activity; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView; import java.util.ArrayList;
import java.util.List; /**
* Created by zps on 2015/9/1.
*/
public class NewsTitleFragment extends Fragment implements AdapterView.OnItemClickListener {
private List<News> listNews;
private ListView newsTitleListView;
private NewsAdapter adapter;
private boolean isTwoPane;
//首先执行onAttach()对数据初始化
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//获取模拟新闻数据
listNews = getNews();
//设置适配器的数据,适配器加载news_item.xml布局
adapter = new NewsAdapter(activity, R.layout.news_adapter, listNews); } //再执行onCreateView()对布局界面初始化
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_title_fragment, container, false);
newsTitleListView = (ListView) view.findViewById(R.id.news_title_list_view);
//取得适配器的数据,为news_title_frag.xml布局下的ListView视图的子视图设置适配器;
newsTitleListView.setAdapter(adapter);
newsTitleListView.setOnItemClickListener(this);
return view;
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (getActivity().findViewById(R.id.news_content_layout)!=null) {
isTwoPane = true;
}else {
isTwoPane = false;
} } @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
News news = listNews.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<News> getNews() {
listNews = new ArrayList<>();
News news1 = new News();
news1.setTitle("会见连战等出席抗战胜利纪念活动的台湾各界代表人士");
news1.setContent("新华网快讯:中共中央总书记1日上午在人民大会堂会见出席抗战胜利70周年纪念活动的连战等台湾各界代表人士。");
listNews.add(news1);
News news2 = new News();
news2.setTitle("9月2日-4日256条公交绕行或停驶");
news2.setContent("北京市交通委表示,北京市公共交通运行将严格执行通告措施,按时尽快恢复公共交通运行,尽量减少对市民出行的影响。同时,在北京火车站等重点地区加大地面公交和出租保障措施,自9月2日20:00起至阅兵活动结束,公交、出租启动专项保障方案。\n" +
"\n" +
"地面公交方面,日班线路20路、25路、29路、39路、52路等5条日班线路,配车147部,每条线路增加5部机动车辆。可接驳地铁4号线、5号线、7号线和10号线四条地铁线路,确保抵京客流的及时疏散。\n" +
"\n" +
"夜班线夜17路、夜19路等2条夜班线路,配车8部,每条线路增加5部机动车辆,共配车18部。可接驳北京站东、北京焦化厂方向");
listNews.add(news2);
return listNews;
}
}
news_title.xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/news_title_fragment"
android:name="com.example.zps.fourfragmentbestpractice.NewsTitleFragment"/> </LinearLayout>
NewsTitleActivity.java
(注意:
因为所有的Fragment类 是继承import android.support.v4.app.Fragment;(也可以继承import android.support.app.Fragment,我没精力去弄了)
所以FragmentActivity类 要继承FragmentActivity 类)
package com.example.zps.fourfragmentbestpractice; import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window; public class NewsTitleActivity extends FragmentActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.news_title);
} }
3.实现新闻内容页面:
基本思路和实现新闻标题的一样,不同点:
NewsContentActivity活动类通过NewsTitleActivity中的命令:NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent())来得到数据,而不是通过news类直接获得;
news_content_fragment.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"
android:orientation="vertical"> <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_content_news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:scaleType="fitXY"
android:src="@drawable/spilt_line_line" />
<TextView
android:id="@+id/news_content_news_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="15dp"
android:textSize="18sp"/>
</LinearLayout>
<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/split_line_vertical"/>
</RelativeLayout>
NewsContentFragment.java
package com.example.zps.fourfragmentbestpractice; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; /**
* Created by zps on 2015/9/1.
*/
public class NewsContentFragment extends Fragment {
private View view; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.news_content_fragment, 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_content_news_title);
TextView newsContentText = (TextView) view.findViewById(R.id.
news_content_news_content);
newsTitleText.setText(newsTitle);
newsContentText.setText(newsContent);
}
}
news_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:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.zps.fourfragmentbestpractice.NewsContentFragment"/>
</LinearLayout>
NewsContentActivity.java
package com.example.zps.fourfragmentbestpractice; import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window; /**
* Created by zps on 2015/9/1.
*/
public class NewsContentActivity extends FragmentActivity { 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) getSupportFragmentManager()
.findFragmentById(R.id.news_content_fragment); newsContentFragment.refresh(newsTitle,newsContent); }
}
Fragment在Activity中跳转,实现类似新闻标题跳转新闻内容功能的更多相关文章
- Fragment在Activity中的应用 (转载)
原文链接 http://www.cnblogs.com/nanxin/archive/2013/01/24/2875341.html 在本小节中介绍在Activity中创建Fragment. 官网有很 ...
- Android ViewPager+Fragment 在Activity中获取Fragment的控件
如果ViewPager+Fragment实现Tab切换,在activity中利用adapter.getItem获取到fragment然后再根据fragment.的方法获取控件 //隐藏求租,以下代码用 ...
- 多个Fragment在一个activity中通过按钮的展示方法
fragment使用方法 1. 创建主Mainactivity extends AppCompatActivity 2. Oncreate & setContentView 3. 完成XML的 ...
- android中清空一个表---类似truncate table 表名 这样的功能 android sqlite 清空数据库的某个表
public void clearFeedTable(){ String sql = "DELETE FROM " + FEED_TABLE_NAME +";" ...
- Android进阶(十二)Fragment VS Activity
Fragment VS Activity Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这 ...
- Fragment、Activity比较——Android碎片介绍
Fragment是Android honeycomb 3.0新增的概念,Fragment名为碎片不过却和Activity十分相似,下面介绍下Android Fragment的作用和用法.Fragmen ...
- Android Fragment与Activity通讯详解
与activity通讯 尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例. Fragment可 ...
- Android进阶之Fragment与Activity之间的数据交互
1 为什么 因为Fragment和Activity一样是具有生命周期,不是一般的bean通过构造函数传值,会造成异常. 2 Activity把值传递给Fragment 2.1 第一种方式,也是最常用的 ...
- 在其他Activity中展示自定义相机拍的照片
在使用相机拍照中,我们需要当点击了确定按钮之后,拍的照片展示在其他Activity的ImageView中,代码如下: 1.首先在自定义相机的Activity中,处理点击拍照确定按钮后的逻辑功能:将图片 ...
随机推荐
- 数据网格和树-EasyUI Datagrid 数据网格、EasyUI Propertygrid 属性网格、EasyUI Tree 树、EasyUI Treegrid 树形网格
EasyUI Datagrid 数据网格 扩展自 $.fn.panel.defaults.通过 $.fn.datagrid.defaults 重写默认的 defaults. 数据网格(datagrid ...
- 6.MySQL必知必会之数据过滤-WHERE组合子句
数据过滤-WHERE组合子句 本章讲授如何组合WHERE子句以建立功能更强的更高级的搜索条件. 我们还将学习如何使用NOT和IN操作符. 1.组合WHERE子句 上一章介绍的WHERE子句在过滤数据时 ...
- vue-router的hash模式与history模式的对比
Vue-router 中hash模式和history模式的关系在vue的路由配置中有mode选项 最直观的区别就是在url中 hash 带了一个很丑的 # 而history是没有#的mode:&quo ...
- Fms3和Flex打造在线多人视频会议和视频聊天(附原代码)
Flex,Fms3系列文章导航 Flex,Fms3相关文章索引 本篇是视频聊天,会议开发实例系列文章的第3篇,该系列所有文章链接如下: http://www.cnblogs.com/aierong/a ...
- iOS开发之开发者申请
一.对于真机调试,首先要在苹果网站上注册APP ID,以及购买iPhone Develop Program(iDP) 开发者授权,99美元.然后要创建证书请求CSR,创建步骤如下: 1.Mac O ...
- Struts2中struts.multipart.maxSize配置
今天使用Struts2的文件上传控件时,在struts.xml中,将处理上传的action中的fileUpload拦截器的maximumSize参数设置为5000000,上传了一个3M的文件后发现控制 ...
- 浅谈history对象以及路由插件原理
简介 History对象最初设计用来表示窗口的浏览历史,但是,出于隐私方面的原因,History对象不再允许脚本访问已经访问过的实际URL.虽然,我们不清楚历史URL,但是,我们可以通过History ...
- MySQL事务之-2
在上一篇中我们提到了MySQL的事务特性,这一片主要讲述事务的实现. 事务的隔离性由锁来实现.原子性,一致性,持久性通过数据库的redo和undo log来实现. redo恢复提交事务修改页的操作,而 ...
- STM32 DMA简述
STM32 DMA简述 DMA (Direct Memory Access) 直接内存存储器,在做数据传输时能够大大减轻CPU的负担. DMA的作用 DMA提供了一个关于数据的高数传输通道,这个通道不 ...
- InstallShield的工程类型的选择
转载:http://blog.csdn.net/wuxiaoqrs/article/details/45717695 InstallScript vs. Basic MSI InstallScript ...