实现五个页面滑动

布局:

<TextView
       android:id="@+id/top_text"
       style="@style/top_text"
       android:background="@color/blue"
       android:text="首页"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/main_viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <RadioGroup
        android:id="@+id/main_radio"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/home_btn"
            android:text="首页"
            style="@style/main_btn"/>
        <RadioButton
            android:id="@+id/service_btn"
            android:text="服务"
            style="@style/main_btn"/>
        <RadioButton
            android:id="@+id/dangjian_btn"
            android:text="党建"
            style="@style/main_btn"/>
        <RadioButton
            android:id="@+id/news_btn"
            android:text="新闻"
            style="@style/main_btn"/>
        <RadioButton
            android:id="@+id/my_btn"
            android:text="我的"
            style="@style/main_btn"/>
    </RadioGroup>

布局界面样式资源:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="top_text">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">20sp</item>
    </style>

    <style name="main_btn">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_gravity">center</item>
        <item name="android:button">@null</item>
        <item name="android:drawableTop">@drawable/change_btnicon</item>
        <item name="android:textColor">@drawable/change_btntext</item>
        <item name="android:textSize">18sp</item>
    </style>
</resources>

创建一个drawable资源实现点击变色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/home01"/>
    <item android:state_checked="false" android:drawable="@drawable/home02"/>
</selector>

创建一个drawable资源实现字体点击变色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:color="@color/blue"/>
    <item android:color="@color/black"/>
</selector>

java代码:

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.myapplication.Fragment.DangjianFragment;
import com.example.myapplication.Fragment.HomeFragment;
import com.example.myapplication.Fragment.MyFragment;
import com.example.myapplication.Fragment.NewsFragment;
import com.example.myapplication.Fragment.ServiceFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TextView top_text;
    ViewPager viewPager;
    RadioGroup radioGroup;
    Fragment home,service,dangjian,news,my;
    List<Fragment> fragmentLislist = new ArrayList<>();

    RadioButton r1,r2,r3,r4,r5;
    RadioButton [] arr_btn = {r1,r2,r3,r4,r5};
    int [] arr_btnid = {R.id.home_btn,R.id.service_btn,R.id.dangjian_btn,R.id.news_btn,R.id.my_btn};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        top_text = findViewById(R.id.top_text);
        viewPager = findViewById(R.id.main_viewpager);

         //初始化底部导航栏按钮
        for (int i=0;i<arr_btn.length;i++){
            arr_btn[i] = findViewById(arr_btnid[i]);
        }

        InitFragment();
        InitViewpager();
        InitBtn();
    }

    private void InitViewpager() {
        viewPager.setCurrentItem(0);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentLislist.get(position);
            }

            @Override
            public int getCount() {
                return fragmentLislist.size();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        radioGroup.check(R.id.home_btn);
                        break;
                    case 1:
                        radioGroup.check(R.id.service_btn);
                        break;
                    case 2:
                        radioGroup.check(R.id.dangjian_btn);
                        break;
                    case 3:
                        radioGroup.check(R.id.news_btn);
                        break;
                    case 4:
                        radioGroup.check(R.id.my_btn);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void InitBtn() {
        radioGroup = findViewById(R.id.main_radio);
        radioGroup.check(R.id.home_btn);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int current = viewPager.getCurrentItem();
                switch (checkedId){
                    case R.id.home_btn:
                        top_text.setText("首页");
                        current = 0;
                        break;
                    case R.id.service_btn:
                        top_text.setText("服务");
                        current = 1;
                        break;
                    case R.id.dangjian_btn:
                        top_text.setText("党建");
                        current = 2;
                        break;
                    case R.id.news_btn:
                        top_text.setText("新闻");
                        current = 3;
                        break;
                    case R.id.my_btn:
                        top_text.setText("我的");
                        current = 4;
                        break;
                }
                if (current!=viewPager.getCurrentItem()){
                    viewPager.setCurrentItem(current);
                }
            }
        });
    }

    private void InitFragment() {
        home = new HomeFragment();
        service = new ServiceFragment();
        dangjian = new DangjianFragment();
        news = new NewsFragment();
        my = new MyFragment();
        fragmentLislist.add(home);
        fragmentLislist.add(service);
        fragmentLislist.add(dangjian);
        fragmentLislist.add(news);
        fragmentLislist.add(my);
    }

        //全部服务界面跳转
    public void select (int i){
        radioGroup.check(arr_btnid[i]);
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值