利用ViewPager建立多个关卡,左右切换
这是主activity:
黔西南州ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
public class BActivity extends Activity {
private ViewPager viewPager;
private TextView text, text2, text3;
private MyViewPager myPager;
private List
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
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:
android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF" > android:id="@+id/line" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#888888" > 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" /> 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" /> 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" /> android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/line" />
这是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
public MyViewPager(List
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;
}
}
当前文章:利用ViewPager建立多个关卡,左右切换
转载注明:http://ybzwz.com/article/ipesgs.html