ScrollView  的xml布局

  1. <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
    tools:context="com.example.bobo.gaojikongjianapplication.MainActivity">
    //ScorllView 竖屏滑动
    // <!---水平滚动-->
    <HorizontalScrollView
    //scorllciew 中只能嵌套一个布局方式,但是其嵌套的布局方式可以嵌套多种布局方式
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:scrollbars="none"//设置滚动时滚动条是否显示
    android:id="@+id/hs">
    <RadioGroup
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rg1"
    android:orientation="horizontal"> //按钮横屏显示
    <RadioButton
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="烟台"
    android:button="@null" //不显示按钮周边 只显示文字
    android:gravity="center"
    android:checked="true"
    android:id="@+id/rb1"
    android:textColor="@color/rdutton"/>
    <RadioButton
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="烟台"
    android:button="@null"
    android:gravity="center"
    android:id="@+id/rb2"
    android:textColor="@color/rdutton"/>
    <RadioButton
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="烟台"
    android:button="@null"
    android:gravity="center"
    android:id="@+id/rb3"
    android:textColor="@color/rdutton"/>
    <RadioButton
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="烟台"
    android:button="@null"
    android:gravity="center"
    android:id="@+id/rb4"
    android:textColor="@color/rdutton"/>
    <RadioButton
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="烟台"
    android:button="@null"
    android:gravity="center"
    android:id="@+id/rb5"
    android:textColor="@color/rdutton"/>
    <RadioButton
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="烟台"
    android:button="@null"
    android:gravity="center"
    android:id="@+id/rb6"
    android:textColor="@color/rdutton"/>
    <RadioButton
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="烟台"
    android:button="@null"
    android:gravity="center"
    android:id="@+id/rb7"
    android:textColor="@color/rdutton"/>
    <RadioButton
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="烟台"
    android:button="@null"
    android:gravity="center"
    android:id="@+id/rb8"
    android:textColor="@color/rdutton"/>
    <RadioButton
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="烟台"
    android:button="@null"
    android:gravity="center"
    android:id="@+id/rb9"
    android:textColor="@color/rdutton"/>
    </RadioGroup>
    </HorizontalScrollView>
    //Imageview xml布局方式
    <android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/vp1"
    android:background="#14d0da"//设置背景 让图片与背景对比更明显
    android:layout_below="@+id/hs">
    </android.support.v4.view.ViewPager>
    </RelativeLayout>
    布局界面如下
  1. Activity 书写
  1. public class MainActivity extends AppCompatActivity {
    private RadioGroup rg;
    private ViewPager vp;
    private List<ImageView> m;
    private MyViewPagerAdapter mp;
    private HorizontalScrollView hs;
    private RadioButton rb1,rb2,rb3,rb4,rb5,rb6,rb7,rb8,rb9;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rg=(RadioGroup)findViewById(R.id.rg1);
    rb1=(RadioButton)findViewById(R.id.rb1);
    rb2=(RadioButton)findViewById(R.id.rb2);
    rb3=(RadioButton)findViewById(R.id.rb3);
    rb4=(RadioButton)findViewById(R.id.rb4);
    rb5=(RadioButton)findViewById(R.id.rb5);
    rb6=(RadioButton)findViewById(R.id.rb6);
    rb7=(RadioButton)findViewById(R.id.rb7);
    rb8=(RadioButton)findViewById(R.id.rb8);
    rb9=(RadioButton)findViewById(R.id.rb9);
    vp=(ViewPager)findViewById(R.id.vp1);
    //hs=(HorizontalScrollView)findViewById(R.id.hs);
  2.  
  3. rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
    switch (checkedId) {
    case R.id.rb1:
    Toast.makeText(getBaseContext(), rb1.getText(), Toast.LENGTH_SHORT).show();
    vp.setCurrentItem(0);//当点击了按钮rb1时切换到第一张图片
    break;
    case R.id.rb2:
    Toast.makeText(getBaseContext(), rb2.getText(), Toast.LENGTH_SHORT).show();
    vp.setCurrentItem(1);
    break;
    case R.id.rb3:
    Toast.makeText(getBaseContext(), rb3.getText(), Toast.LENGTH_SHORT).show();
    vp.setCurrentItem(2);
    break;
    case R.id.rb4:
    Toast.makeText(getBaseContext(), rb4.getText(), Toast.LENGTH_SHORT).show();
    vp.setCurrentItem(3);
    break;
    case R.id.rb5:
    Toast.makeText(getBaseContext(), rb5.getText(), Toast.LENGTH_SHORT).show();
    vp.setCurrentItem(4);break;
    case R.id.rb6:
    Toast.makeText(getBaseContext(), rb6.getText(), Toast.LENGTH_SHORT).show();
    vp.setCurrentItem(5);
    break;
    case R.id.rb7:
    Toast.makeText(getBaseContext(), rb7.getText(), Toast.LENGTH_SHORT).show();
    vp.setCurrentItem(6);
    break;
    case R.id.rb8:
    Toast.makeText(getBaseContext(), rb8.getText(), Toast.LENGTH_SHORT).show();
    vp.setCurrentItem(7);break;
    case R.id.rb9:
    Toast.makeText(getBaseContext(), rb9.getText(), Toast.LENGTH_SHORT).show();
    vp.setCurrentItem(8);break;
    }
    }
    });
    vpInit();
    }
    public void vpInit(){
    //往viepager中存放图片
    m=new ArrayList<>();
    ImageView iv=new ImageView(this);
    iv.setImageResource(R.mipmap.ic_launcher);
    m.add(iv);
    iv=new ImageView(this);
    iv.setImageResource(R.mipmap.ic_launcher);
    m.add(iv);
    iv=new ImageView(this);
    iv.setImageResource(R.mipmap.ic_launcher);
    m.add(iv);
    iv=new ImageView(this);
    iv.setImageResource(R.mipmap.ic_launcher);
    m.add(iv);
    iv=new ImageView(this);
    iv.setImageResource(R.mipmap.ic_launcher);
    m.add(iv);
    iv=new ImageView(this);
    iv.setImageResource(R.mipmap.ic_launcher);
    m.add(iv);
    iv=new ImageView(this);
    iv.setImageResource(R.mipmap.ic_launcher);
    m.add(iv);
    iv=new ImageView(this);
    iv.setImageResource(R.mipmap.ic_launcher);
    m.add(iv);
    iv=new ImageView(this);
    iv.setImageResource(R.mipmap.ic_launcher);
    m.add(iv);
    //mp=new MyViewPagerAdapter(m);
    vp.setAdapter(new MyViewPagerAdapter(m));//适配器与图片关联起来
    vp.setCurrentItem(0);//默认选中0
    vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    //该类方法实现当切换图片时,按钮同时切换
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  4.  
  5. }
  6.  
  7. @Override
    public void onPageSelected(int position) {
    RadioButton radioButton= (RadioButton) rg.getChildAt(position);
    radioButton.setChecked(true);
    }
  8.  
  9. @Override
    public void onPageScrollStateChanged(int state) {
  10.  
  11. }
    });
    }
    }
    viewpager适配器的书写代码
  1. public class MyViewPagerAdapter extends PagerAdapter {
  2.  
  3. private List<ImageView> imageViewList;//建一个imageview类型的list链表
  4.  
  5. public MyViewPagerAdapter(List<ImageView> imageViewList) {
    this.imageViewList = imageViewList;
    }
    @Override
    public int getCount() {
    return imageViewList.size();//返回图片数量
    }
  6.  
  7. @Override
    public boolean isViewFromObject(View view, Object object) {
    return view==object;//返回这是一个view值
    }
  8.  
  9. @Override
    public Object instantiateItem(ViewGroup container, int position) {
    container.addView(imageViewList.get(position));
    return imageViewList.get(position);//增加图片,position图片数量
  10.  
  11. }
  12.  
  13. @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView(imageViewList.get(position));//删除图片
  14.  
  15. }
    }
  1.  

高级控件之Scrollview ( 滑动屏幕 ) 与 Imageview (滑动屏幕 切换图片)的更多相关文章

  1. Android高级控件--AdapterView与Adapter

    在J2EE中提供过一种非常好的框架--MVC框架,实现原理:数据模型M(Model)存放数据,利用控制器C(Controller)将数据显示在视图V(View)上.在Android中有这样一种高级控件 ...

  2. Android高级控件(六)——自定义ListView高仿一个QQ可拖拽列表的实现

    Android高级控件(六)--自定义ListView高仿一个QQ可拖拽列表的实现 我们做一些好友列表或者商品列表的时候,居多的需求可能就是需要列表拖拽了,而我们选择了ListView,也是因为使用L ...

  3. UGUI核心元素、基本控件、复合控件和高级控件

    UGUI的核心元素: Anchor(锚点):每个控件都有一个Anchor属性,控件的4个顶点,分别与Anchor的4个点保持不变的距离,不受屏幕分辨率变化的影响. 系统默认设置控件的Anchor位置在 ...

  4. Android 高级控件(七)——RecyclerView的方方面面

    Android 高级控件(七)--RecyclerView的方方面面 RecyclerView出来很长时间了,相信大家都已经比较了解了,这里我把知识梳理一下,其实你把他看成一个升级版的ListView ...

  5. Android高级控件(五)——如何打造一个企业级应用对话列表,以QQ,微信为例

    Android高级控件(五)--如何打造一个企业级应用对话列表,以QQ,微信为例 看标题这么高大上,实际上,还是运用我么拿到listview去扩展,我们讲什么呢,就是研究一下QQ,微信的这种对话列表, ...

  6. Android高级控件(四)——VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷

    Android高级控件(四)--VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷 是不是感觉QQ空间什么的每次新版本更新那炫炫的引导页就特别的激动,哈哈,其实他实现起来真的很简单很 ...

  7. Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系

    Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 现在的二维码可谓是烂大街了,到处都是二维码,什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...

  8. Android高级控件(二)——SurfaceView实现GIF动画架包,播放GIF动画,自己实现功能的初体现

    Android高级控件(二)--SurfaceView实现GIF动画架包,播放GIF动画,自己实现功能的初体现 写这个的原因呢,也是因为项目中用到了gif动画,虽然网上有很多的架包可以实现,不过我们还 ...

  9. Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能

    Android高级控件(一)--ListView绑定CheckBox实现全选,增加和删除等功能 这个控件还是挺复杂的,也是项目中应该算是比较常用的了,所以写了一个小Demo来讲讲,主要是自定义adap ...

随机推荐

  1. 解决浏览器兼容ES6特性

    为什么ES6会有兼容性问题? 由于广大用户使用的浏览器版本在发布的时候也许早于ES6的定稿和发布,而到了今天,我们在编程中如果使用了ES6的新特性,浏览器若没有更新版本,或者新版本中没有对ES6的特性 ...

  2. C语言的隐式类型转换

    看了一篇博文,该文章中有如下描述 #include <stdio.h> int main() { unsigned ; unsigned ; )) //a和b-1的结果-1都被转换为int ...

  3. http协议中302和303的区别

    http1.0协议中只有302码,没有303状态码:http1.1,在默认情况下,很多服务端基础程序,为了兼容http1.0,在遇到本应响应303时,也给客户端响应了302. 碰到的问题: 场景: 在 ...

  4. nodejs简单数据迁移demo

    近期做数据迁移,采用nodejs框架,数据库为mysql.作为一枚菜鸟,在编码过程中,遇到众多奇葩问题,感谢民少给予的支持. 由于旧数据库中的数据,在之前设计中存在众多不合理的情况,因此在数据迁移中, ...

  5. ECEF和大地坐标系的相互转化

    在阅读 RTKLIB的源码时,发现了ECEF和大地坐标系的相互转换的函数,大地坐标系(φ,λ,h)转成ECEF(X,Y,Z)与所看书籍(GPS原理与接收机,谢刚,电子工业出版社)的公式是一样的,而EC ...

  6. Java基础学习笔记九 Java基础语法之this和super

    构造方法 我们对封装已经有了基本的了解,接下来我们来看一个新的问题,依然以Person为例,由于Person中的属性都被private了,外界无法直接访问属性,必须对外提供相应的set和get方法.当 ...

  7. 项目Alpha冲刺Day5

    一.会议照片 二.项目进展 1.今日安排 熟悉后台框架并尝试编写及继续搭建前台框架模版.完成登录相关的功能实现,添加一些用户相关的单元测试代码,以及相应的测试数据. 2.问题困难 前端不是很熟,页面框 ...

  8. 20145237 实验一 逆向与Bof基础

    20145237 实验一 逆向与Bof基础 1.直接修改程序机器指令,改变程序执行流程 此次实验是下载老师传给我们的一个名为pwn1的文件. 首先,用 objdump -d pwn1 对pwn1进行反 ...

  9. 团队作业4——第一次项目冲刺(Alpha版本)2017.11.16

    第一次会议:2017-11-16 大家的任务完成的不错^_^,继续努力了. 上图: 忘记照了,额....... 会议主要内容: 1.登录功能的讨论 2. 代码统一 具体分工: 成员 计划任务 遇见难题 ...

  10. 基于协程的Python网络库gevent

    import gevent def test1(): print 12 gevent.sleep(0) print 34 def test2(): print 56 gevent.sleep(0) p ...