利用ViewPager建立多个关卡,左右切换

发布时间:2020-07-01 05:27:18 作者:清甘茶
来源:网络 阅读:395

    这是主activity:

public class BActivity extends Activity {


private ViewPager viewPager;

private TextView text, text2, text3;

private MyViewPager myPager;

private List<View> mList;

private View view1, view2, view3;


@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

initText();

initViewPager();

}

//初始化textView

public void initText() {

text = (TextView) findViewById(R.id.text);

text2 = (TextView) findViewById(R.id.text2);

text3 = (TextView) findViewById(R.id.text3);

text.setOnClickListener(new MyOnClickListerImpl(0));

text2.setOnClickListener(new MyOnClickListerImpl(1));

text3.setOnClickListener(new MyOnClickListerImpl(2));

}

//TextView的监听事件

private class MyOnClickListerImpl implements OnClickListener {

int index = 0 ;

public MyOnClickListerImpl(int i){

index = i ;

}

@Override

public void onClick(View v) {

viewPager.setCurrentItem(index);

}

}

//初始化viewPager

public void initViewPager() {

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

view1 = LayoutInflater.from(BActivity.this).inflate(

R.layout.show_content, null);

view2 = LayoutInflater.from(BActivity.this).inflate(

R.layout.show_content2, null);

view3 = LayoutInflater.from(BActivity.this).inflate(

R.layout.show_content3, null);

mList = new ArrayList<View>();

mList.add(view1);

mList.add(view2);

mList.add(view3);


myPager = new MyViewPager(mList);

viewPager.setAdapter(myPager);

viewPager.setCurrentItem(0);

viewPager.setOnPageChangeListener(new OnPageChangeListenerImpl());

}


private class OnPageChangeListenerImpl implements OnPageChangeListener {


@Override

public void onPageScrollStateChanged(int arg0) {

/*

* 此方法是改变状态的时候调用,arg0有三个值,分别是0、1、2 当为0的时候说明说明都没做, 当为1的时候说明正在滑动

* 当为2的时候说明已经滑动完成

*/


}


@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

/*

* 这个方法是滑动的时候调用的, 第一个参数是当前页面 ,第二个参数是当前页面偏移的百分比, 第三个页面是当前页面偏移的像素位置

*/


}


@Override

public void onPageSelected(int arg0) {

// 此方法是页面跳转完成之后调用,参数的意思是,当前旋转的position,点击选中的position

switch (arg0) {

case 0:

text.setTextColor(getResources().getColor(R.color.whi));

text2.setTextColor(getResources().getColor(R.color.back));

text3.setTextColor(getResources().getColor(R.color.back));

break;

case 1:

text.setTextColor(getResources().getColor(R.color.back));

text2.setTextColor(getResources().getColor(R.color.whi));

text3.setTextColor(getResources().getColor(R.color.back));

break;

case 2:

text.setTextColor(getResources().getColor(R.color.back));

text2.setTextColor(getResources().getColor(R.color.back));

text3.setTextColor(getResources().getColor(R.color.whi));

break;

default:

break;

}

}

}

这是main.xml:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#FFFFFF" >


    <LinearLayout

        android:id="@+id/line"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        android:background="#888888" >


        <TextView

            android:id="@+id/text"

            android:layout_width="0sp"

            android:layout_height="wrap_content"

            android:layout_margin="10sp"

            android:layout_weight="1"

            android:gravity="center_horizontal"

            android:text="关卡一"

            android:textColor="#FFFFFF"

            android:textSize="20sp" />


        <TextView

            android:id="@+id/text2"

            android:layout_width="0sp"

            android:layout_height="wrap_content"

            android:layout_margin="10sp"

            android:layout_weight="1"

            android:gravity="center_horizontal"

            android:text="关卡二"

            android:textColor="#000000"

            android:textSize="20sp" />


        <TextView

            android:id="@+id/text3"

            android:layout_width="0sp"

            android:layout_height="wrap_content"

            android:layout_margin="10sp"

            android:layout_weight="1"

            android:gravity="center_horizontal"

            android:text="关卡三"

            android:textColor="#000000"

            android:textSize="20sp" />

    </LinearLayout>


    <android.support.v4.view.ViewPager

        android:id="@+id/viewPager"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_below="@id/line" />


</RelativeLayout>

这是MyViewPager:

package your.namespace;


import java.util.List;

import android.support.v4.view.PagerAdapter;

import android.view.View;

import android.view.ViewGroup;


public class MyViewPager extends PagerAdapter {


private List<View> mList;


public MyViewPager(List<View> mList) {

this.mList = mList;

}


@Override

public int getCount() {

// 返回mList中包含view的所有view的个数

return mList.size();

}


@Override

public void destroyItem(ViewGroup container, int position, Object object) {

// 删除mList中的某一个view

container.removeView(mList.get(position));

}


@Override

public Object instantiateItem(ViewGroup container, int position) {

// 添加一个view到mList中

container.addView(mList.get(position), 0);

return mList.get(position);

}


@Override

public boolean isViewFromObject(View arg0, Object arg1) {


return arg0 == arg1;

}

}


附件:http://down.51cto.com/data/2365686
推荐阅读:
  1. ViewPager左右滑动
  2. activity多个片段添加切换(没有用到viewpager)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

viewpager 多个关卡 左右切换

上一篇:简单易用的Rest

下一篇:[SQL]学习一下sql数据分页

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》