Fragment

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button toutiao;
    private Button guonei;
    private Button junshi;
    private Button keji;
    private int count = 0;
    private ViewPager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        final ArrayList<Fragment> fs = new ArrayList<>();
        Fragment1 fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();
        Fragment3 fragment3 = new Fragment3();
        Fragment4 fragment4 = new Fragment4();
        fs.add(fragment1);
        fs.add(fragment2);
        fs.add(fragment3);
        fs.add(fragment4);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fs.get(position);
            }
            @Override
            public int getCount() {
                return fs.size();
            }
        });
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
              int positionOffsetPixels) {

               }
            @Override
            public void onPageSelected(int position) {
                    demo(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void init() {
        toutiao = (Button) findViewById(R.id.toutiao);
        guonei = (Button) findViewById(R.id.guonei);
        junshi = (Button) findViewById(R.id.junshi);
        keji = (Button) findViewById(R.id.keji);
        toutiao.setOnClickListener(this);
        guonei.setOnClickListener(this);
        junshi.setOnClickListener(this);
        keji.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.toutiao:
                count = 0;
                break;
            case R.id.guonei:
                count = 1;
                break;
            case R.id.junshi:
                count = 2;
                break;
            case R.id.keji:
                count = 3;
                break;
        }
        viewpager.setCurrentItem(count);
        demo(count);
    }
    public void demo(int count){
        toutiao.setTextColor(count == 0? Color.RED:Color.BLACK);
        guonei.setTextColor(count == 1? Color.RED:Color.BLACK);
        junshi.setTextColor(count == 2? Color.RED:Color.BLACK);
        keji.setTextColor(count == 3? Color.RED:Color.BLACK);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值