项目代码结构:

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. LeetCode_Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  2. unix c 10

    网络常识:    OSI 7层模型  TCP模型    IP和端口    IP是用来定位网络中的计算机,端口用来代表 计算机中的某个进程.    IP 有点分十进制 和 十六进制的两种表示方式,底层 ...

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

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

  4. 苹果拒绝App内部使用版本检测功能

    10.6 - Apple and our customers place a high value on simple, refined, creative, well thought through ...

  5. 包含深度学习常用框架的Docker环境

    相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ All in one d ...

  6. Zabbix使用外部邮箱服务器发送邮件报警

    本来是想自己写一篇文章的,但是看到发现网上有写的不错的,于是乎又抄别人的文章,作为记录. 使用外部邮箱来发生邮件明显好处就是防止其他邮箱服务器当垃圾邮件处理,另一方面能降低收邮件延迟. 下面开始进行使 ...

  7. 关于 knockout js 学习中的疑问 (1)

    最近刚刚学习knockout中遇到如下问题: 1.在给viewModel定义一个方法时,有时后面跟 的this,有的时候没有 如下所示: this.fullName = ko.computed(fun ...

  8. 本地计算机上的OracleOraDb11g_home2TNSListener服务启动又停止了。

    电脑上装了oracle后启动很慢,然后我就不oracle服务设置成手动启动,没想到今天启动的时候居然报错 折腾了一上午,终于搞定, 在环境变量中把ORACLE_HOME 设置成D:\app\XL\pr ...

  9. CSS 设计彻底研究(四)盒子的浮动与定位

    第四章 盒子的浮动与定位 本章的重点和难点是深刻地理解”浮动“和”定位“这两个重要的性质,对于复杂页面的排版至关重要. 4.1 盒子的浮动 在标准流中,一个块级元素在水平方向会自动伸张,直到包含它的元 ...

  10. (原+转)ubuntu16中莫名死机及重新安装显卡驱动

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5992693.html 参考网址: http://blog.csdn.net/u012581999/ar ...