Android高级控件系列八之碎片
来源:互联网 发布:好运通软件 编辑:程序博客网 时间:2024/06/03 06:01
效果图:
代码实例:
xml布局代码:
总:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.zking.laci.android10.MainActivity" android:orientation="horizontal" > <fragment android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/fragment_left_main" android:name="com.zking.laci.android10.LeftFragment" ></fragment> <fragment android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:id="@+id/fragment_right_mian" android:name="com.zking.laci.android10.RightFragment" ></fragment> </LinearLayout>
分支:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是小白的布局" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是小黑的布局" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是小红的布局" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><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:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv_fragment_left" ></ListView></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_fragment_right" /></LinearLayout>
Java代码:
package com.zking.laci.android10;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Laci on 2017/6/15. */public class BaiFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_bai,null); }}
package com.zking.laci.android10;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Laci on 2017/6/15. */public class HeiFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_hei,null); }}
package com.zking.laci.android10;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Laci on 2017/6/15. */public class RedFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_red,null); }}
package com.zking.laci.android10;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
package com.zking.laci.android10;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Laci on 2017/6/15. */public class RightFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_right,null); return v; }}
package com.zking.laci.android10;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView;/** * Created by Laci on 2017/6/15. */public class LeftFragment extends Fragment{ private ListView lv; private String names[]={"小白","小红","小黑"}; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_left,null); lv = (ListView) v.findViewById(R.id.lv_fragment_left); ArrayAdapter aa=new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,names); lv.setAdapter(aa); //给左边的ListView设值事件 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //右边同样的布局 TextView tv= (TextView) getActivity().findViewById(R.id.tv_fragment_right); tv.setText(names[position]); //获取碎片的管理者 FragmentManager fm=getFragmentManager(); //获取事务 FragmentTransaction ft=fm.beginTransaction(); //右边不同布局 switch (position) { case 0: BaiFragment bf=new BaiFragment(); ft.replace(R.id.fragment_right_mian,bf); break; case 1: RedFragment rf=new RedFragment(); ft.replace(R.id.fragment_right_mian,rf); break; case 2: HeiFragment hf=new HeiFragment(); ft.replace(R.id.fragment_right_mian,hf); break; } ft.commit();} }); return v; }}
阅读全文
0 0
- Android高级控件系列八之碎片
- Android常用控件之碎片Fragment
- android控件之Fragment(碎片)
- Android高级控件系列之ListView的用法
- Android高级控件系列四之RecyclerView的使用
- Android高级控件系列六之ViewPager的使用
- 跟我学Android UI高级之十五碎片Fragment
- Android高级控件系列二之第三方控件PullToRefreshListView下拉刷新的使用
- Android高级控件系列三之第三方控件XListView下拉刷新实现代码
- android高级控件之Volley
- android高级控件之AutoCompleteTextView
- Android知识点之高级控件
- Android高级控件之ListView
- Android高级控件系列五之Gallery的使用(已过时)
- Android之Fragment(碎片)
- Android之Fragment 碎片
- android 问题汇总系列之八
- android 问题汇总系列之八
- C学习笔记
- Cartographer简介
- Python学习笔记:爬虫下载图片
- shell脚本命令代换之‘ ’(反单引号)和$()
- linux logo居中,小企鹅的数量问题
- Android高级控件系列八之碎片
- 欢迎使用CSDN-markdown编辑器
- PHP获取ip与ip所在城市
- test
- 解决android设备adb offline问题
- 花卉大识别之崭露头角
- ubuntu mount cifs
- Android样式开发之shape
- mysql 下载地址