您好,登錄后才能下訂單哦!
這是主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;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。