【问题标题】:Radio buttons to see which fragment is selected strange behaviour单选按钮查看选择了哪个片段的奇怪行为
【发布时间】:2015-08-07 11:06:01
【问题描述】:

我正在努力找出为什么我的单选按钮在我的片段活动中没有以正确的顺序选择。我正在尝试使用单选按钮来跟踪选择了哪个片段。但它们似乎是随机检查的。

片段活动的我的 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>

我的 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;
    }
}

【问题讨论】:

    标签: android android-fragments radio-button


    【解决方案1】:

    如果有人需要,这里是解决它的代码

    代码

       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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-11
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 2014-08-17
      • 2014-09-20
      • 2013-09-24
      • 1970-01-01
      • 2011-07-12
      相关资源
      最近更新 更多