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中,处理点击拍照确定按钮后的逻辑功能:将图片 ...
随机推荐
- find the safest road---hdu1596(最短路模板求最大概率)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1596 求给定的任意两点之间的最大安全概率,概率之间是相乘的关系,所以注意初始化即可 #include& ...
- 给所有开发者的React Native详细入门指南
建议先下载好资料后,再阅读本文.demo代码和资料下载 目录 一.前言 二.回答一些问题 1.为什么写此教程 2.本文适合哪些人看 3.如何使用本教程 4.需要先学习JavaScript.HTML.C ...
- Flask系列之源码分析(一)
目录: 涉及知识点 Flask框架原理 简单示例 路由系统原理源码分析 请求流程简单源码分析 响应流程简单源码分析 session简单源码分析 涉及知识点 1.装饰器 闭包思想 def wapper( ...
- Spark提交应用程序之Spark-Submit分析
1.提交应用程序 在提交应用程序的时候,用到 spark-submit 脚本.我们来看下这个脚本: if [ -z "${SPARK_HOME}" ]; then export S ...
- golang 复制对象的正确做法
需求 实际运用种,传参是一对象指针,现在如何最简便地复制一对象? 实现 坑:&* 先拿到值再指针? package main import ( "time" " ...
- 系统管理命令之who
作为一名系统管理员,我们需要弄清楚在一个特定的时间点都有谁活跃在系统上.必须严密监视我们的服务器.但是,我们当然不可能一天24小时都盯着系统.在linux系统中,我们可以使用who命令查看登录者信息. ...
- MySQL基础语句【学习笔记】
放在这里,以备后查. 1. 数据库, 数据库服务器, 数据库语言 数据库,是持久性数据的集合,供给定企业的应用程序系统使用,并且由一个数据库管理系统来管理: 数据库服务器,又称数据库管理系统,用来管理 ...
- centOS下升级python版本,详细步骤
1.可利用linux自带下载工具wget下载,如下所示:( 笔者安装的是最小centos系统,所以使用编译命令前,必须安装wget服务,读者如果安装的是界面centos系统,或者使用过编译工具则可跳 ...
- springcloud20---Config加入eureka
Config server也可以加用户名和密码.Config client通过用户名和密码访问. Config server也可以做成高可用集群. Config与eureka配置使用.把Config ...
- Python笔记 #16# Pandas: Operations
10 Minutes to pandas #Stats # shift 这玩意儿有啥用??? s = pd.Series([1,5,np.nan], index=dates).shift(0) # s ...