项目代码结构:

1:MainActivity.java

package com.example.fgtest;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); } }

layout/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"
android:orientation="vertical" > <fragment
android:id="@+id/fgNoticeTitle"
android:name="com.wzh.fragment.NoticeTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

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"
android:orientation="horizontal" > <fragment
android:id="@+id/fgNoticeTitle"
android:name="com.wzh.fragment.NoticeTitleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/> <FrameLayout
android:id="@+id/flNoticeContent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3">
<fragment
android:id="@+id/fgNoticeContent"
android:name="com.wzh.fragment.NoticeContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>

2:实体对象Notice.java

package com.wzh.bean;

public class Notice {
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;
} }

3:NoticeAdapter.java

package com.wzh.adapter;

import java.util.List;

import com.example.fgtest.R;
import com.wzh.bean.Notice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView; public class NoticeAdapter extends ArrayAdapter<Notice>{
int resourceId; public NoticeAdapter(Context context, int resourceId, List<Notice> list){
super(context, resourceId, list);
this.resourceId = resourceId;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
Notice notice = getItem(position);
ViewHolder viewHolder; if(convertView==null){
convertView = LayoutInflater.from(getContext()).inflate(this.resourceId, null); viewHolder = new ViewHolder(); viewHolder.tvNoticeTitle = (TextView)convertView.findViewById(R.id.tvNoticeTitle); convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}      viewHolder.tvNoticeTitle.setText(notice.getTitle()); return convertView;
} class ViewHolder{
TextView tvNoticeTitle;
}
}

fragment_notice_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"
android:layout_margin="8dp"> <TextView
android:id="@+id/tvNoticeTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"/> <TextView
android:id="@+id/tvNoticeContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/> </LinearLayout>

4:NoticeContentActivity.java

package com.example.fgtest;

import com.wzh.fragment.NoticeContentFragment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; public class NoticeContentActivity extends Activity{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_notice_content); String title = getIntent().getStringExtra("title");
String content = getIntent().getStringExtra("content"); NoticeContentFragment noticeContentFragment = (NoticeContentFragment)
getFragmentManager().findFragmentById(R.id.fgNoticeContent);
noticeContentFragment.refreshNoticeContent(title, content); } public static void openActivity(Context context, String title, String content){
Intent intent = new Intent(context, NoticeContentActivity.class);
intent.putExtra("title", title);
intent.putExtra("content", content);
context.startActivity(intent);
}
}

activity_notice_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/fgNoticeContent"
android:name="com.wzh.fragment.NoticeContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

5:NoticeContentFragment.java

package com.wzh.fragment;

import com.example.fgtest.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class NoticeContentFragment extends Fragment{
View view; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_notice_content, container, false); return view;
} public void refreshNoticeContent(String title,String content){
TextView tvNoticeTitle = (TextView)view.findViewById(R.id.tvNoticeTitle);
TextView tvNoticeContent = (TextView)view.findViewById(R.id.tvNoticeContent); tvNoticeTitle.setText(title);
tvNoticeContent.setText(content);
}
}

fragment_notice_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"
android:layout_margin="8dp"> <TextView
android:id="@+id/tvNoticeTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"/> <TextView
android:id="@+id/tvNoticeContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/> </LinearLayout>

6:NoticeTitleFragment.java

package com.wzh.fragment;

import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView; import com.example.fgtest.NoticeContentActivity;
import com.example.fgtest.R;
import com.wzh.adapter.NoticeAdapter;
import com.wzh.bean.Notice; public class NoticeTitleFragment extends Fragment{ NoticeAdapter noticeAdapter;
ListView lvNoticeTitle;
List<Notice> noticeList;
boolean isTwoPanel = false;
int curClickPosition = -1;
NoticeContentFragment noitceContentFragment; @Override
public void onAttach(Activity activity) {
super.onAttach(activity);
getNoticeTestData();
noticeAdapter = new NoticeAdapter(activity,R.layout.list_notice_title_item,noticeList);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_notice_title, container, false); lvNoticeTitle = (ListView)view.findViewById(R.id.lvNoticeTitle);
lvNoticeTitle.setAdapter(noticeAdapter);
lvNoticeTitle.setOnItemClickListener(itemClickListener()); return view;
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); if(getActivity().findViewById(R.id.flNoticeContent)!=null){
isTwoPanel = true;
noitceContentFragment = (NoticeContentFragment)
getActivity().getFragmentManager().findFragmentById(R.id.fgNoticeContent);
if(noticeList.size()>0){
Notice notice = noticeList.get(0);
noitceContentFragment.refreshNoticeContent(notice.getTitle(), notice.getContent());
}
}else{
isTwoPanel = false;
}
} OnItemClickListener itemClickListener(){
return new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Notice notice = noticeList.get(arg2);
String title = notice.getTitle();
String content = notice.getContent();
if(isTwoPanel){
if(curClickPosition!=arg2){
noitceContentFragment.refreshNoticeContent(title, content);
}
curClickPosition = arg2;
}else{
NoticeContentActivity.openActivity(getActivity(), title, content);
}
} };
} void getNoticeTestData(){
noticeList = new ArrayList<Notice>();
Notice notice = new Notice();
notice.setTitle("测试标题1");
notice.setContent("测试标题内容1");
noticeList.add(notice);
notice = new Notice();
notice.setTitle("测试标题2");
notice.setContent("测试标题内容2");
noticeList.add(notice);
notice = new Notice();
notice.setTitle("测试标题3");
notice.setContent("测试标题内容3");
noticeList.add(notice);
}
}

fragment_notice_title.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/lvNoticeTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

使用fragment,Pad手机共用一套代码的更多相关文章

  1. VUE实现Studio管理后台(七):树形结构,文件树,节点树共用一套代码NodeTree

    本次介绍的内容,稍稍复杂了一点,用VUE实现树形结构.目前这个属性结构还没有编辑功能,仅仅是展示.明天再开一篇文章,介绍如何增加编辑功能,标题都想好了.先看今天的展示效果: 构建树必须用到递归,使用s ...

  2. web技术栈开发原生应用-多端共用一套代码

    weex: vuejs开发原生应用 nativescript: vuejs开发原生应用 ReactNative = reactjs开发原生应用 ionic = angularjs 开发原生应用

  3. 【深入ASP.NET原理系列】--Asp.Net Mvc和Asp.Net WebForm共用一套ASP.NET请求管道

    .NET FrameWork4在系统全局配置文件(如在如下目录中C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config) 中添加了一个名字叫Url ...

  4. 推荐:Asp.Net MVC 多语言(html+js共用一套资源文件)

    此文主要是最近做多语言的工作的一个经验分享.文中的内容为参照多位大神的方案后,自己揉捏出来的一个新的方案,对于html和javascript部分的多语言的切换,共用一套资源文件.代码中主要是使用 IH ...

  5. 使用 Gradle 实现一套代码开发多个应用

    代码地址如下:http://www.demodashi.com/demo/11297.html 在文章 使用 Gradle 对应用进行个性化定制 中,我们能够针对一个应用的正式服.测试服.超管服等其他 ...

  6. 利用Synergy在局域网内让Ubuntu和Windows 7两台机器共用一套键鼠。

    一个主机可以连接多个显示器, 方便自己使用, 但是这只是一个系统分屏显示, 如果想用两台不同系统的电脑, 并且还不想老是在两套键鼠之间来回转换, 那么建议你可以用Synergy软件来实现多台电脑之间的 ...

  7. 【原创】从零开始搭建Electron+Vue+Webpack项目框架,一套代码,同时构建客户端、web端(二)

    摘要:上篇文章说到了如何新建工程,并启动一个最简单的Electron应用.“跑起来”了Electron,那就接着把Vue“跑起来”吧.有一点需要说明的是,webpack是贯穿这个系列始终的,我也是本着 ...

  8. 【深入ASP.NET原理系列】--Asp.Net Mvc和Asp.Net WebForm实际上共用一套ASP.NET请求管道

    .NET FrameWork4在系统全局配置文件(如在如下目录中C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config) 中添加了一个名字叫Url ...

  9. zepto手机拼音字母城市选择器代码

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. heritrix 3.2.0 下载

    由于archive.org屏蔽,编译完成版本 http://builds.archive.org/maven2/org/archive/heritrix/heritrix/3.2.0/ 无法下载. 现 ...

  2. STL List容器

    转载http://www.cnblogs.com/fangyukuan/archive/2010/09/21/1832364.html 各个容器有很多的相似性.先学好一个,其它的就好办了.先从基础开始 ...

  3. 《招聘一个靠谱的iOS》面试题参考答案(下)

    相关文章: <招聘一个靠谱的iOS>面试题参考答案(上) 说明:面试题来源是微博@我就叫Sunny怎么了的这篇博文:<招聘一个靠谱的 iOS>,其中共55题,除第一题为纠错题外 ...

  4. hadoop之mapreduse 在Eclipse下的调试环境篇

    搭建完毕环境后,開始调试mapreduse程序. 可是遇到不停的报错.本人非常讨厌在自己的操作系统环境变量里设置来设置去,包含linux也是. 通常喜欢把设置环境变量在启动程序的脚本中.让脚本自己执行 ...

  5. 利用boost获取时间并格式化

    利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题. 1. 输出YYYYMMDD #include <boost/date_time/gregorian/gregorian.hpp& ...

  6. ThinkPHP整合百度Ueditor图文教程

    ThinkPHP整合百度Ueditor图文教程 ThinkPHP整合百度Ueditor,基于黄永成老师的视频说明的申明:最好大家都能写绝对路径的都写好绝对路径比如:window.UEDITOR_HOM ...

  7. Vue2.0环境搭建和测试demo

    Vue2.0 推荐开发环境 Homebrew 1.0.6(Mac).Node.js 6.7.0.npm 3.10.3.webpack 1.13.2.vue-cli 2.4.0.Atom 1.10.2 ...

  8. nopCommerce架构分析系列(二)数据Cache

    原文(http://www.cnblogs.com/gusixing/archive/2012/04/12/2443799.html)非常感谢作者顾思行的分享! 序言 在很多访问量较大的系统中,尤其在 ...

  9. jquery中的 ajax 以及map遍历

    1.语法 $.ajax{ type:'get',//类型 有get post url:'',//路径 data:{name:$('#ma').val(),nameq:$('#maq').val()}, ...

  10. Android 使用Facebook的 Stetho工具

    Stetho在Android Studio中用: 1, 引入 compile 'com.facebook.stetho:stetho:1.3.1' compile 'com.facebook.stet ...