fragment(防平板联系人界面,设置界面)、ViewPager+Fragment(防微信界面切换)
2017-06-17 05:09
369 查看
fragment
实现防联系人效果
实现步骤首先,建一个用来装载fragment的xml,和一个加载xml的类,建立二个fragment,必须设定id否者报错,还有name=“被加载的碎片类” 这个不能少,fragment只是用来展示次类的数据
建立放入左边fragment内的xml及它的JAVA类,此类继承Fragment(推荐使用android.support.v4.app.Fragment 内的,向下兼容)
重复上步操作
实现代码
activity_main.xml 用来承载fragment 内的数据
<?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.example.viewpagerfragment.MainActivity" android:orientation="horizontal" > <!--使用Fragment--> <fragment android:layout_width="150dp" android:layout_height="match_parent" android:id="@+id/fr_main_left" android:name="com.example.viewpagerfragment.Left_Fragment" > </fragment> <fragment android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/fr_mian_right" android:layout_weight="1" android:name="com.example.viewpagerfragment.Right_Fragment" > </fragment> </LinearLayout>
建立JAVA类,加载 activity_main.xml 即可
建立左边fragment 内的JAVA文件及xml,左边使用Listview展示数据
xml fragment_left.xml
<?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="wrap_content" android:id="@+id/lv_left_leftOne" /> </LinearLayout>
JAVA 类
package com.example.viewpagerfragment; 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; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; /** * Created by Administrator on 2017/6/17. */ public class Left_Fragment extends Fragment{ private String[] dataDatum; private String[] linkmanContacts; @Nullable @Override //重写创建View 的方法,把 fragment_left.xml 转化成view public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //取到xml文件,转成view View view=inflater.inflate(R.layout.fragment_left,null); //模拟数据 linkmanContacts = new String[]{"曾维","傅钱","闫立光","傅楠","二狗子","胡斐"}; dataDatum = new String[]{"男,二十三,未婚,求解脱!!","男,二十二","男,二十二","男,二十三,未婚,求解脱!!","男,二十二","男,二十二"}; //取到Listview ListView listView= (ListView) view.findViewById(R.id.lv_left_leftOne); //建立适配器 此时的上下文是要取的是fragment的,而不是自己的,因为本类已经是通过fragment显示了 ArrayAdapter arrayAdapter=new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1, linkmanContacts); //给组件设定适配器 listView.setAdapter(arrayAdapter); //给ListView 设定点击事件,点击右边的fragment 更换内容 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> 4000 parent, View view, int position, long id) { //取fragment 右边的 textview组件,兄弟组件,直接取拿不到,从能透过父(就是fragment的那个xml的Activity) TextView textView= (TextView) getActivity().findViewById(R.id.tv_right_one); textView.setText(linkmanContacts[position]+"\n"+dataDatum[position]); } }); return view; } }
fragment 右侧xml及JAVA
XML fragment_rigth.xml
<?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="match_parent" android:id="@+id/tv_right_one" /> </LinearLayout>
JAVA
package com.example.viewpagerfragment; 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 Administrator on 2017/6/17. */ public class Right_Fragment extends Fragment{ //继承于碎片类 推荐使用此包的,向下兼容android.support.v4.app.Fragment; @Nullable @Override //重写创建View 的方法,把 fragment_left.xml 转化成view public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //取到xml文件,转成view View view=inflater.inflate(R.layout.fragment_right,null); return view; } }
fragment防设置页面
步骤其他部分和防联系人一样,而这个是每个界面样式都不同,每次更换不同的fragment即可
package com.example.viewpagerfragment; 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; import android.widget.Toast; /** * Created by Administrator on 2017/6/17. */ public class Left_Fragment extends Fragment{ private String[] dataDatum; private String[] linkmanContacts; private TextView textView; @Nullable @Override //重写创建View 的方法,把 fragment_left.xml 转化成view public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //取到xml文件,转成view View view=inflater.inflate(R.layout.fragment_left,null); //模拟数据 linkmanContacts = new String[]{"曾维","傅钱","闫立光","傅楠","二狗子","胡斐"}; dataDatum = new String[]{"男,二十三,未婚,求解脱!!","男,二十二","男,二十二","男,二十三,未婚,求解脱!!","男,二十二","男,二十二"}; //取到Listview ListView listView= (ListView) view.findViewById(R.id.lv_left_leftOne); //建立适配器 此时的上下文是要取的是fragment的,而不是自己的,因为本类已经是通过fragment显示了 ArrayAdapter arrayAdapter=new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1, linkmanContacts); //给组件设定适配器 listView.setAdapter(arrayAdapter); //给ListView 设定点击事件,点击右边的fragment 更换内容 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //取fragment 右边的 textview组件,兄弟组件,直接取拿不到,从能透过父(就是fragment的那个xml的Activity) //取到碎片管理器 FragmentManager fragmentManager= getFragmentManager(); //从碎片管理器中,取到事务 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Toast.makeText(getContext(),""+position, Toast.LENGTH_SHORT).show(); switch (position){ case 0: //当选择的是第一个时候,通过事务对象,把主页面右边 fragment 替换成 你想要的 Fragment ZhuangFragment zhaung=new ZhuangFragment(); fragmentTransaction.replace(R.id.fr_mian_right,zhaung); break; case 1: fragmentTransaction.replace(R.id.fr_mian_right,new XuFragment()); break; case 2: fragmentTransaction.replace(R.id.fr_mian_right,new WangFragment()); break; case 3: //防联系人 textView = (TextView) getActivity().findViewById(R.id.tv_right_one); textView.setText(linkmanContacts[position]+"\n"+dataDatum[position]); break; case 4: //防联系人 textView= (TextView) getActivity().findViewById(R.id.tv_right_one); textView.setText(linkmanContacts[position]+"\n"+dataDatum[position]); break; case 5: //防联系人 textView= (TextView) getActivity().findViewById(R.id.tv_right_one); textView.setText(linkmanContacts[position]+"\n"+dataDatum[position]); break; case 6: //防联系人 textView= (TextView) getActivity().findViewById(R.id.tv_right_one); textView.setText(linkmanContacts[position]+"\n"+dataDatum[position]); break; } //提交事务 fragmentTransaction.commit(); } }); return view; } }
ViewPager+Fragment(防微信界面切换)
步骤在主xml中建立ViewPager,然后在建立一个Activity
再建立,其他要被滑动的fragment然后加入Activity ,中fragment集合
代码
其他fragmnet的xml和java代码我就不贴了,因为和上面一样的
主xml
activity_view_pager__fragemt.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" android:orientation="vertical" tools:context="com.example.viewpagerfragment.ViewPager_Fragemt"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="550dp" android:id="@+id/vp_fragment_one" > </android.support.v4.view.ViewPager> <RadioGroup android:gravity="top" android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/rg_fragment_group" android:orientation="horizontal" > <RadioButton android:layout_gravity="clip_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_fragment_weixin" android:text="微信" android:button="@null" android:layout_weight="1" android:gravity="center" android:drawableTop="@drawable/weixin_radio" /> <RadioButton android:layout_gravity="clip_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_fragment_tongxunlv" android:layout_weight="1" android:text="通讯录" android:button="@null" android:gravity="center" android:drawableTop="@drawable/txl_radio" /> <RadioButton android:layout_gravity="clip_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_fragment_faxian" android:layout_weight="1" android:text="发现" android:button="@null" android:gravity="center" android:drawableTop="@drawable/fx_radio" /> <RadioButton android:layout_gravity="clip_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_fragment_wo" android:layout_weight="1" android:text="我" android:button="@null" android:gravity="center" android:drawableTop="@drawable/i_radio" /> </RadioGroup> </LinearLayout>
JAVA代码
package com.example.viewpagerfragment; import android.support.annotation.IdRes; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RadioGroup; import android.widget.Toast; import java.util.LinkedList; import java.util.List; public class ViewPager_Fragemt extends AppCompatActivity { private List<Fragment> fragmentList; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_pager__fragemt); //建立fragment 集合,存入碎片,就是要被切换的fragment fragmentList = new LinkedList<Fragment>(); fragmentList.add(new WeixinFragment()); fragmentList.add(new TxlFragment()); fragmentList.add(new FxFragment()); fragmentList.add(new IwodeFragment()); //取到 ViewPager viewPager = (ViewPager) findViewById(R.id.vp_fragment_one); //建立重写的 碎片适配器 参数为,碎片管理器 Myadapter myadapter=new Myadapter(getSupportFragmentManager()); //设定 适配器 viewPager.setAdapter(myadapter); //取到单选组 RadioGroup radioGroup= (RadioGroup) findViewById(R.id.rg_fragment_group); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { //取到单选组中,此view所在的下标 group.indexOfChild(group.findViewById(checkedId)) //设定视图页对象,选中页数,参数填写多少,就选中为多少页 viewPager.setCurrentItem(下标); viewPager.setCurrentItem(group.indexOfChild(group.findViewById(checkedId))); } }); } class Myadapter extends FragmentPagerAdapter{ //重写,特有的碎片页面适配器 public Myadapter(FragmentManager fm) { //返回碎片管理器 super(fm); } @Override public Fragment getItem(int position) {//去当前下标的碎片 return fragmentList.get(position); } @Override public int getCount() { //取碎片个数 return fragmentList.size(); } } }
效果图
相关文章推荐
- 微信切换界面延时加载的实现方法是优化ViewPager中加载Fragment
- Android Studio使用ViewPager+Fragment实现仿微信滑动切换界面
- Fragment+ViewPager实现仿微信点击和滑动切换界面
- ViewPager中切换界面Fragment被销毁解析
- 【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题
- 【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁
- android viewpager fragment切换时界面卡顿解决办法
- Android 安卓 fragment+viewpager 仿qq界面 实现点击菜单切换界面+滑动切换viewpager切换界面
- Android仿微信界面--使用FragmentPagerAdapter,viewpager实现(慕课网笔记)
- ViewPager中切换界面Fragment被销毁的问题分析
- ViewPager+Fragment打造滑动切换界面
- Android ViewPager+Fragment超高仿微信主界面(带底部图标切换动画)
- 【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析
- ViewPager中切换界面Fragment被销毁的问题
- 转-Fragment+ViewPager组件(高仿微信界面)
- ViewPager中切换界面Fragment被销毁的问题分析ViewPager中切换界面Fragment被销毁的问题分析
- Fragment和ViewPager的结合——仿制微信界面6.0<二>
- 实现Fragment的切换和ViewPager自动循环设置切换时间
- 仿QQ界面切换简单实现-ViewPager、Fragment
- ViewPager中切换界面Fragment被销毁的问题分析