Android高级控件系列八之碎片
2017-06-15 12:17
162 查看
效果图:
代码实例:
xml布局代码:
总:
分支:
Java代码:
代码实例:
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; } }
相关文章推荐
- Android高级控件系列四之RecyclerView的使用
- Android高级控件系列之ListView的用法
- Android高级控件系列六之ViewPager的使用
- Android高级控件系列二之第三方控件PullToRefreshListView下拉刷新的使用
- Android高级控件系列五之Gallery的使用(已过时)
- Android系列之浅谈AndroidGallery控件使用方法详解
- Android--从零单排系列(2)--常用控件
- Android高级控件(三)
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android高级_视频播放控件,android播放控件
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android React-Native系列之<二>零基础学习React-Native控件之View
- android高级控件-Fragment
- Day4.2--Android高级UI控件之AutoCompleteTextView的使用
- Android 学习笔记10:高级控件(二)
- android控件之Fragment(碎片)
- Android高级图片滚动控件实现3D版图片轮播器
- Android高级控件(五)——如何打造一个企业级应用对话列表,以QQ,微信为例
- Android高级图片滚动控件,编写3D版的图片轮播器
- [Android]Android高级UI开发系列教程(一) - Android拖拽教程