Android Fragment(三)ListFragment简单介绍以及Fragment之间通信
一、Fragment通信简单介绍:Fragments之间是不能够直接通信的,他们之间的通信是通过Activity这个中间件来通信的, 为了让Fragment跟它的Activity通信,我们可以在Fragment类中定义一个接口,并在它所属的Activity中实现该接口。Fragment在它的onAttach()方法执行期间捕获该接口的实现,然后就可以调用接口方法,以便跟Activity通信。
二、需求:利用ListFragment实现一个浮动的二级菜单,点击左边菜单在右边显示与之对应的二级菜单列表。效果如下图所示:
废话不多说了,直接给大家上代码:
一、ListFragmentLeft(左边的ListFragment菜单)
package com.yw.myapiupdate.fragment; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast; import com.yw.myapiupdate.R; @SuppressLint("NewApi")
public class ListFragmentLeft extends ListFragment{
private LeftFragmentCallback callback;
/* String[] lists = new String[]{
"贾克斯",
"无双剑姬",
"蕾欧娜",
"安妮",
"潘森",
"盖伦"
};*/ @Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
callback = (LeftFragmentCallback)activity;
}catch(Exception e){ }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.listfragmentleft_layout, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,TestData.getInstance().getData(-1)));
} @Override
public void onListItemClick(ListView l, View v, int position, long id) {
callback.leftCallback(position);
/*Toast.makeText(getActivity(),
"You have selected:\t " + lists[position],
Toast.LENGTH_SHORT).show(); */
}
/**
* 回调接口
* @author yw-tony
*
*/
public interface LeftFragmentCallback{
public void leftCallback(int position);
}
}
与之对应的布局文件:
<?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:background="#33ff00"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/> </LinearLayout>
二、点击左边菜单时弹出的右边菜单类(ListFragmentRight)
package com.yw.myapiupdate.fragment; import com.yw.myapiupdate.R; import android.annotation.SuppressLint;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast; @SuppressLint("NewApi")
public class ListFragmentRight extends ListFragment{
/* String[] listRight = new String[]{
"天启者",
"赵信",
"嘉文四世",
"稻草人",
"瑞文",
"船长",
"盲僧"
};*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.listfragment_right, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,TestData.getInstance().getData(1)));
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
((MyFragmentActivity)getActivity()).windowDiss();
/*Toast.makeText(getActivity(),
"You have selected:\t " + listRight[position],
Toast.LENGTH_SHORT).show(); */
}
}
与之对应的布局文件:
<?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:background="#ff66ff"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/> </LinearLayout>
三、以上两者之间的通信桥梁Activity(MyFragmentActivity)
package com.yw.myapiupdate.fragment; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.PopupWindow; import com.yw.myapiupdate.R;
import com.yw.myapiupdate.fragment.ListFragmentLeft.LeftFragmentCallback; @SuppressLint("NewApi")
public class MyFragmentActivity extends Activity implements
LeftFragmentCallback,OnClickListener{
/* String[] lists = new String[]{
"天启者",
"赵信",
"嘉文四世",
"稻草人",
"瑞文",
"船长",
"盲僧",
"那身男爵"
};*/
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestData.getInstance().setData();
setContentView(R.layout.listfragment_layout);
btn = (Button)findViewById(R.id.listfragment_btn);
btn.setOnClickListener(this);
/*Display display = getWindowManager().getDefaultDisplay();
if(display.getWidth() > display.getHeight()){
FragmentRed red = new FragmentRed();
getFragmentManager().beginTransaction().replace(R.id.fragment_linear_layout, red).commit();
}else{
FragmentYellow yellow = new FragmentYellow();
getFragmentManager().beginTransaction().replace(R.id.fragment_linear_layout, yellow).commit();
}*/
initMenu();
}
private LayoutInflater inflater;
private View view;
private PopupWindow window;
private void initMenu(){
inflater = LayoutInflater.from(this);
view = inflater.inflate(R.layout.popupwindow_layout, null);
window = new PopupWindow(view,WindowManager.LayoutParams.FILL_PARENT,WindowManager.LayoutParams.FILL_PARENT);
window.setWidth(WindowManager.LayoutParams.FILL_PARENT);
window.setHeight(WindowManager.LayoutParams.FILL_PARENT);
window.setFocusable(true);
window.setOutsideTouchable(true);
window.setBackgroundDrawable(new BitmapDrawable()); }
/**
* 显示窗体
* @param anchor
*/
public void show(View anchor){
if(window!=null && !window.isShowing()){
window.showAsDropDown(anchor);
}
}
/**
* 关闭窗体
*/
public void windowDiss(){
if(window!=null && window.isShowing()){
window.dismiss();
}
}
@Override
public void leftCallback(int position) {
try{
/*ListFragmentRight listRight = (ListFragmentRight)
getFragmentManager.findFragmentById(
R.id.listfragment_right);*/
FragmentManager manager = getFragmentManager();
ListFragmentRight listRight = (ListFragmentRight) manager.findFragmentById(R.id.listfragment_right);
if(listRight != null){
listRight.setListAdapter(new ArrayAdapter<String>(MyFragmentActivity.this,
android.R.layout.simple_list_item_1,TestData.getInstance().getData((position+1))));
}else{ }
}catch(Exception e){ } }
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.listfragment_btn:
if(window!=null && !window.isShowing()){
show(btn);
}
break;
} } }
与之对应的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
</LinearLayout>
四、提供静态数据源的工具类
package com.yw.myapiupdate.fragment; import java.util.ArrayList;
import java.util.List; public class TestData {
private List<String> listParent = new ArrayList<String>();
private List<String> listA = new ArrayList<String>();
private List<String> listB = new ArrayList<String>();
private List<String> listC = new ArrayList<String>();
private List<String> listD = new ArrayList<String>(); private TestData(){}
private static TestData instance;
public static TestData getInstance(){
if(instance == null){
synchronized (TestData.class) {
if(instance == null){
instance = new TestData();
}
}
}
return instance;
}
/**
* 获取数据
* @param target
* @return
*/
public List<String> getData(int target){
// List<String> lists = new ArrayList<String>();
switch(target){
case 1:
return listA;
// break;
case 2:
return listB;
// break;
case 3:
return listC;
// break;
case 4:
return listD;
// break;
}
return listParent;
}
public void setData(){
listParent.add("A");
listParent.add("B");
listParent.add("C");
listParent.add("D"); listA.add("A1");
listA.add("A2");
listA.add("A3"); listB.add("B1");
listB.add("B2");
listB.add("B3");
listB.add("B4"); listC.add("C1");
listC.add("C2"); listD.add("D1");
listD.add("D2");
listD.add("D3");
listD.add("D4");
listD.add("D5");
} }
程序运行后的效果图:
好了,ListFragment以及他们之间的通信到此就结束了,你?弄明白了吗?
Android Fragment(三)ListFragment简单介绍以及Fragment之间通信的更多相关文章
- Fragment的使用简单介绍【Android】
Fragment在实际项目开发中使用的越来越多,如今简介一下 布局文件: <LinearLayout xmlns:android="http://schemas.android.com ...
- Android 懒加载简单介绍
1.懒加载介绍 1.1.效果预览 1.2.效果讲解 当页面可见的时候,才加载当前页面. 没有打开的页面,就不会预加载. 说白了,懒加载就是可见的时候才去请求数据. 1.3.懒加载文章传送门 参考文章: ...
- NDK中android.mk文件的简单介绍和第三方库的调用
先贴一个样例,然后解释一下: LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := mydjvuapi SRC_FILE_ ...
- 开源项目Universal Image Loader for Android 说明文档 (1) 简单介绍
When developing applications for Android, one often facesthe problem of displaying some graphical ...
- Android Studio使用心得 - 简单介绍与环境配置
FBI Warning:欢迎转载,但请标明出处:http://blog.csdn.net/codezjx/article/details/38544823,未经本人允许请勿用于商业用途.感谢支持! 关 ...
- Android线程---UI线程和非UI线程之间通信
近期自学到了线程这一块,用了一上午的时间终于搞出来了主.子线程间的相互通信.当主线程sendMessage后,子线程便会调用handleMessage来获取你所发送的Message.我的主线程 ...
- ANDROID培训准备资料之四大组件的简单介绍
Android四大组件是一个android app 最基本的组成部分,这篇博客主要给大家简单的介绍一下四种组件 (1)Activities (2)Services (3)BroadcastReceiv ...
- 介绍开源的.net通信框架NetworkComms框架 源码分析
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 售价249英镑 我曾经花了 ...
- Android中Fragment的简单介绍
Android是在Android 3.0 (API level 11)引入了Fragment的,中文翻译是片段或者成为碎片(个人理解),可以把Fragment当成Activity中的模块,这个模块有自 ...
随机推荐
- mybaits模糊查询使用<bind>标签
<select id="selectBlogsLike" resultType="Blog"> <bind name="patter ...
- CentOS 7环境下Pycharm安装流程记录
1.准备安装文件: 方法1: 使用内置火狐浏览器访问下载最新格式为tar.gz的压缩包 网址:https://www.jetbrains.com/pycharm/download/previous.h ...
- Unix环境高级编程:文件 IO 原子性 与 状态 共享
参考 UnixUnix环境高级编程 第三章 文件IO 偏移共享 单进程单文件描述符 在只有一个进程时,打开一个文件,对该文件描述符进行写入操作后,后续的写入操作会在原来偏移的基础上进行,这样就可以实现 ...
- APP中内嵌H5页面为什么不能下载?
在APP中内嵌H5页面,若页面上存在下载链接,没有任何反应,为什么呢? 原因是app中内嵌的H5页面是WebView解析的,什么是WebView呢? 在Android手机中内置了一款高性能webkit ...
- 分页插件 jquery.pagination.js
引用 <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script> <lin ...
- 关于CSS和JS中用到的各种Height和Width的问题
自己记不住,列一下关于CSS和JS中用到的各类有关Height和Width属性的介绍对比. 所属类别 属性名 意义 其他 浏览器模型 Screen.height 浏览器窗口所在的屏幕的高度(单位像素) ...
- 对WebSocket技术的学习与探索(二)
近日重新开始学习WebSocket技术,什么是WebSocket,在<对WebSocket技术的学习与探索(一)>文章中已经说明白了,还没理解可以看看这篇文章http://www.ruan ...
- 使用anaconda安装pytorch的清华镜像地址
1.安装anaconda:国内镜像网址:https://mirror.tuna.tsinghua.edu.cn/help/anaconda/下载对应系统对应python版本的anaconda版本(Li ...
- 【SPL标准库专题(8)】 Datastructures:SplFixedArray
SplFixedArray主要是处理数组相关的主要功能,与普通php array不同的是,它是固定长度的,且以数字为键名的数组,优势就是比普通的数组处理更快. 类摘要 SplFixedArray im ...
- Oracle EBS 用户职责人员取值
SELECT fu.user_name 用户名, fu.description 用户说明, fu.start_date 用户启用日期, fu.end_date 用户终止日期 --,fu.employe ...