Android Fragment之巩固
来源:互联网 发布:德客会员管理系统源码 编辑:程序博客网 时间:2024/06/08 05:38
在看完关于Fragment的资料之后,自己决定将视频中涉及到的几个小项目整合一下,终于花了一个多小时的时间,做出来了自己设计的东西。
大体的实现的思路就是将单程和往返这两个按钮放置在Activity布局文件中,两个Fragment控件在同一个LinearLayout布局中,然后整个的LinearLayout布局又处于Activity中,
通信的话采用了昨天说的第二种方式来通信,感觉比较简单快捷。由于只是点在昨天的代码已将讲的差不多了,在这里直接上代码:
MainActivity.java代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button singleButton; private Button circleButton; private boolean flag; private DetailFragment detailFragment; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); singleButton = (Button)findViewById(R.id.singlebutton); circleButton = (Button)findViewById(R.id.circlebutton); singleButton.setOnClickListener(this); circleButton.setOnClickListener(this); Fragment fragment=getSupportFragmentManager().findFragmentById(R.id.fragment_detail); if (fragment!=null && fragment instanceof DetailFragment) { detailFragment = new DetailFragment(); detailFragment =(DetailFragment)fragment; } } //完成了城市列表的传值操作 public void receiveCity(String cityName) { //在活动中获取Fragment Fragment fragment=getSupportFragmentManager().findFragmentById(R.id.fragment_detail); if(fragment!=null && fragment instanceof DetailFragment) { DetailFragment detailFragment=new DetailFragment(); detailFragment=(DetailFragment)fragment; detailFragment.showCity(cityName); } } @Override public void onClick(View v) { switch (v.getId()){ case R.id.singlebutton: flag=true; detailFragment.getFlag(flag); break; case R.id.circlebutton: flag=false; detailFragment.getFlag(flag); break; default: break; } }}CityFragment代码:
public class CityFragment extends Fragment implements AdapterView.OnItemClickListener { private List<String> data; public CityFragment() { } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.citylayout,container,false); ListView citylist=(ListView) view.findViewById(R.id.city_list); //构造适配器,进行数据的装配 data = new LinkedList<String>(); data.add("章丘"); data.add("济南"); data.add("明水"); data.add("绣惠"); data.add("北京"); data.add("天津"); data.add("上海"); data.add("南京"); //在Fragment中获取上下文,使用getActivity(),第二个参数是子项的布局形式,第三个参数是数据 ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, data); //将数据和子项的布局加载进去 citylist.setAdapter(adapter); citylist.setOnItemClickListener(this); return view; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String cityName=data.get(position); //为了避免程序崩掉,尽量每次都判断一下 if(cityName!=null) { //在Fragment中获取活动 FragmentActivity activity=getActivity(); //判断活动是否为空并且是不是MainActivity //FragmentActivity是继承自Activity,所以FragmentActivity的实例也就是MainActivity的实例 if(activity!=null && activity instanceof MainActivity) { //创建一个对象,将FragmentActivity的对象转换为MainActivity的对象 MainActivity transfer=(MainActivity) activity; //调用Activity的方法,将Franment的值传递过去 //receiveCity()的方法在这里开始调用 //一点击就会执行 transfer.receiveCity(cityName); } } }}
DetailFragment代码
public class DetailFragment extends Fragment { private TextView citytext; private TextView tickettext; private FragmentActivity activity; private MainActivity send; public DetailFragment() { } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.detaillayout,container,false); citytext=(TextView) view.findViewById(R.id.city_text); tickettext=(TextView) view.findViewById(R.id.ticket_text); activity = getActivity(); if(activity!=null && activity instanceof MainActivity) { send = (MainActivity)activity; } return view; } public void showCity(String cityName) { if(citytext!=null) { if(cityName!=null) { citytext.setText(cityName); } } } public void getFlag(boolean flag) { if(flag) { if(tickettext!=null) { tickettext.setText("这是单程车票"); } } else { tickettext.setText("这是往返车票"); } }}activity.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/singlebutton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="单程" android:textSize="25sp" android:gravity="center" /> <Button android:id="@+id/circlebutton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="往返" android:textSize="25sp" android:checked="true" android:gravity="center" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <fragment android:id="@+id/fragment_city" android:name="com.example.ticket.CityFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <fragment android:id="@+id/fragment_detail" android:name="com.example.ticket.DetailFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3"/> </LinearLayout></LinearLayout>citylayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/city_list" android:layout_width="match_parent" android:layout_height="match_parent"></ListView></LinearLayout>
detalilayout.xml
TextView android:id="@+id/city_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是城市的信息" android:textSize="40sp" android:textColor="#f00" android:gravity="center" /> <TextView android:id="@+id/ticket_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是车次的信息" android:textSize="40sp" android:textColor="#f00" android:gravity="center" />以上就是全部的项目代码,运行效果如下图:
0 0
- Android Fragment之巩固
- Android学习之分析代码&巩固基础
- Android学习之自己需要提高和巩固的知识
- android文档之Fragment
- Android昆仑镜之Fragment
- Android之Fragment
- android之Fragment
- Android学习之 Fragment
- android之Fragment
- android之Fragment
- Android之Fragment
- android之Fragment
- Android入门之Fragment
- Android之fragment初探
- Android之FragMent
- Android之Fragment通信
- Android之Fragment学习心得
- Android之Fragment优点
- Java-集合框架Collection之Set(一)
- Ashes of Time
- 柱状图表定义
- 我一直把html视频音频放在二级域名,现在放到目录竟发现很多新问题没解决?
- 论使用文件测试的坏处
- Android Fragment之巩固
- 饼图表定义
- Unity3d中使用OnGUI()函数判断“键盘按下抬起”功能的新方法。
- CentOS 6 安装在虚拟机上 eth0网卡无法工作的解决
- csu1673 集训队组队计划 (二分)
- C语言内嵌汇编(arm-v7)----加减乘移位
- 在O(logn)时间内找到数组中离每个数最近而又比它大的数的下标
- 按钮权限分配
- C++基本概念——聊聊C++中的函数匹配那些事儿