效果图:

代码如下:

1、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/title"
class="com.njupt.fragment1.TitleFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
/> <FrameLayout
android:id="@+id/detail"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
/> </LinearLayout>

2、TitleFragment

package com.njupt.fragment1;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class TitleFragment extends ListFragment { private String[] data = new String[]{
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
"星期天"
};
private int currentPosition = 0; @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_activated_1,android.R.id.text1,data));
showDetail(currentPosition);
} public void showDetail(int index){
FragmentManager fm = getFragmentManager();
DetailFragment detail = (DetailFragment) fm.findFragmentById(R.id.detail);
if(detail == null || index != detail.getShowIndex()){
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setItemChecked(index, true);
detail = DetailFragment.getInstance(index); FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.detail, detail);
ft.commit();
} } @Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id); showDetail(position);
} }

3、DetailFragment

package com.njupt.fragment1;

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 DetailFragment extends Fragment { private String[] data = new String[]{
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
"星期天",
}; public static DetailFragment getInstance(int index){
DetailFragment df = new DetailFragment();
Bundle args = new Bundle();
args.putInt("index", index);
df.setArguments(args);
return df;
} public int getShowIndex(){
return getArguments().getInt("index");
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView tv = new TextView(getActivity());
tv.setText(data[getShowIndex()]);
return tv;
} }

fragment的入门DEMO的更多相关文章

  1. 【SSH系列】初识spring+入门demo

    学习过了hibernate,也就是冬天,经过一个冬天的冬眠,当春风吹绿大地,万物复苏,我们迎来了spring,在前面的一系列博文中,小编介绍hibernate的相关知识,接下来的博文中,小编将继续介绍 ...

  2. 基于springboot构建dubbo的入门demo

    之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...

  3. apollo入门demo实战(二)

    1. apollo入门demo实战(二) 1.1. 下载demo 从下列地址下载官方脚本和官方代码 https://github.com/nobodyiam/apollo-build-scripts ...

  4. lua入门demo(HelloWorld+redis读取)

    1. lua入门demo 1.1. 入门之Hello World!! 由于我习惯用docker安装各种软件,这次的lua脚本也是运行在docker容器上 openresty是nginx+lua的各种模 ...

  5. netty入门demo(一)

    目录 前言 正文 代码部分 服务端 客服端 测试结果一: 解决粘包,拆包的问题 总结 前言 最近做一个项目: 大概需求: 多个温度传感器不断向java服务发送温度数据,该传感器采用socket发送数据 ...

  6. canal入门Demo

    关于canal具体的原理,以及应用场景,可以参考开发文档:https://github.com/alibaba/canal 下面给出canal的入门Demo (一)部署canal服务器 可以参考官方文 ...

  7. C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序

    C#中缓存的使用   缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可:  <%@ Outp ...

  8. SpringBoot 入门 Demo

    SpringBoot   入门 Demo Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从 ...

  9. ReactJs 入门DEMO(转自别人)

    附件是分享的一些他人的ReactJs入门DEMO,以前版本使用的是JSXTransformer.js,新版的用browser.min.js替代了. DEMO 下载地址:http://files.cnb ...

随机推荐

  1. [.NET | 發佈] 如何指定固定的目錄給程式調用的外部DLL?

    1.OverView 一般程式只會查找與主程式同目錄的DLL檔案 解決方案主要可以參考這篇:http://support.microsoft.com/kb/837908 2.實作app.config方 ...

  2. 将 varchar 值转换为 JDBC 数据类型 DATE 时发生错误。

    问题是: 我是这样解决的  : 网上的 转型方法 并不好使 ,我想了想 可能是由于返回值是String  我 就成功的解决错误了  ..下面是关于原理的讲解肯定方法不唯一   至于错误,的产生,这个肯 ...

  3. Spring dbcp连接池简单配置 示例

    一.配置db.properties属性文件 #database connection config connection.username=sa connection.password=sa conn ...

  4. OpenGL ES 2.0 卷绕和背面剪裁

    基本知识 背面剪裁是指渲染管线在对构成立体物体的三角形图元进行绘制时,仅当摄像机观察点位于三角形正面的情况下才绘制三角形. OpenGL ES中规定若三角形中的3个顶点的卷绕顺序是逆时针则摄像机观察其 ...

  5. POJ 1001 Exponentiation 模拟小数幂

    模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...

  6. C/C++:Unions 联合

    原文:http://msdn.microsoft.com/en-us/library/5dxy4b7b(v=vs.80).aspx 联合是用户定义的数据或类类型,在任何时间里,它只包含成员列表中的一个 ...

  7. web app之rem

    rem是什么? rem:font size of the root element,是指相对于根元素的字体大小的单位.简单的说它就是一个相对单位. em:font size of the elemen ...

  8. talos项目记录

    1. 跑schedule : php src/cli-schedule/cli.php -a sales.coupon.offer-coupons  所用方法在schedule里

  9. 将图片以Blob格式存入数据库,再通过Servlet显示到界面

    1:为了方便测试,直接将1.png图片存入到数据库中. public static void storePicBlog() throws FileNotFoundException, SQLExcep ...

  10. Oracle BigFile

    http://blog.chinaunix.net/uid-20779720-id-3078273.html