我有以下片段:
public class ChooseAssigmentFragment extends Fragment {
private ListArrayAdapter listArrayAdapter;
private Assigments assigments;
private ListView assigList;
private Assigment assigment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.assigments, container, false);
init(v);
assigments = new Assigments();
((MainFragmentActivity) getActivity()).getConfigAssigments();
loadAssigments(assigments);
return v;
}
private void init(View v) {
// TODO Auto-generated method stub
final EditText assigSearchET = (EditText) v.findViewById(R.id.AssigSearch);
final ImageButton assigSearchIB = (ImageButton) v.findViewById(R.id.AssigSB);
assigSearchET.setVisibility(View.VISIBLE);
assigSearchIB.setVisibility(View.VISIBLE);
assigList = (ListView) v.findViewById(R.id.assigListView);
alert();
assigList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> assigList, View v, int position, long id) {
Assigment assigment = ((ListArrayAdapter) assigList.getAdapter()).getItem(position);
LogService.log(getTag(), "position: " + position);
assigSearchET.setVisibility(View.GONE);
assigSearchIB.setVisibility(View.GONE);
((MainFragmentActivity) getActivity()).setConfigAssig(position, assigment);
}
});
OnClickListener searchListener = new OnClickListener() {
public void onClick(View v) {
LogService.log(getTag(), "Search Pressed");
}
};
assigSearchIB.setOnClickListener(searchListener);
}
public void setAssigments(Assigments assigments) {
this.assigments = assigments;
}
public void loadAssigments(Assigments assigments) {
System.out.println("assigments"+assigments );
this.assigments = assigments;
listArrayAdapter = new ListArrayAdapter(ChooseAssigmentFragment.this, assigments);
assigList.setAdapter(listArrayAdapter);
}
public void alert() {
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.show();
dialog.dismiss();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("ChooseAssigmentFragment.onDestroy()");
super.onDestroy();
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
System.out.println("ChooseAssigmentFragment.onDestroyView()");
super.onDestroyView();
}
}
使用以下 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayoutAssig2"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/SlateGray"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:text="@string/sensor_conf" />
</RelativeLayout>
<EditText
android:id="@+id/AssigSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/RelativeLayoutAssig2"
android:layout_toLeftOf="@+id/AssigSB"
android:visibility="gone"
android:ems="10" >
</EditText>
<ListView
android:id="@+id/assigListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/AssigSearch" >
</ListView>
<ImageButton
android:id="@+id/AssigSB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/assigListView"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/AssigSearch"
android:background="@color/Transparent"
android:src="@drawable/ic_action_search"
android:visibility="gone" />
这是我的片段活动:
public class MainFragmentActivity extends MyBaseActivity implements OnClickListener {
private static final String TAG = MainFragmentActivity.class.getName();
private Fragment f;
RelativeLayout header, parentHeader;
Assigments assigments;
@Override
public void onBackPressed() {
Class<?> previousFragment = getPreviousScreen();
if (previousFragment != null) {
navigateTo(previousFragment, AnimEffect.RIGHT_TO_LEFT);
} else {
super.onBackPressed();
}
}
public Class<?> getPreviousScreen() {
if ((f instanceof MainFragment) && f.isAdded()) {
return null;
} else if ((f instanceof ChooseAssigmentFragment) && f.isAdded()) {
return MainFragment.class;
}
else if ((f instanceof ConfigFragment) && f.isAdded()) {
return ChooseAssigmentFragment.class;
}
else if ((f instanceof EditNetworkFragment) && f.isAdded()) {
return ConfigFragment.class;
}
return null;
}
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_fragment_container);
assigments = new Assigments();
header = (RelativeLayout) findViewById(R.id.mainFragmentContainerheader);
findViewById(R.id.notificationsButton).setOnClickListener(this);
parentHeader = (RelativeLayout) findViewById(R.id.mainFragmentHeader);
header.setOnClickListener(this);
navigateTo(MainFragment.class, AnimEffect.BOTTOM_TO_TOP);
}
public void hideHeader() {
parentHeader.setVisibility(View.GONE);
}
public void showHeader() {
parentHeader.setVisibility(View.VISIBLE);
}
public void navigateTo(Class<?> fragmentClass, AnimEffect effect) {
removeOldFragment(effect);
try {
f = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
LogService.err(TAG, e.getMessage(), e, LogService.LOG_LEVEL_LITE);
}
if (f != null) {
getSupportFragmentManager().beginTransaction().disallowAddToBackStack().setCustomAnimations(AnimEffect.getInAnimation(effect), 0).add(R.id.mainFragmentContainer, f, fragmentClass.getSimpleName()).commitAllowingStateLoss();
}
}
private void removeOldFragment(AnimEffect effect) {
if ((f != null) && f.isAdded()) {
System.out.println("removing: " + f.getClass().getSimpleName());
getSupportFragmentManager().beginTransaction().setCustomAnimations(0, AnimEffect.getOutAnimation(effect)).remove(f).commitAllowingStateLoss();
getSupportFragmentManager().executePendingTransactions();
}
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.mainFragmentContainerheader:
if (f.getClass() != MainFragment.class) {
navigateTo(MainFragment.class, AnimEffect.RIGHT_TO_LEFT);
}
break;
case R.id.notificationsButton:
View x =getLayoutInflater().inflate(R.layout.help_fragment, null);
((RelativeLayout)findViewById(R.id.mainFragmentContainerx)).addView(x, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
Animation a=AnimationUtils.loadAnimation(this, R.anim.push_left_in);
a.setFillAfter(true);
x.startAnimation(a);
break;
default:
break;
}
// TODO Auto-generated method stub
}
public void setConfigAssig(int position, Assigment assigment) {
// TODO Auto-generated method stub
navigateTo(ConfigFragment.class, AnimEffect.RIGHT_TO_LEFT);
if ((f != null) && (f instanceof ConfigFragment)) {
((ConfigFragment) f).setAssigment(position, assigment);
}
}
public void setConfigAssigments(Assigments assigments) {
navigateTo(ChooseAssigmentFragment.class, AnimEffect.RIGHT_TO_LEFT);
this.assigments = assigments;
}
public void getConfigAssigments() {
if ((f != null) && (f instanceof ChooseAssigmentFragment)) {
((ChooseAssigmentFragment) f).setAssigments(assigments);
}
}
}
如您所见,我的 xml 中有一个名为 AssigSearch 的 EditText 和一个名为 AssigSB 的 imageButton。问题是,如果我更改我的片段(来自 MainFragmentActivity 的类 navigateTo),editText 和 imagebuttons 也会保留在其他页面上。我尝试从 ChooseAssigmentFragment 中删除与这两个有关的代码的每一部分,但它们仍然出现在其他片段上。如果我将它们放在 xml 代码中以显示:GONE,那么我将看不到它们。但我也不会在我的 xml 中看到它们。我尝试从我的片段以编程方式设置可见性,然后在我更改片段之前将其更改为 Gone,但仍然没有运气。试图在我的 onDestroyView 和 onDestroy 函数上调用 enable(false) ,仍然没有运气。任何想法我可以如何在我的片段上制作这个节目,而不是在其他人身上?