使用fragment,Pad手机共用一套代码
项目代码结构:


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手机共用一套代码的更多相关文章
- VUE实现Studio管理后台(七):树形结构,文件树,节点树共用一套代码NodeTree
本次介绍的内容,稍稍复杂了一点,用VUE实现树形结构.目前这个属性结构还没有编辑功能,仅仅是展示.明天再开一篇文章,介绍如何增加编辑功能,标题都想好了.先看今天的展示效果: 构建树必须用到递归,使用s ...
- web技术栈开发原生应用-多端共用一套代码
weex: vuejs开发原生应用 nativescript: vuejs开发原生应用 ReactNative = reactjs开发原生应用 ionic = angularjs 开发原生应用
- 【深入ASP.NET原理系列】--Asp.Net Mvc和Asp.Net WebForm共用一套ASP.NET请求管道
.NET FrameWork4在系统全局配置文件(如在如下目录中C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config) 中添加了一个名字叫Url ...
- 推荐:Asp.Net MVC 多语言(html+js共用一套资源文件)
此文主要是最近做多语言的工作的一个经验分享.文中的内容为参照多位大神的方案后,自己揉捏出来的一个新的方案,对于html和javascript部分的多语言的切换,共用一套资源文件.代码中主要是使用 IH ...
- 使用 Gradle 实现一套代码开发多个应用
代码地址如下:http://www.demodashi.com/demo/11297.html 在文章 使用 Gradle 对应用进行个性化定制 中,我们能够针对一个应用的正式服.测试服.超管服等其他 ...
- 利用Synergy在局域网内让Ubuntu和Windows 7两台机器共用一套键鼠。
一个主机可以连接多个显示器, 方便自己使用, 但是这只是一个系统分屏显示, 如果想用两台不同系统的电脑, 并且还不想老是在两套键鼠之间来回转换, 那么建议你可以用Synergy软件来实现多台电脑之间的 ...
- 【原创】从零开始搭建Electron+Vue+Webpack项目框架,一套代码,同时构建客户端、web端(二)
摘要:上篇文章说到了如何新建工程,并启动一个最简单的Electron应用.“跑起来”了Electron,那就接着把Vue“跑起来”吧.有一点需要说明的是,webpack是贯穿这个系列始终的,我也是本着 ...
- 【深入ASP.NET原理系列】--Asp.Net Mvc和Asp.Net WebForm实际上共用一套ASP.NET请求管道
.NET FrameWork4在系统全局配置文件(如在如下目录中C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config) 中添加了一个名字叫Url ...
- zepto手机拼音字母城市选择器代码
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...
随机推荐
- windows完全支持C++11的轻量级编译器(官网MinGW和非官方的MinGW-builds)
作者:网事如风链接:https://www.zhihu.com/question/22923569/answer/23172337来源:知乎著作权归作者所有,转载请联系作者获得授权. 完全支持C++1 ...
- 【温故而知新:文件操作】C#的文件读写相关
StreamReader类以及其方法ReadLine,Read,ReadToEnd的分析 首先StreamReader类的构造参数非常丰富在这里,我觉得最常用的就是StreamReader(Strea ...
- 【转】Windows与Linux(Ubuntu)双系统时间不一致的解决方法
当在嵌入式Linux里面备份文件时候,在备份的时候,PC(win7)和开发板的时间都是9:30,但是在开发板发现文件创建时间是9:30,然后u盘插在PC(win7)上,发现文件创建时间是1:30,为什 ...
- poj 3320 Jessica's Reading Problem(尺取法)
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
- 后台数据导出为Excel
数据导出的方法如下: 一.下载office的类库:microsoft.office.interop.excel.zip 根据电脑安装的office版本选择引入相应的类库,office2007选择12. ...
- Linux各种包安装及命令
1.Locate yum -y install mlocate 若出现问题: locate: can not stat () `/var/lib/mlocate/mlocate.db': 没有那个文件 ...
- Javascript 解构的用处
对象的解构赋值,可以很方便地将现有对象的方法,赋值到某个变量. let { log, sin, cos } = Math; 上面代码将Math对象的对数.正弦.余弦三个方法,赋值到对应的变量上,使用起 ...
- 免费自学Cocos2d-x3.0final2014原创视频教程(56集)(适用于Cocos2d-x3.1 Cocos2d-x3.2版本号全)
Cocos2d-x3.0final(适用于Cocos2d-x3.1 Cocos2d-x3.2版本号全) 视频播放地址:http://yun.itxdl.cn/course/62 视频下载地址:http ...
- Android 使用开源xUtils来实现多线程下载(非原创)
1.程序员自己也是可以实现多线程下载的,只是代码量比较大,而且,其中有许多细节需要考虑到,在GitHub上有人写好的代码,我们可以拿过来使用下,节省了我们开发程序的时间 2.导包:xUtils-2.6 ...
- Web 上一页下一页 用超链接 用按钮
方法一超链接 Default.aspx.cs html代码************************************************************* ...