Android的ListFragment和Fragment的使用
工程目录:
CenterListFragment
package com.example.test1;import java.util.ArrayList;
import java.util.List;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;
import android.widget.Toast;public class CenterListFragment extends ListFragment {private ArrayAdapter<String> adapter;private List<String> data;private FragmentManager manager;private FragmentTransaction transaction;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);data = new ArrayList<String>();for (int i = 0; i < 11; i++) {data.add("data" + i);}manager = getFragmentManager();adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, data);setListAdapter(adapter);}@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id);String str = adapter.getItem(position);transaction = manager.beginTransaction();RightFragment rFragment = new RightFragment();Bundle bundle = new Bundle();bundle.putString("id", str);rFragment.setArguments(bundle);transaction.replace(R.id.right, rFragment, "detail");transaction.commit();Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();}
}
MainActivity
package com.example.test1;import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends Activity {private Button button;private FragmentManager manager;private FragmentTransaction transaction;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);manager = getFragmentManager();button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {transaction = manager.beginTransaction();CenterListFragment centerListFragment = new CenterListFragment();transaction.add(R.id.center, centerListFragment, "center");transaction.commit();}});}}
RightFragment
package com.example.test1;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 RightFragment extends Fragment {private TextView textView;private View view;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.detail, null);textView = (TextView) view.findViewById(R.id.textView);Bundle bundle = getArguments();String str = bundle.getString("id");textView.setText(str);return view;}}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"tools:context=".MainActivity" ><LinearLayoutandroid:id="@+id/left"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical" ><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="显示列表" /></LinearLayout><LinearLayoutandroid:id="@+id/center"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="2"android:orientation="vertical" ></LinearLayout><LinearLayoutandroid:id="@+id/right"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="2"android:orientation="vertical" ></LinearLayout></LinearLayout>
detail.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"tools:context=".MainActivity" ><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="30sp"/></LinearLayout>
演示:
Android的ListFragment和Fragment的使用相关推荐
- 【Android 应用开发】 Fragment 详解
作者 : 韩曙亮 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragmen ...
- Android Tab大总结 Fragment+TabPageIndicator+ViewPager
2019独角兽企业重金招聘Python工程师标准>>> 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24740 ...
- [Android] 使用Include布局+Fragment滑动切换屏幕
前面的文章已经讲述了"随手拍"项目图像处理的技术部分,该篇文章主要是主界面的布局及屏幕滑动切换,并结合鸿洋大神的视频和郭神的第一行代码(强推两人Android博客),完成了 ...
- android onattach方法,Java Fragment.onAttach方法代码示例
import android.support.v4.app.Fragment; //导入方法依赖的package包/类 private void testBindToLifecycle(Lifecyc ...
- android fragmentactivity fragment,Android:Activity与Fragment通信(99%)完美解决方案
前言 最近一直在想着能否有一种更好的方案来解决:Android中Activity与Fragment之间通信的问题,什么叫更好呢,就是能让Fragment的复用性高,性能还有好(不用反射),代码还要好维 ...
- android 动态生成fragment,Android动态加载fragment(fragment复用)
[实例简介] Android动态加载fragment(fragment复用) [实例截图] [核心代码] fm_reuse └── fm_reuse ├── AndroidManifest.xml ├ ...
- android中viewpager+fragment,Android开发之ViewPager+Fragment
使用步骤 1.Activity的布局文件 android:id="@+id/viewpager" android:layout_width="wrap_content&q ...
- 【Android开发】之Fragment与Acitvity通信
上一篇我们讲到与Fragment有关的常用函数,既然Fragment被称为是"小Activity",现在我们来讲一下Fragment如何与Acitivity通信.如果上一篇还有不懂 ...
- android mainactivity 刷新fragment,Android刷新从FragmentActivity android.support.v4.app.Fragment
我使用FragmentTabHost创建dinamically FragmentActivity.我想通过单击操作栏中的项目来刷新当前选项卡中托管的片段.Android刷新从FragmentActiv ...
最新文章
- 解决Mysql复制Relay log read failure 的问题
- MVC--Rzaor--例子
- Axure RP使用攻略--入门级(五)系统函数与变量
- python中怎么输入角度_在Python中更正两点之间的角度
- UA池和IP代理池使用
- Flask 中的蓝图 Blueprint
- mysql 数据删除 索引_MySQL操作之索引创建与删除
- 来自一个用户的体验-Alpha项目测试
- 在线json解析工具 在线js解析加密混合工具
- 【C#】通过Devcon.exe控制设备管理器中设备的启停
- oracle数据库基础笔试题,Oracle数据库入门笔试试题及参考答案
- 移动端h5不支持font-family里面的楷体、微软雅黑等字体
- 全国计算机三级网络技术题库南开,计算机三级网络技术上机部分(南开100题题库).doc...
- csdn 修改博客皮肤
- SQL的between....and 用法
- 什么样的视频算搬运的?
- 怎么找网图本人_如何通过一张照片找到一个人的位置?https://www.zhihu.com/zvideo/1312521748374917120...
- Flutter编程之BoxDecoration用法详解
- 安装nvm,配置nvm源,配置npm源
- 根据以下原理图使用VHDL语言编写完整程序。
热门文章
- 腾讯云2019年全年营收超170亿元,腾讯健康码累计访问量达80亿次
- 开辟 Dart 到 Native 的超级通道,饿了么跨平台的最佳实践
- 2684 亿背后的虚拟化技术:双 11 All on 神龙 | 问底中国 IT 技术演进
- 重磅揭晓阿里 AliOS Things 3.0 革命性创新!
- 为什么说没有物联网,就没有 AI ?
- Java 11 中 11 个不为人知的瑰宝
- 放弃 VSCode,再次选择使用十年之久的 Emacs
- 北漂的程序员们,这寒冬你是否“有枝可依”?
- linux用qt实现聊天,linux下的Qt开发双人聊天服务器编写
- html5hover鼠标悬停,不使用hover外部CSS样式实现hover鼠标悬停改变样式