我有两个片段。第二个片段的内容取决于第一个片段的用户输入。
因此,如果用户在第一个片段中放置0,我想显示第一个LinearLayout,如果他放1,我想显示第二个LinearLayout。两个LinearLayouts都在同一个xml文件中。
我的代码有效(见下文)。但它只适用于ONCE。在第一次调用时,它会执行它应该执行的所有操作。但是在以下所有调用中,setVisibility命令似乎不再起作用(同时控制台打印工作没有问题)。
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position+1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("test"+unitOfMeasurement);
View rootView;
View unitView = inflater.inflate(R.layout.fragment_personalization_unit, container, false);
View boundaryView = inflater.inflate(R.layout.fragment_personalization_low, container, false);
switch(getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
rootView = unitView;
break;
case 2:
rootView = boundaryView;
break;
default:
rootView = inflater.inflate(R.layout.fragment_personalization_dummy, container, false);
}
setupUnitClickListener(unitView, boundaryView);
setupInputFields(boundaryView);
return rootView;
}
private void setupInputFields(View view) {
view.findViewById(R.id.fragment_personalization_mmol_low_picker).setVisibility(View.VISIBLE);
view.findViewById(R.id.fragment_personalization_mgdl_low_picker).setVisibility(View.VISIBLE);
if(unitOfMeasurement==1) {
view.findViewById(R.id.fragment_personalization_mmol_low_picker).setVisibility(View.GONE);
System.out.println("mmol gone");
} else if(unitOfMeasurement==0) {
view.findViewById(R.id.fragment_personalization_mgdl_low_picker).setVisibility(View.GONE);
System.out.println("mgdl gone");
}
/*switch(unitOfMeasurement) {
case 0: //it's mmol/l
view.findViewById(R.id.fragment_personalization_mgdl_low_picker).setVisibility(View.VISIBLE);
final EditText val1 = (EditText) view.findViewById(R.id.fragment_personalization_low_mmol_value1);
final EditText val2 = (EditText) view.findViewById(R.id.fragment_personalization_low_mmol_value2);
System.out.println("testtt");
break;
case 1: //it's mg/dl
LinearLayout lin = (LinearLayout) view.findViewById(R.id.fragment_personalization_mgdl_low_picker);
lin.setVisibility(View.VISIBLE);
final EditText val3 = (EditText) view.findViewById(R.id.fragment_personalization_low_mgdl_value1);
System.out.println("gettinasd here"+unitOfMeasurement);
break;
}*/
}
private void setupUnitClickListener(View view, final View boundaryView) {
final TextView mmoll = (TextView) view.findViewById(R.id.fragment_personalization_unit_option1);
final TextView mgdl = (TextView) view.findViewById(R.id.fragment_personalization_unit_option2);
if(unitOfMeasurement==0) { //using mmol
mmoll.setTextColor(Color.parseColor("#920d0a"));
mmoll.setBackgroundColor(Color.parseColor("#44525252"));
mgdl.setTextColor(Color.parseColor("#525252"));
mgdl.setBackgroundColor(Color.parseColor("#ffffffff"));
} else { //using mg/dl
mgdl.setTextColor(Color.parseColor("#920d0a"));
mgdl.setBackgroundColor(Color.parseColor("#44525252"));
mmoll.setTextColor(Color.parseColor("#525252"));
mmoll.setBackgroundColor(Color.parseColor("#ffffffff"));
}
mmoll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mmoll.setTextColor(Color.parseColor("#920d0a"));
mmoll.setBackgroundColor(Color.parseColor("#44525252"));
mgdl.setTextColor(Color.parseColor("#525252"));
mgdl.setBackgroundColor(Color.parseColor("#ffffffff"));
unitOfMeasurement = 0;
}
});
mgdl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mgdl.setTextColor(Color.parseColor("#920d0a"));
mgdl.setBackgroundColor(Color.parseColor("#44525252"));
mmoll.setTextColor(Color.parseColor("#525252"));
mmoll.setBackgroundColor(Color.parseColor("#ffffffff"));
unitOfMeasurement = 1;
}
});
}
}
对于凌乱的代码感到抱歉,我也尝试了另一种方式(设置所有可见性,然后显示一个)。第一次通话后,一切都不再有用了。
编辑:添加了整个代码。所有这些,除了虚拟类都是由Android SDK创建的。