单选按钮,查看选择哪个片段的奇怪行为

时间:2015-05-25 23:31:56

标签: android android-fragments radio-button

我很难找到为什么在我的片段活动中没有以正确的顺序选择我的单选按钮。我试图使用单选按钮来跟踪选择的片段。但他们似乎随意检查。

片段活动的我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fb="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.partyfind.ScreenSlidePagerActivity">

<LinearLayout
    android:id="@+id/alipay_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.2"
    android:orientation="vertical">
    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:layout_width="match_parent"/>
</LinearLayout>

<RelativeLayout
    android:id="@+id/taobao_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.8"
    android:orientation="vertical">
    <RadioGroup
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">
        <RadioButton
            android:id="@+id/selector_1"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:button="@null"
            android:background="@drawable/radio_button_slide_indicator"/>
        <RadioButton
            android:id="@+id/selector_2"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:button="@null"
            android:background="@drawable/radio_button_slide_indicator"/>
        <RadioButton
            android:id="@+id/selector_3"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:button="@null"
            android:background="@drawable/radio_button_slide_indicator"/>
        <RadioButton
            android:id="@+id/selector_4"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:button="@null"
            android:background="@drawable/radio_button_slide_indicator"/>
        </RadioGroup>
    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="fill_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_height="55dp"
        android:textSize="20dp"
        android:background="@drawable/facebook_btn_login"
        fb:login_text="Login with Facebook"
        fb:logout_text="Log out of Facebook"
        android:layout_centerInParent="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="We wont post on your behalf"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"/>
</RelativeLayout>

My FragmentStatePagerAdapter:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Log.v("Position"," "+String.valueOf(position));

        switch(position) {
            case 0: selector(0); return Fragment1.newInstance();
            case 1: selector(1); return Fragment2.newInstance();
            case 2: selector(2); return Fragment3.newInstance();
            case 3: selector(3); return Fragment4.newInstance();
            default:selector(0); return Fragment1.newInstance();
        }
    }
    public void selector(int position){
        RadioButton selector_1 = (RadioButton) findViewById(R.id.selector_1);
        RadioButton selector_2 = (RadioButton) findViewById(R.id.selector_2);
        RadioButton selector_3 = (RadioButton) findViewById(R.id.selector_3);
        RadioButton selector_4 = (RadioButton) findViewById(R.id.selector_4);
        if (position == 0){
            resetSelector();
            selector_1.setChecked(true);
        }
        if (position == 1){
            resetSelector();
            selector_2.setChecked(true);
        }
        if (position == 2){
            resetSelector();
            selector_3.setChecked(true);
        }
        if (position == 3){
            resetSelector();
            selector_4.setChecked(true);
        }
    }
    public void resetSelector(){
        RadioButton selector_1 = (RadioButton) findViewById(R.id.selector_1);
        RadioButton selector_2 = (RadioButton) findViewById(R.id.selector_2);
        RadioButton selector_3 = (RadioButton) findViewById(R.id.selector_3);
        RadioButton selector_4 = (RadioButton) findViewById(R.id.selector_4);
        selector_1.setChecked(false);
        selector_2.setChecked(false);
        selector_3.setChecked(false);
        selector_4.setChecked(false);


    }
    @Override
    public int getCount() {
        return 4;
    }
}

1 个答案:

答案 0 :(得分:0)

如果任何人需要它,这里解决的是

<强>代码

   private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int position) {

        switch(position) {
            case 0: return Fragment1.newInstance();
            case 1: return Fragment2.newInstance();
            case 2: return Fragment3.newInstance();
            case 3: return Fragment4.newInstance();
            default:return Fragment1.newInstance();
        }
    }
    public void selector(int position){
        RadioButton selector_1 = (RadioButton) findViewById(R.id.selector_1);
        RadioButton selector_2 = (RadioButton) findViewById(R.id.selector_2);
        RadioButton selector_3 = (RadioButton) findViewById(R.id.selector_3);
        RadioButton selector_4 = (RadioButton) findViewById(R.id.selector_4);
        if (position == 0){
            selector_1.setChecked(true);
        }
        if (position == 1){
            selector_2.setChecked(true);
        }
        if (position == 2){
            selector_3.setChecked(true);
        }
        if (position == 3){
            selector_4.setChecked(true);
        }
    }

    @Override
    public int getCount() {
        selector(mPager.getCurrentItem());
        return 4;
    }
}