全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Android ViewPager与radiogroup实现关联示例

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.当页面切换后,还要把当前的导航栏变为绿色

  • 设置文本颜色的setTextColor(getResources().getColor(R.color.tvGreen));
  • 设置文本的上方的图片的,四个参数分别为,左、上、右、下setCompoundDrawablesWithIntrinsicBounds (null,getResources().getDrawable)(R.drawable.call_t),null,null);

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, 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, 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, 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, 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, null);
    msgBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.call_f), null, null);
    showBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.show_f), null, null);
    myBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.my), null, 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, getResources().getDrawable(R.drawable.mess_t), null, null);
        break;
      case 1:
        msgBtn.setTextColor(getResources().getColor(R.color.tvGreen));
        msgBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.call_t), null, null);
        break;
      case 2:
        showBtn.setTextColor(getResources().getColor(R.color.tvGreen));
        showBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.show_t), null, null);
        break;
      case 3:
        myBtn.setTextColor(getResources().getColor(R.color.tvGreen));
        myBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.my_t), null, null);
        break;
    }

  }

  //手指放上去,松开,拖动都会被调用
  @Override
  public void onPageScrollStateChanged(int state) {

  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# ViewPager  # radiogroup  # radiogroup关联  # Android控件系列之RadioButton与RadioGroup使用方法  # android RadioGroup的使用方法  # android自定义RadioGroup可以添加多种布局的实现方法  # Android程序开发中单选按钮(RadioGroup)的使用详解  # Android RadioGroup和RadioButton控件简单用法示例  # Android RadioGroup 设置某一个选中或者不可选中的方法  # Android编程开发之RadioGroup用法实例  # 让Android中RadioGroup不显示在输入法上面的办法  # Android编程单选项框RadioGroup综合应用示例  # Android开发之RadioGroup的简单使用与监听示例  # 加载  # 要把  # 分别为  # 滑出  # 这是  # 当你  # 并在  # 拖动  # 后会  # 大家多多  # 过程中  # 新建一个  # 包中  # 只写  # http  # LinearLayout  # xmlns  # tools  # layout_width  # match_parent 


相关文章: 如何选择高效稳定的ISP建站解决方案?  如何在搬瓦工VPS快速搭建网站?  如何使用Golang table-driven基准测试_多组数据测量函数效率  小程序网站制作需要准备什么资料,如何制作小程序?  C++时间戳转换成日期时间的步骤和示例代码  如何在IIS中新建站点并配置端口与物理路径?  中山网站制作网页,中山新生登记系统登记流程?  北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?  详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)  如何访问已购建站主机并解决登录问题?  Python多线程使用规范_线程安全解析【教程】  网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?  c++怎么用jemalloc c++替换默认内存分配器【性能】  重庆市网站制作公司,重庆招聘网站哪个好?  音乐网站服务器如何优化API响应速度?  全景视频制作网站有哪些,全景图怎么做成网页?  C++中的Pimpl idiom是什么,有什么好处?(隐藏实现)  建站主机选哪种环境更利于SEO优化?  极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?  香港网站服务器数量如何影响SEO优化效果?  学校免费自助建站系统:智能生成+拖拽设计+多端适配  山东网站制作公司有哪些,山东大源集团官网?  道歉网站制作流程,世纪佳缘致歉小吴事件,相亲网站身份信息伪造该如何稽查?  香港服务器网站卡顿?如何解决网络延迟与负载问题?  如何快速搭建支持数据库操作的智能建站平台?  如何高效利用亚马逊云主机搭建企业网站?  大学网站设计制作软件有哪些,如何将网站制作成自己app?  洛阳网站制作公司有哪些,洛阳的招聘网站都有哪些?  如何有效防御Web建站篡改攻击?  如何通过建站之星自助学习解决操作问题?  如何通过二级域名建站提升品牌影响力?  如何配置WinSCP新建站点的密钥验证步骤?  制作网页的网站有哪些,电脑上怎么做网页?  外贸公司网站制作哪家好,maersk船公司官网?  建站之星手机一键生成:多端自适应+小程序开发快速建站指南  ,柠檬视频怎样兑换vip?  建站之星在线客服如何快速接入解答?  高防服务器:AI智能防御DDoS攻击与数据安全保障  湖南网站制作公司,湖南上善若水科技有限公司做什么的?  深圳 网站制作,深圳招聘网站哪个比较好一点啊?  建站之星如何快速解决建站难题?  建站之星导航如何优化提升用户体验?  如何挑选优质建站一级代理提升网站排名?  小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化  如何在万网开始建站?分步指南解析  广州顶尖建站服务:企业官网建设与SEO优化一体化方案  制作网站的软件免费下载,免费制作app哪个平台好?  如何用美橙互联一键搭建多站合一网站?  北京企业网站设计制作公司,北京铁路集团官方网站?  如何在阿里云虚拟主机上快速搭建个人网站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。