Marcel Tinner
Marcel Tinner

Reputation: 1449

Listen on Switch state change in Tab / Android

How can I recognize if the Switch in the Tab have changed the state? I tried it with an own Class but I've got no idea from what I should extend them. I thought, I can use the Class also as a Storage if the Orientation is changed. Here is a Screenshot of my Application.

HomecontrolFragment.java

public class HomecontrolFragment extends Fragment {
private SlidingTabLayout mSlidingTabLayout;
private ViewPager mViewPager;
private String tabNames[] = {"General", "basement", "ground-floor", "first-floor", "heating"};

public HomecontrolFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_homecontrol, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
    mViewPager.setAdapter(new SamplePagerAdapter());
    mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
    mSlidingTabLayout.setViewPager(mViewPager);
}

class SamplePagerAdapter extends PagerAdapter {
    public int getCount() {
        return tabNames.length;
    }

    public boolean isViewFromObject(View view, Object o) {
        return o == view;
    }

    public CharSequence getPageTitle(int position) {
        System.out.write(position);
        return tabNames[position];
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view;
        switch (position) {
            case 0:
                view = GeneralTabFragment.getInstance(getActivity(), container);
                break;
            case 1:
                view = getActivity().getLayoutInflater().inflate(R.layout.pager_basement, container, false);
                break;
            case 2:
                view = getActivity().getLayoutInflater().inflate(R.layout.pager_groundfloor, container, false);
                break;
            case 3:
                view = getActivity().getLayoutInflater().inflate(R.layout.pager_firstfloor, container, false);
                break;
            default:
                view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
                        container, false);
                TextView title = (TextView) view.findViewById(R.id.item_title);
                title.setText(String.valueOf(position + 1));
                break;
        }

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}}

My own Class for the View / Layout

public class GeneralTabFragment extends View implements CompoundButton.OnCheckedChangeListener{
private static View instance = null;
Switch lightSwitch = (Switch) findViewById(R.id.general_light_switch);

public GeneralTabFragment(Context context) {
    super(context);
}

public static View getInstance(Activity activity, ViewGroup container){
    if (instance == null)
    {
        instance = activity.getLayoutInflater().inflate(R.layout.pager_general, container, false);
    }
return instance;
}

Layout

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin"
    android:paddingEnd="8dp">

    <Switch
        android:id="@+id/general_light_switch"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:text="@string/light" />

    <View
        android:layout_width="fill_parent"
        android:layout_marginTop="8dp"
        android:layout_height="1dp"
        android:background="@color/separator" />

    <Switch
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_marginTop="8dp"
        android:text="@string/heating" />

</LinearLayout>

Upvotes: 1

Views: 661

Answers (1)

WojciechKo
WojciechKo

Reputation: 1531

You can use OnCheckedChangeListener on switch in Fragment

lightSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // do something based on isChecked
     }
});

To pass this event somewhere outside of Fragment you can use Otto.

Upvotes: 1

Related Questions