0

我有一个 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>

我已经完全按照链接进行操作,但如果您想查看活动代码,请告诉我。

4

1 回答 1

1

i want to change the background of the buttons that are outside of the pager when the fragment change the page.

Use a ViewPager.OnPageChangedListener on the ViewPager:

((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) {

        }
    });
于 2012-11-22T17:47:25.610 回答