ViewPager和Fragment


main:

<?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">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>


    <RadioGroup
        android:id="@+id/radion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

            <RadioButton
                android:id="@+id/sy"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@mipmap/ic_nav_home"
                android:text="首页"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />


            <RadioButton

                android:id="@+id/fl"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@mipmap/ic_nav_class"
                android:text="分类" />


            <RadioButton
                android:gravity="center"
                android:id="@+id/gwc"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@mipmap/ic_nav_cart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="购物车" />


            <RadioButton
                android:gravity="center"
                android:id="@+id/gr"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@mipmap/ic_nav_user"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="个人"
                />


    </RadioGroup>



</LinearLayout>


Activity:

package aaaaa.appxm;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class Main2Activity extends AppCompatActivity {

    private ViewPager viewpager;
    private RadioButton fl, sy, gwc, gr;
    private RadioGroup radion;

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

        viewpager=(ViewPager) findViewById(R.id.viewpager);
        gr=(RadioButton) findViewById(R.id.gr);
        fl=(RadioButton) findViewById(R.id.fl);
        gwc=(RadioButton) findViewById(R.id.gwc);
        sy=(RadioButton) findViewById(R.id.sy);
        radion=(RadioGroup)findViewById(R.id.radion);


        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                Fragment fragment = null;
                switch (position) {
                    case 0:
                        fragment = new Fragment1();
                        break;
                    case 1:
                        fragment = new Fragment2();
                        break;
                    case 2:
                        fragment = new Fragment3();
                        break;
                    case 3:
                        fragment = new Fragment4();
                        break;

                    default:break;
                }
                return fragment;
            }

            @Override
            public int getCount() {
                return 4;
            }
        });



        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position){
                    case 0:
                        sy.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.red));
                        sy.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_home_press), null, null);
                        fl.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        fl.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_class), null, null);
                        gwc.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        gwc.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_cart), null, null);
                        gr.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        gr.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_user), null, null);


                    break;
                    case 1:
                        sy.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        sy.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_home), null, null);
                        fl.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.red));
                        fl.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_class_press), null, null);
                        gwc.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        gwc.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_cart), null, null);
                        gr.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        gr.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_user), null, null);
                        break;

                    case 2:
                        sy.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        sy.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_home), null, null);
                        fl.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        fl.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_class), null, null);
                        gwc.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.red));
                        gwc.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_cart_press), null, null);
                        gr.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        gr.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_user), null, null);
                        break;


                    case 3:
                        sy.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        sy.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_home), null, null);
                        fl.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        fl.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_class), null, null);
                        gwc.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.hs));
                        gwc.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_cart), null, null);
                        gr.setTextColor(ContextCompat.getColor(Main2Activity.this,R.color.red));
                        gr.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(Main2Activity.this, R.mipmap.ic_nav_user_press), null, null);
                        break;

                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });






        radion.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.sy:
                        viewpager.setCurrentItem(0);
                        break;
                    case R.id.fl:
                        viewpager.setCurrentItem(1);
                        break;
                    case R.id.gwc:
                        viewpager.setCurrentItem(2);
                        break;

                    case R.id.gr:
                        viewpager.setCurrentItem(3);
                        break;

                    default:
                        break;
                }
            }
        });
    }





}



Fragment

package aaaaa.appxm;

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;

public class Fragment1 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frgmen1, container, false);
        return view;
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值