AndroID VIEwPager与radiogroup实现关联
效果图展示
AndroID VIEwPager与radiogroup实现关联步骤
1.实例化VIEwPager
2.通过LayoutInflater加载布局,返回VIEw结果
3.把生成的每一个VIEw对象添加到List集合中
4.实例化适配器,传递VIEw集合
5.在适配器中继承自PagerAdapter,实现内部的四个方法
getCount(); 返回视图的数量 isVIEwFromObject(); 是否通过对象加载视图 VIEw==object instantiateltem(); 加载当前页面(通过container.addVIEw();添加视图)返回个给用户 destroyItem(); 销毁滑出的视图(通过container.removerVIEw();销毁视图)6.实例化每个Radiobutton
7.点击每个RaIDobutton时,切换不同的页面(vIEwPager.setCurrentltem(下标))
8.当页面切换后,还要把当前的导航栏变为绿色
9.当你每次点击之前的时候,添加一个方法,清除方法,(清理之 前的所有导航栏的状态,置为灰色)
10.实现滑动监听需要addOnPagerchangelistener
11.在onPagerSelected方法中,根据position页面的下标判断分别设置对应的底部导航栏状态
代码演示
1.在主布局文件中引入androID-support-v4.jar包并添加RadioGroup并在RadioGroup中添加Radiobutton用于显示导航栏
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context="com.example.cxy.vIEwpager.MainActivity"> <androID.support.v4.vIEw.VIEwPager androID:ID="@+ID/vIEwPager" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="9"> </androID.support.v4.vIEw.VIEwPager> <RadioGroup androID:ID="@+ID/radioGroup" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:background="#F4F3F3" androID:orIEntation="horizontal"> <Radiobutton androID:ID="@+ID/radiobutton1" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:button="@null" androID:drawabletop="@drawable/mess_t" androID:gravity="center" androID:text="微信" androID:textcolor="#11CD6E"/> <Radiobutton androID:ID="@+ID/radiobutton2" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:button="@null" androID:drawabletop="@drawable/call_f" androID:gravity="center" androID:text="通讯录" androID:textcolor="@androID:color/darker_gray"/> <Radiobutton androID:ID="@+ID/radiobutton3" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:button="@null" androID:drawabletop="@drawable/show_f" androID:gravity="center" androID:text="发现" androID:textcolor="@androID:color/darker_gray"/> <Radiobutton androID:ID="@+ID/radiobutton4" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:button="@null" androID:drawabletop="@drawable/my" androID:gravity="center" androID:text="我" androID:textcolor="@androID:color/darker_gray"/> </RadioGroup></linearLayout>
2.VIEwPager需要适配器继承于PagerAdapter
package com.example.cxy.vIEwpager.adapter;import androID.support.v4.vIEw.PagerAdapter;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import java.util.List;/** * date:2017/3/7 * Created:陈箫阳(admin) */public class MyVIEwPagerAdpter extends PagerAdapter { private List<VIEw> mList; public MyVIEwPagerAdpter(List<VIEw> List) { mList = List; } //返回视图数量 @OverrIDe public int getCount() { return mList.size(); } //是否通过对象加载视图 @OverrIDe public boolean isVIEwFromObject(VIEw vIEw,Object object) { return vIEw == object; } //加载当前页面 @OverrIDe public Object instantiateItem(VIEwGroup container,int position) { container.addVIEw(mList.get(position)); return mList.get(position);//VIEw } //销毁滑出视图 @OverrIDe public voID destroyItem(VIEwGroup container,int position,Object object) { container.removeVIEw(mList.get(position)); }}
3.新建一个fragment包,在包中新建OneFragment类用于滑动展示,新建布局文件fragmentone.xml并添加TextVIEw用于添加不同页面的内容,共有四个这里只写一个
OneFragment类
package com.example.cxy.vIEwpager.fragment;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 com.example.cxy.vIEwpager.R;/** * date:2017/3/7 * Created:陈箫阳(admin) */public class OneFragment extends Fragment{ @Nullable @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,@Nullable VIEwGroup container,@Nullable Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.fragmentone,null); return vIEw; }}
fragmentone.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <TextVIEw androID:textSize="30sp" androID:ID="@+ID/textVIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:text="这是微信页面"/></linearLayout>
4.编写主类
package com.example.cxy.vIEwpager;import androID.os.Bundle;import androID.support.v4.vIEw.VIEwPager;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.Widget.Radiobutton;import androID.Widget.RadioGroup;import com.example.cxy.vIEwpager.adapter.MyVIEwPagerAdpter;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedchangelistener,VIEwPager.OnPagechangelistener { private VIEwPager mVIEwPager; private List<VIEw> mList; private RadioGroup mRadioGroup; private Radiobutton weChatBtn,msgBtn,showBtn,myBtn; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //初始化所有控件 initVIEw(); } private voID initVIEw() { //实例化VIEwPager mVIEwPager = (VIEwPager) findVIEwByID(R.ID.vIEwPager); //实例化Radiogroup mRadioGroup = (RadioGroup) findVIEwByID(R.ID.radioGroup); //给RadioGroup添加监听 mRadioGroup.setonCheckedchangelistener(this); //实例化Radiobutton weChatBtn = (Radiobutton) findVIEwByID(R.ID.radiobutton1); msgBtn = (Radiobutton) findVIEwByID(R.ID.radiobutton2); showBtn = (Radiobutton) findVIEwByID(R.ID.radiobutton3); myBtn = (Radiobutton) findVIEwByID(R.ID.radiobutton4); //实例化List数组 mList = new ArrayList<>(); VIEw vIEw1 = LayoutInflater.from(this).inflate(R.layout.fragmentone,null); VIEw vIEw2 = LayoutInflater.from(this).inflate(R.layout.fragmenttwo,null); VIEw vIEw3 = LayoutInflater.from(this).inflate(R.layout.fragmentthree,null); VIEw vIEw4 = LayoutInflater.from(this).inflate(R.layout.fragmentfour,null); //把生成的每一个VIEw对象添加到集合中 mList.add(vIEw1); mList.add(vIEw2); mList.add(vIEw3); mList.add(vIEw4); //实例化适配器 MyVIEwPagerAdpter adapter = new MyVIEwPagerAdpter(mList); //给VIEwPager添加适配器 mVIEwPager.setAdapter(adapter); //给VIEwPager添加监听事件 mVIEwPager.addOnPagechangelistener(this); } @OverrIDe public voID onCheckedChanged(RadioGroup group,int checkedID) { //清理所有导航栏的状态 clearState(); switch (checkedID) { case R.ID.radiobutton1: //给VIEwPager设置当前布局 mVIEwPager.setCurrentItem(0); //给Radiobutton设置文本颜色 weChatBtn.setTextcolor(getResources().getcolor(R.color.tvGreen)); //给Radiobutton设置文本上方的图片 weChatBtn.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.mess_t),null); break; case R.ID.radiobutton2: mVIEwPager.setCurrentItem(1); msgBtn.setTextcolor(getResources().getcolor(R.color.tvGreen)); msgBtn.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.call_t),null); break; case R.ID.radiobutton3: mVIEwPager.setCurrentItem(2); showBtn.setTextcolor(getResources().getcolor(R.color.tvGreen)); showBtn.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.show_t),null); break; case R.ID.radiobutton4: mVIEwPager.setCurrentItem(3); myBtn.setTextcolor(getResources().getcolor(R.color.tvGreen)); myBtn.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.my_t),null); break; } } //初始化底部导航栏 private voID clearState() { weChatBtn.setTextcolor(getResources().getcolor(androID.R.color.darker_gray)); msgBtn.setTextcolor(getResources().getcolor(androID.R.color.darker_gray)); showBtn.setTextcolor(getResources().getcolor(androID.R.color.darker_gray)); myBtn.setTextcolor(getResources().getcolor(androID.R.color.darker_gray)); weChatBtn.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.mess_f),null); msgBtn.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.call_f),null); showBtn.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.show_f),null); myBtn.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.my),null); } //滑动过程中的动作 @OverrIDe public voID onPageScrolled(int position,float positionOffset,int positionOffsetPixels) { } //选择某个页面松手后会被调用 @OverrIDe public voID onPageSelected(int position) { //清理所有导航栏的状态 clearState(); switch (position) { //使用Switch拿到下标定义当滑动到相应位置小点显示颜色 case 0: //当页面切换后,还要把当前的导航栏变为绿色 weChatBtn.setTextcolor(getResources().getcolor(R.color.tvGreen)); //设置文本的上方的图片的,四个参数分别为,左、上、右、下 weChatBtn.setCompoundDrawablesWithIntrinsicBounds(null,null); break; case 1: msgBtn.setTextcolor(getResources().getcolor(R.color.tvGreen)); msgBtn.setCompoundDrawablesWithIntrinsicBounds(null,null); break; case 2: showBtn.setTextcolor(getResources().getcolor(R.color.tvGreen)); showBtn.setCompoundDrawablesWithIntrinsicBounds(null,null); break; case 3: myBtn.setTextcolor(getResources().getcolor(R.color.tvGreen)); myBtn.setCompoundDrawablesWithIntrinsicBounds(null,null); break; } } //手指放上去,松开,拖动都会被调用 @OverrIDe public voID onPageScrollStateChanged(int state) { }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android ViewPager与radiogroup实现关联示例全部内容,希望文章能够帮你解决Android ViewPager与radiogroup实现关联示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)