gpt4 book ai didi

android - 无法正确交换操作栏选项卡上的 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:26 33 4
gpt4 key购买 nike

已阅读关于此主题的所有问题/答案,但我无法让其中任何一个为我工作。aggghh

我有 3 个操作栏标签

tab1 调用/使用动态显示“添加”按钮的 fragment

当按钮被点击时,我希望它自己删除“按钮 fragment ”并用一个有一堆 EditTexts 的 fragment 替换它

这按预期工作。但是,当我单击另一个选项卡时,例如调用自己 fragment 的 tab2,来自 tab1 的 EditTexts fragment 仍然存在并覆盖 tab2 fragment

我想明白我正在尝试做的事情从根本上是错误的,一个 fragment 不能调用另一个 fragment 来替换自己?或者不知何故我没有正确删除以前的 fragment 和与 backstack 有关的东西..????或显示和隐藏 xml?????

我对此很陌生,完全困惑!请帮忙...提前谢谢你:)

这是我的大部分代码取自 android 站点 http://developer.android.com/training/backward-compatible-ui/index.html使用 TabCompat.zip 示例

MainActivity.java

package com.example.android.tabcompat;
public class MainActivity extends TabCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TabHelper tabHelper = getTabHelper();

CompatTab tab1Tab = tabHelper.newTab("tab1")
.setText(R.string.tab_tab1)
.setTabListener(new InstantiatingTabListener(this,"A", Tab1Fragment.class));
tabHelper.addTab(tab1Tab);

CompatTab tab2Tab = tabHelper.newTab("tab2")
.setText(R.string.tab_tab2)
.setTabListener(new InstantiatingTabListener(this, "B", Tab2Fragment.class));
tabHelper.addTab(tab2Tab);

CompatTab tab3Tab = tabHelper.newTab("tab3")
.setText(R.string.tab_tab3)
.setTabListener(new InstantiatingTabListener(this,"C", Tab3Fragment.class));
tabHelper.addTab(tab3Tab);
}

/**
* Implementation of {@link CompatTabListener} to handle tab change events. This implementation
* instantiates the specified fragment class with no arguments when its tab is selected.
*/
public static class InstantiatingTabListener implements CompatTabListener {

private final TabCompatActivity mActivity;
private final Class mClass;


/**
* Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param cls The class representing the fragment to instantiate
*/
public InstantiatingTabListener(TabCompatActivity activity, String tag, Class<? extends Fragment> cls) {
mActivity = activity;
mClass = cls;

}

/* The following are each of the ActionBar.TabListener Callback */
@Override
public void onTabSelected(CompatTab tab, FragmentTransaction ft) {
// Check if the fragment is already initialised
Fragment fragment = tab.getFragment();
if (fragment == null) {
// If not, instantiate and add it to the activity
fragment = Fragment.instantiate(mActivity, mClass.getName());
tab.setFragment(fragment);
ft.add(android.R.id.tabcontent, fragment, tab.getTag());
}
else {
// If it exists, simply attach it in order to show it
ft.attach(fragment);
}
}


public void onTabUnselected(CompatTab tab, FragmentTransaction ft) {
Fragment fragment = tab.getFragment();
if (fragment != null) {
ft.detach(fragment);
}
}

@Override
public void onTabReselected(CompatTab tab, FragmentTransaction ft) {
// User selected the already selected tab. Do nothing.

} }
}

Tab1Fragment.java 这是加载“添加按钮”的地方

package com.example.android.tabcompat;

public class Tab1Fragment extends Fragment{

protected int position;
//display the add tab1 button
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Button btn = new Button (getActivity());
btn.setGravity(Gravity.TOP);
btn.setText(R.string.btn_tab1);
btn.setId(1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//when button is clicked replace the button and display the tab1 form fragment


FragmentManager fragmentManager = getFragmentManager();
Tab1FormFragment formFragment = new Tab1FormFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(android.R.id.tabcontent, formFragment);
transaction.addToBackStack(null);
transaction.commit();
}

});
return btn;

}
}

Tab1FormFragment.java 此 fragment 显示一种从布局文件 form.xml 中获取的编辑 TextView

package com.example.android.tabcompat;



public class Tab1FormFragment extends Fragment {
protected static final String ARG_POSITION = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.form, container, false);

return myFragmentView;
}

}

form.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/petForm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_tab_photos" />

<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/field_name"
android:inputType="text" />


<EditText
android:id="@+id/editTextAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/field_age"
android:inputType="text" />
<EditText
android:id="@+id/editTextBreed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/field_breed"
android:inputType="text" />

<Spinner
android:id="@+id/spinnerSex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/sex_arrays"
android:hint="@string/field_sex" />

<Button
android:id="@+id/savePet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save_record" />
</LinearLayout>`

最佳答案

您可以尝试调用非操作栏选项卡(您说的那个有一堆 EditText)并为该 fragment 实例提供一个标记,即在您的动态按钮的 onClick 方法中,在启动您的 Tab1formFragment 之后添加此行:

getFragmentManager().beginTransaction().replace(android.R.id.tabcontent, formFragment, "FORM_FRAG").addToBackStack(null).commit();

并通过添加对 formFragment 实例的检查来更改操作栏选项卡事务:if(getFragmentManager().findFragmentByTag("FORM_FRAG")!=null)getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentByTag("FORM_FRAG")).commit()

此外,您可以在 onTabSelected() 方法中使用 replace() fragment 事务,而不是 attach() 和 add()。

关于android - 无法正确交换操作栏选项卡上的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12966956/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com