二级联动轮播图

布局文件:

activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context="com.example.myviewpaper.MainActivity">
    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>
        <RadioGroup
            android:layout_width="match_parent"
            android:id="@+id/rg"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="40dp"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/rb1"
                android:layout_width="10dp"
                android:button="@null"
                android:checked="true"
                android:background="@drawable/icon"
                android:layout_height="10dp" />
            <RadioButton
                android:id="@+id/rb2"
                android:layout_width="10dp"
                android:button="@null"
                android:background="@drawable/icon"
                android:layout_height="10dp" />
            <RadioButton
                android:id="@+id/rb3"
                android:layout_width="10dp"
                android:button="@null"
                android:background="@drawable/icon"
                android:layout_height="10dp" />
        </RadioGroup>
</RelativeLayout>

视图布局

Viewone

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@mipmap/img1"
    android:layout_height="match_parent">

</LinearLayout>

Viewtwo

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@mipmap/img2"
    android:layout_height="match_parent">

</LinearLayout>

Viewthree

按钮事件自行添加

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@mipmap/img3"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:text="立即体验"
        android:textSize="20sp"
        android:background="#FF7800"
        android:textColor="@android:color/white"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="45dp"
        android:layout_height="wrap_content" />
</RelativeLayout>

MainActivity

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private RadioGroup rg;
    List<View> list=new ArrayList<>();
    private Button bt1;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = (ViewPager) findViewById(R.id.vp);
        rg = (RadioGroup) findViewById(R.id.rg);
        list.add(LayoutInflater.from(this).inflate(R.layout.view1,null));
        list.add(LayoutInflater.from(this).inflate(R.layout.view2,null));
        View inflate = LayoutInflater.from(this).inflate(R.layout.view3, null);
        bt1 = ((Button) inflate.findViewById(R.id.bt1));
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });
        list.add(inflate);
        adapter = new MyAdapter(list);
        vp.setAdapter(adapter);
     //ViewPager页面滑动事件
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override//页面滑动事件
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override//页面选中事件
            public void onPageSelected(int position) {
                RadioButton childAt = (RadioButton) rg.getChildAt(position);
                childAt.setChecked(true);
            }

            @Override//页面活动状态改变事件
            public void onPageScrollStateChanged(int state) {

            }
        });
     //圆点选中状态改变事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb1://如果第一个RadioButton选中,就把ViewPager的当前显示项设为第一页
                        vp.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        vp.setCurrentItem(2);
                        break;
                }
            }
        });
    }
}

MyAdapter 适配器

public class MyAdapter extends PagerAdapter {
    private List<View> list;

    public MyAdapter(List<View> list) {
        this.list = list;
    }
    //获取数据源长度
    @Override
    public int getCount() {
        return list.size();
    }
    
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    //实例化一个页面
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(list.get(position));//往父容器(ViewPager)里添加视图
        return list.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(list.get(position));//从父容器中移除该页面
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值