您好,登錄后才能下訂單哦!
這是主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的監(jiān)聽(tīng)事件
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) {
/*
* 此方法是改變狀態(tài)的時(shí)候調(diào)用,arg0有三個(gè)值,分別是0、1、2 當(dāng)為0的時(shí)候說(shuō)明說(shuō)明都沒(méi)做, 當(dāng)為1的時(shí)候說(shuō)明正在滑動(dòng)
* 當(dāng)為2的時(shí)候說(shuō)明已經(jīng)滑動(dòng)完成
*/
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
/*
* 這個(gè)方法是滑動(dòng)的時(shí)候調(diào)用的, 第一個(gè)參數(shù)是當(dāng)前頁(yè)面 ,第二個(gè)參數(shù)是當(dāng)前頁(yè)面偏移的百分比, 第三個(gè)頁(yè)面是當(dāng)前頁(yè)面偏移的像素位置
*/
}
@Override
public void onPageSelected(int arg0) {
// 此方法是頁(yè)面跳轉(zhuǎn)完成之后調(diào)用,參數(shù)的意思是,當(dāng)前旋轉(zhuǎn)的position,點(diǎn)擊選中的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="關(guān)卡一"
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="關(guān)卡二"
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="關(guān)卡三"
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的個(gè)數(shù)
return mList.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// 刪除mList中的某一個(gè)view
container.removeView(mList.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 添加一個(gè)view到mList中
container.addView(mList.get(position), 0);
return mList.get(position);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。