我使用的是標準Viewpager自定義PageAdapter。Android的 - 訪問的TextView裏面ViewPager
如何訪問Viewpager的TextView,更改onPageSelected()的字體顏色?
編輯1:ViewPager監聽
final ViewPager vp = (ViewPager) findViewById(R.id.a2viewpager);
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
vp.getParent().requestDisallowInterceptTouchEvent(true);
}
public void onPageSelected(final int currentPage) {
ImageButton leftNav = (ImageButton) findViewById(R.id.a2left_nav);
ImageButton rightNav = (ImageButton) findViewById(R.id.a2right_nav);
int lastIdx = vpAadapter.getCount() - 1;
if (receiptPool.get(currentPage).contains("_(Canceled)") == true){
vp.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorRed));
}
else {
vp.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorMainTextBackground));
}
}
});
編輯1:自定義適配器
public class CustomPagerAdapter extends PagerAdapter {
Context mContext;
private Vector<View> pages;
private activity1 act1;
private activity2 act2;
private Intent intent;
public CustomPagerAdapter(Context context, Vector<View> pages) {
super();
this.act1 = new activity1();
this.act2 = new activity2();
this.mContext=context;
this.pages=pages;
}
@Override
public Object instantiateItem(final ViewGroup container, final int CurrentPage) {
View page = pages.get(CurrentPage);
page.setOnTouchListener(new View.OnTouchListener() {
/******/
});
container.addView(page);
return page;
}
@Override
public int getCount() {
return pages.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getItemPosition(Object object) {
// Seems to be required as a workaround.
return POSITION_NONE;
}
}
感謝
你能提供的源代碼 –
@vinodh庫馬爾我加入了一些代碼,THX。 –