Fragment的使用相关
使用Fragment时,需要继承Fragment或者Fragment的子类,
直接贴代码,展示一个例子:
LeftFragment.java:
left_fragment.xml
平板:
手机
RightFragment.java
right_fragment.xml
主程序:
activity_first_fragment.xml
DetailActivity.java
activity_detail.xml
fragment之间的数据交互
发送:
接受
使用Fragment时,需要继承Fragment或者Fragment的子类,
直接贴代码,展示一个例子:
LeftFragment.java:
import android.app.Fragment; import android.content.Intent; 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.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class LeftFragment extends Fragment implements OnItemClickListener { private String[] data = new String[] { "灵魂战车2", "变形金刚3:月黑之时", "敢死队2" }; private ListView listView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment, null); listView = (ListView) view.findViewById(R.id.listview_movie_list); listView.setOnItemClickListener(this); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( getActivity(), android.R.layout.simple_list_item_activated_1, data); listView.setAdapter(arrayAdapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); return view; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView textView = (TextView) getActivity().findViewById( R.id.textview_detail); String detail = "nihao"; if (textView == null) { Intent intent = new Intent(getActivity(), DetailActivity.class); intent.putExtra("detail", detail); startActivity(intent); } else { textView.setText(detail); } } }
left_fragment.xml
平板:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/titles" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="4" class="cn.eoe.first.fragment.LeftFragment" /> <fragment android:id="@+id/details" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" class="cn.eoe.first.fragment.RightFragment" /> </LinearLayout>
手机
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#FFF"> <ListView android:id="@+id/listview_movie_list" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
RightFragment.java
import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class RightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.right_fragment, null); return view; } }
right_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textview_detail" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
主程序:
import android.app.Activity; import android.os.Bundle; public class FirstFragmentActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first_fragment); } }
activity_first_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/titles" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="4" class="cn.eoe.first.fragment.LeftFragment" /> <fragment android:id="@+id/details" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" class="cn.eoe.first.fragment.RightFragment" /> </LinearLayout>
DetailActivity.java
import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class DetailActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); TextView detail = (TextView)findViewById(R.id.textview_detail); detail.setText(getIntent().getExtras().getString("detail")); } }
activity_detail.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/details" android:layout_width="match_parent" android:layout_height="match_parent" class="cn.eoe.first.fragment.RightFragment" /> </RelativeLayout>
fragment之间的数据交互
发送:
public void onClick_SendData(View view) { MyFragment fragment = new MyFragment(); Bundle bundle = new Bundle(); String textdd = text.getText().toString(); bundle.putString("name", textdd); fragment.setArguments(bundle); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); fragmentTransaction.add(R.id.fragment_container1, fragment, "fragment"); fragmentTransaction.commit(); Toast.makeText(this, "数据已成功传递.", Toast.LENGTH_LONG).show(); }
接受
String name = getFragmentManager().findFragmentByTag("fragment") .getArguments().getString("name");
- FirstFragment.zip (1.1 MB)
- 下载次数: 0
- FragmentArgument.zip (1.2 MB)
- 下载次数: 0
发表评论
-
2048源码(核心算法有,缺少几个anctionbar,以后补上)
2014-09-25 13:22 14592048游戏基本上有四部分组成, 1:主activity,包含 ... -
android动画效果
2014-09-24 18:06 1106前几天弄alertdialog和popupwindow的时候, ... -
AlertDialog和PopupWindow
2014-09-18 15:44 1862区别:AlertDialog是非阻塞式对话框:AlertDia ... -
基础篇--resources资源
2014-09-12 15:18 490最近一直在做java开发,偶尔敲点android代码,突然发现 ... -
多点触摸(图片缩放为例)
2014-09-01 17:22 625多点触摸的事件跟单点是大同小异的,上个图片缩放的代码,供大家参 ... -
Toast的多种样式(附带Notification)
2014-09-01 13:48 888Toast以前用的时候一直以为只有文字提示,偶然得知也有多种样 ... -
Android Adapter详解(2)
2014-08-15 14:05 10以前Adapter一直用的不是太好,经过长时间的浸淫,现在可以 ... -
BroadcastReceiver简介
2014-08-14 16:27 645BroadcastReceiver作为四大 ... -
关于Android的Service
2014-08-14 13:57 428说起来真是羞愧,以前手机经常开机的时候,不会有任何QQ消息通知 ... -
在开发过程中易出的错误
2014-08-13 16:53 3601:如果继承ListActivity,那么layout中必须有 ... -
多媒体的浅尝辄止
2014-08-12 15:57 502下面简单讲几种Android的多媒体技术,音频,视频,摄像头, ... -
Sqlite无脑使用
2014-08-11 14:56 857不会sqlite的人再也不用愁了,无脑使用,只要会粘贴复制就O ... -
android弹出框
2014-08-11 11:23 486不得不说,android自带的弹出框真心丑,而且还不好用,接下 ... -
android几种数据存储方式
2014-08-11 10:45 676android数据存储方式 1:SharedPreferen ... -
SQLiteOpenHelper和ContentProvider区别
2014-08-06 15:08 1399Android中操作数据库主要有两种方法:使用SQLiteOp ... -
xml文件解析SAX
2014-08-05 13:45 461xml文件解析:xml文件解析有四种方式, 1.DOM生成和解 ... -
Android不常用代码(1)
2014-07-31 18:07 495目录 1:Webview 2:js交互 1:Web ... -
系统窗口的调用
2014-07-31 15:46 435直接上代码吧,intent进行调用 @Override ... -
fragment简单实用及数据传递(2)
2014-07-31 15:13 2518FragmentTransaction 进行数据传递 imp ... -
ActionBar简介
2014-07-31 10:47 671Action bar是一个标识应用程序和用户位置的窗口功能,并 ...
相关推荐
这里简单描述了以下Activity使用Bundle向Fragment传递数据
Fragment数据传递详细示例
fragment与activity之间相互通信,一般有两种方法,接口方式和bundle方式
Android 笔记-Fragment 与 Activity之间传递数据,使用setArgument方法的例子
activity与fragment的传递和fragment与Fragment的传递
Java文件,Activity,ViewPager,Fragment和TabLayout数据传递
同一activity托管的两个fragment间的数据传递与返回
Activity与Activity、Activity与Fragment、Fragment与Fragment的数据交互
Android fragemnt向activity,activity向fragment,fragment 和frament之间传递数据,详解,能运行!
主要介绍了Android 两个Fragment之间的跳转和数据的传递实例详解的相关资料,这里说明实现的思路及实现方法,需要的朋友可以参考下
上一篇介绍了,Fragment从Activity获取数据。这篇介绍两个Fragment之间的如何获取数据。 MainActivity包含有两个Fragment,LeftFragment和RightFragment,如果Leftfragment想要获取RightFragment中的数据,那么首先...
Fragment与Activity之间的数据交换,大体上包括三种: 一、Fragment从Activity获取数据(本文章只介绍第一种); 二、Activity从Fragment获取...从Activity传递数据到两个Fragment中,Fragment获取数据后,展示出来。
本实例介绍了通过接口调用的方式进行Fragment之间的通信和参数传递,关键部分做了注释。希望给学习Android移动开发的朋友带来帮助。
简单实用的Fragment框架,适用于商城,影音,即时通讯等项目!经测试可用!下载直接运行,如有问题请联系扣扣2691608900!!!
1、fragment传递信息给Activity 此点是通过在fragment中定义接口与Activity共享数据。 2、Activity传递信息给fragment 此点主要是通过fragment的getArgument()和setArgument()两个函数传递bundle来传递。
Fragment 之间的切换,与Activity之间的数据传递,实现基本的功能
1、fragment传递信息给Activity 此点是通过在fragment中定义接口与Activity共享数据。 2、Activity传递信息给fragment 此点主要是通过fragment的getArgument()和setArgument()两个函数传递bundle来传递。