viewpager切换


//Fragment

public class MainActivity extends AppCompatActivity {


    private ViewPager viewPager;
    private List<Fragment> data;
    private MyAdapter adapter;
    private Button[] titles;//所有的标题




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


        viewPager = (ViewPager) findViewById(R.id.view_pager);


        //构建数据源
        data = new ArrayList<>();
        MyFragment01 fragment01 = new MyFragment01();
        data.add(fragment01);
        MyFragment02 fragment02 = new MyFragment02();
        data.add(fragment02);
        MyFragment03 fragment03 = new MyFragment03();
        data.add(fragment03);




        //初始化标题
        initTitles();


        //定义适配器
        adapter = new MyAdapter(getSupportFragmentManager());


        viewPager.setAdapter(adapter);


        //为ViewPager添加事件
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageSelected(int position) {


                for(int i=0;i<titles.length;i++)
                {
                    titles[i].setBackgroundColor(Color.WHITE);
                }
                titles[position].setBackgroundColor(Color.RED);


            }


            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub


            }


            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub


            }
        });


    }




    private void initTitles() {


        //得到布局容器
        LinearLayout layout  = (LinearLayout) findViewById(R.id.layout_id);


        titles = new Button[data.size()];


        for(int i=0;i<titles.length;i++)
        {
            //得到布局中的子视图
            titles[i] = (Button) layout.getChildAt(i);
            titles[i].setBackgroundColor(Color.WHITE);




            //点击标题, 切换ViewPager
            titles[i].setTag(i);


            titles[i].setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {
                    // 点击后, ViewPager切换到指定位置
                    viewPager.setCurrentItem((Integer)v.getTag());


                }
            });


        }


        //设置第一个为默认选中的颜色
        titles[0].setBackgroundColor(Color.RED);


    }


    class MyAdapter extends FragmentPagerAdapter
    {


        public MyAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }


        @Override
        public Fragment getItem(int position) {
            // TODO 返回指定下标对应的Fragment
            return data.get(position);
        }


        @Override
        public int getCount() {
            // TODO 得到数据源中的总数量
            return data.size();
        }


    }
}




//布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bw.com.day23.MainActivity">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="我的页面"
        android:gravity="center"
        android:background="#ccc"
        android:id="@+id/tv"
        />


    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_below="@id/tv"
        android:id="@+id/view_pager"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/layout_id"
        android:layout_below="@id/view_pager"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/but_01"
            android:text="第一个"
            android:onClick="onClick"/>


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/but_02"
            android:text="第二个"
            android:onClick="onClick"/>


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/but_03"
            android:text="第三个"
            android:onClick="onClick"/>


    </LinearLayout>
</RelativeLayout>






//布局

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center"
        android:id="@+id/ly_id">


      <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@mipmap/ic_launcher"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>


    </LinearLayout>


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"
        android:divider="@color/colorAccent"
        android:dividerHeight="2dp"
        />


</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值