怎么在ViewPage中使用Fragment模拟一个微信界面
这期内容当中小编将会给大家带来有关怎么在ViewPage中使用Fragment模拟一个微信界面,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于做网站、成都做网站、崆峒网络推广、成都小程序开发、崆峒网络营销、崆峒企业策划、崆峒品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供崆峒建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
界面与碎片:
主界面:
在drawable中创建四个选择器
button_selector.xml
button2_selector.xml
button3_selector.xml
button4_selector.xml
四个碎片:
fragment_weixin.xml
fragment_contact.xml
fragment_find.xml
fragment_mine.xml
Java代码
主Activity:
package com.example.g160628_android10_viewpagerfragment_zuoye; import android.content.res.Resources; 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.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Listfragments=new ArrayList<>(); private ViewPager vp_main_view; private RadioGroup rg_main_group; private List views; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //把碎片加入到碎片集合中 fragments.add(new WeiXinFragment()); fragments.add(new ContactFragment()); fragments.add(new FindFragment()); fragments.add(new MineFragment()); //找到自己的ViewPager vp_main_view = (ViewPager) findViewById(R.id.vp_main_view); vp_main_view.setAdapter(new MyAdapter(getSupportFragmentManager())); //设置当前的碎片为1 vp_main_view.setCurrentItem(1); //获得单选按钮组 rg_main_group = (RadioGroup) findViewById(R.id.rg_main_group); //设置单选按钮组的选择事件 rg_main_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { Resources res=MainActivity.this.getResources(); switch (checkedId) { case R.id.rb_main_bu1: vp_main_view.setCurrentItem(0); break; case R.id.rb_main_bu2: vp_main_view.setCurrentItem(1); break; case R.id.rb_main_bu3: vp_main_view.setCurrentItem(2); break; case R.id.rb_main_bu4: vp_main_view.setCurrentItem(3); break; } } }); //获得所有的单选框 views=rg_main_group.getTouchables(); //设置单选框事件 vp_main_view.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //设置选中 RadioButton button= (RadioButton) views.get(position); button.setChecked(true); } @Override public void onPageScrollStateChanged(int state) { } }); } //定义属于自己的适配器 class MyAdapter extends FragmentPagerAdapter{ public MyAdapter(FragmentManager fm) { super(fm); } //获得碎片的所有 @Override public Fragment getItem(int position) { return fragments.get(position); } //返回碎片的长度 @Override public int getCount() { return fragments.size(); } } }
四个碎片对应的Fragment
WeiXinFragment
package com.example.g160628_android10_viewpagerfragment_zuoye; 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/15. */ public class WeiXinFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //返回对应的fragment_weixin return inflater.inflate(R.layout.fragment_weixin,null); } }
ContactFragment
package com.example.g160628_android10_viewpagerfragment_zuoye; 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/15. */ public class ContactFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //返回对应的fragment_contact return inflater.inflate(R.layout.fragment_contact,null); } }
FindFragment
package com.example.g160628_android10_viewpagerfragment_zuoye; 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/15. */ public class FindFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //返回对应的fragment_find return inflater.inflate(R.layout.fragment_find,null); } }
MineFragment
package com.example.g160628_android10_viewpagerfragment_zuoye; 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/15. */ public class MineFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //返回对应的fragment_mine return inflater.inflate(R.layout.fragment_mine,null); } }
需要的图片
small_weixin small_contact small_find small_mine
上述就是小编为大家分享的怎么在ViewPage中使用Fragment模拟一个微信界面了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
本文题目:怎么在ViewPage中使用Fragment模拟一个微信界面
浏览路径:http://scyanting.com/article/gicpco.html