如何确认选项卡中的开关是否已更改状态? 我尝试了自己的课程,但我不知道应该扩展它们。我想,如果改变方向,我也可以将Class用作存储。 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);
}
}}
我自己的视图/布局类
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;
}
布局
<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>
答案 0 :(得分:1)
您可以在片段
中切换时使用OnCheckedChangeListenerlightSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something based on isChecked
}
});
要在Fragment之外的某处传递此事件,您可以使用Otto。