ViewPage+Fragmnet

实现标签滑动

首先是布局文件

<HorizontalScrollView
        android:id="@+id/ho"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


            <TextView
                android:id="@+id/t1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会调查" />


            <TextView
                android:id="@+id/t2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会实践" />


            <TextView
                android:id="@+id/t3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会公益" />


            <TextView
                android:id="@+id/t4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会毒瘤" />


            <TextView
                android:id="@+id/t5"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会害虫" />


            <TextView
                android:id="@+id/t6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会天地" />




        </LinearLayout>
    </HorizontalScrollView>


    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ho" />


再是主界面



public class MainActivity extends FragmentActivity implements View.OnClickListener {


    private ViewPager vp;
    private TextView t1;
    private TextView t2;
    private TextView t3;
    private TextView t4;
    private TextView t5;


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


        vp = (ViewPager) findViewById(R.id.vp);
        vp.setOffscreenPageLimit(5);
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {


            @Override
            public int getCount() {


                return 5;
            }


            @Override
            public Fragment getItem(int arg0) {
                // TODO Auto-generated method stub
                Fragment fragment = null;
                switch (arg0) {
                    case 0:
                        fragment = new Fragment1();
                        break;
                    case 1:
                        fragment = new Fragment2();
                        break;
                    case 2:
                        fragment = new Fragment3();
                        break;
                    case 3:
                        fragment = new Fragment4();
                        break;
                    case 4:
                        fragment = new Fragment5();
                        break;


                    default:
                        break;
                }
                return fragment;
            }
        });


        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position) {
                    case 0:
                        t1.setTextColor(Color.RED);
                        t2.setTextColor(Color.BLACK);
                        t3.setTextColor(Color.BLACK);
                        t4.setTextColor(Color.BLACK);
                        t5.setTextColor(Color.BLACK);
                        break;
                    case 1:
                        t1.setTextColor(Color.BLACK);
                        t2.setTextColor(Color.RED);
                        t3.setTextColor(Color.BLACK);
                        t4.setTextColor(Color.BLACK);
                        t5.setTextColor(Color.BLACK);
                        break;
                    case 2:
                        t1.setTextColor(Color.BLACK);
                        t2.setTextColor(Color.BLACK);
                        t3.setTextColor(Color.RED);
                        t4.setTextColor(Color.BLACK);
                        t5.setTextColor(Color.BLACK);
                        break;
                    case 3:
                        t1.setTextColor(Color.BLACK);
                        t2.setTextColor(Color.BLACK);
                        t3.setTextColor(Color.BLACK);
                        t4.setTextColor(Color.RED);
                        t5.setTextColor(Color.BLACK);
                        break;
                    case 4:
                        t1.setTextColor(Color.BLACK);
                        t2.setTextColor(Color.BLACK);
                        t3.setTextColor(Color.BLACK);
                        t4.setTextColor(Color.BLACK);
                        t5.setTextColor(Color.RED);
                        break;
                }




            }


            @Override
            public void onPageSelected(int position) {


            }


            @Override
            public void onPageScrollStateChanged(int state) {


            }
        });


        t1 = (TextView) findViewById(R.id.t1);
        t2 = (TextView) findViewById(R.id.t2);
        t3 = (TextView) findViewById(R.id.t3);
        t4 = (TextView) findViewById(R.id.t4);
        t5 = (TextView) findViewById(R.id.t5);
        t1.setOnClickListener(this);
        t2.setOnClickListener(this);
        t3.setOnClickListener(this);
        t4.setOnClickListener(this);
        t5.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.t1:
                vp.setCurrentItem(0);
                t1.setTextColor(Color.RED);
                t2.setTextColor(Color.BLACK);
                t3.setTextColor(Color.BLACK);
                t4.setTextColor(Color.BLACK);
                t5.setTextColor(Color.BLACK);
                break;
            case R.id.t2:
                vp.setCurrentItem(1);
                t2.setTextColor(Color.RED);
                t1.setTextColor(Color.BLACK);
                t3.setTextColor(Color.BLACK);
                t4.setTextColor(Color.BLACK);
                t5.setTextColor(Color.BLACK);
                break;
            case R.id.t3:
                vp.setCurrentItem(2);
                t3.setTextColor(Color.RED);
                t2.setTextColor(Color.BLACK);
                t1.setTextColor(Color.BLACK);
                t4.setTextColor(Color.BLACK);
                t5.setTextColor(Color.BLACK);
                break;
            case R.id.t4:
                vp.setCurrentItem(3);
                t4.setTextColor(Color.RED);
                t2.setTextColor(Color.BLACK);
                t3.setTextColor(Color.BLACK);
                t1.setTextColor(Color.BLACK);
                t5.setTextColor(Color.BLACK);
                break;
            case R.id.t5:
                vp.setCurrentItem(4);
                t5.setTextColor(Color.RED);
                t2.setTextColor(Color.BLACK);
                t3.setTextColor(Color.BLACK);
                t4.setTextColor(Color.BLACK);
                t1.setTextColor(Color.BLACK);
                break;




            default:
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值