我有一个FragmentPagerAdapter(this)的实现,我想在片段更改页面时更改寻呼机外部按钮的背景。
这是我的主要xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_ll"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView android:src="@drawable/banner"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:contentDescription="@string/banner_desc"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<LinearLayout android:orientation="horizontal"
android:gravity="center" android:measureWithLargestChild="true"
android:layout_width="match_parent" android:layout_height="70dip"
android:layout_weight="0"
android:paddingTop="10dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
style="@style/BottomTabs">
<Button android:id="@+id/btn_theaters"
android:drawableTop="@drawable/bt_theaters_icon"
android:text="@string/label_theaters"
style="@style/BtnTabs">
</Button>
<ImageView android:src="@drawable/tabs_separator"
android:contentDescription="@string/tabs_separator_desc"
android:layout_width="10dip" android:layout_height="63dip"
/>
<Button android:id="@+id/btn_movies"
android:drawableTop="@drawable/bt_movies_icon"
android:text="@string/label_movies"
style="@style/BtnTabs">
</Button>
<ImageView android:src="@drawable/tabs_separator"
android:contentDescription="@string/tabs_separator_desc"
android:layout_width="10dip" android:layout_height="63dip"
/>
<Button android:id="@+id/btn_premiers"
android:drawableTop="@drawable/bt_premiers_icon"
android:text="@string/label_premiers"
style="@style/BtnTabs">
</Button>
<ImageView android:src="@drawable/tabs_separator"
android:contentDescription="@string/tabs_separator_desc"
android:layout_width="10dip" android:layout_height="63dip"
/>
<Button android:id="@+id/btn_soon"
android:drawableTop="@drawable/bt_soon_icon"
android:text="@string/label_soon"
style="@style/BtnTabs">
</Button>
<ImageView android:src="@drawable/tabs_separator"
android:contentDescription="@string/tabs_separator_desc"
android:layout_width="10dip" android:layout_height="63dip"
/>
<Button android:id="@+id/btn_deals"
android:drawableTop="@drawable/bt_deals_icon"
android:text="@string/label_deals"
style="@style/BtnTabs">
</Button>
</LinearLayout>
我完全按照this链接,但如果您想查看活动代码,请告诉我。
答案 0 :(得分:1)
我想改变外面的按钮的背景 片段更改页面时的寻呼机。
在ViewPager
上使用ViewPager.OnPageChangedListener
:
((ViewPager)findViewById(R.id.pager)).setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// the position parameter tells us at what page we moved to
// you could use the other two methods to update the views as
// soon as the user starts the swipe
// get a reference to the Buttons and change their backgrounds
}
@Override
public void onPageScrolled(int position, float offset, int offsetPixel) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});