简体   繁体   中英

Radiobuttons becomes unclickable when used with viewpager in android

I am using viewpager and segmented group to create swipeable fragments. These segmented group is actually based on RadioGroup. What I want is that on clicking these segments(actually radiobuttons) the respective pages should be loaded. The problem is these radiobuttons are not clickable. But as soon as I comment out this line

viewPager.setAdapter(adapter)

These radiobuttons becomes clickable. I tried searching for the solution but could not resolve it. Also I do not want to use TabLayout because I want this UI somewhere in the middle of screen.

Here's the code -

public class MainActivity extends AppCompatActivity implements      
ViewPager.OnPageChangeListener {

SegmentedGroup segmentedGroup;
ViewPager viewPager;
MyViewAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();

}

private void initView() {

    viewPager = (ViewPager) findViewById(R.id.pager);
    adapter = new MyViewAdapter(getSupportFragmentManager());

    segmentedGroup = (SegmentedGroup) findViewById(R.id.segmentedgroup);
    viewPager.setAdapter(adapter);
    segmentedGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i) {
                case R.id.rb1:
                    Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
                    viewPager.setCurrentItem(0);
                    break;

                case R.id.rb2:
                    Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
                    viewPager.setCurrentItem(1);
                    break;

                case R.id.rb3:
                    Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_SHORT).show();
                    viewPager.setCurrentItem(2);
                    break;

            }
        }
    });

    viewPager.addOnPageChangeListener(this);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(0);
    segmentedGroup.check(segmentedGroup.getChildAt(0).getId());
}


@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
    switch (position) {
        case 0:
            segmentedGroup.check(R.id.rb1);
            break;
        case 1:
            segmentedGroup.check(R.id.rb2);
            break;
        case 2:
            segmentedGroup.check(R.id.rb3);
            break;
    }
}

@Override
public void onPageScrollStateChanged(int state) {

}


class MyViewAdapter extends FragmentPagerAdapter {

    public MyViewAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0: return new FragmentOne();
            case 1: return new FragmentTwo();
            case 2: return new FragmentThree();
        }
        return null;
    }

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

Here is the XML-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">

<LinearLayout
    android:id="@+id/someView"
    android:layout_width="wrap_content"
    android:layout_height="105px"
    android:orientation="horizontal"></LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/someView"
    android:gravity="center_horizontal">

    <info.hoang8f.android.segmented.SegmentedGroup xmlns:segmented="http://schemas.android.com/apk/res-auto"
        android:id="@+id/segmentedgroup"
        android:layout_width="match_parent"
        android:layout_height="105px"
        android:orientation="horizontal"
        segmented:sc_border_width="1dp"
        segmented:sc_corner_radius="10dp"
        segmented:sc_tint_color="#36578B">

        <RadioButton
            android:id="@+id/rb1"
            style="@style/RadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="ONE" />

        <RadioButton
            android:id="@+id/rb2"
            style="@style/RadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TWO" />

        <RadioButton
            android:id="@+id/rb3"
            style="@style/RadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="THREE" />

    </info.hoang8f.android.segmented.SegmentedGroup>
</LinearLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>

I am beginner and any help is appreciated.

I figured out the answer after playing around with android layout. In android studio layout editor, the viewpager was occupying the whole screen. After adding android:layout_below parameter in viewpager it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM