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中,处理点击拍照确定按钮后的逻辑功能:将图片 ...
随机推荐
- JAVA参数没有引用传递,只有值传递
原文章地址:http://www.cnblogs.com/clara/archive/2011/09/17/2179493.html 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性, ...
- tooltip提示文字
<p class="tooltip-demo">Tigh <a title="Default tooltip" rel="toolt ...
- MFC截图和界面刷新相关问题
问题描写叙述: 就是首先用CDC来截图,保存图片的路径通过dlg窗体来手动设置并传入.但是截下来的图片就会连带那个对话框也截图下来. 就是这样.我想截后面那个图.前面这 ...
- android gson使用
第一步注册: compile 'com.google.code.gson:gson:2.6.2' 第二步初始化: Gson gson = new GsonBuilder() .setLenient( ...
- log4j2配置日志大小,个数等
1:设置log输出文件 https://www.cnblogs.com/sa-dan/p/6837225.html <?xml version="1.0" encoding= ...
- 【PGM】Representation--Knowledge Engineering,不同的模型表示,变量的类型,structure & parameters
Part 1. 重要的区别: Template based vs. specific Directed vs. undirected Generative vs. discrimina ...
- python + unittest 做单元测试之学习笔记
单元测试在保证开发效率.可维护性和软件质量等方面有很重要的地位,所谓的单元测试,就是对一个类,一个模块或者一个函数进行正确性检测的一种测试方式. 这里主要是就应用 python + unitest 做 ...
- Bootstrap按钮组学习
简介 通过按钮组容器把一组按钮放在同一行里.通过与按钮插件联合使用,可以设置为单选框或多选框的样式和行为. 按钮组中的工具提示和弹出框需要特别的设置 当为 .btn-group 中的元素应用工具提示或 ...
- open-falcon api相关
本文描述通过被监控endpoint的名称获取该endpoint的eid和监控项,从而获取到该endpoint的监控历史数据,使用python代码的 api操作方法 注:同步open-falcon和ag ...
- SQL学习笔记之MySQL查询练习2
(网络搜集) 0x00 数据准备 CREATE TABLE students (sno ) NOT NULL, sname ) NOT NULL, ssex ) NOT NULL, sbirthday ...