gpt4 book ai didi

Android用另一个 fragment 替换当前 fragment

转载 作者:IT王子 更新时间:2023-10-28 23:51:39 24 4
gpt4 key购买 nike

我刚开始为 HoneyComb 设计 fragment 。我创建了两个 fragment 。当我单击左侧 fragment 中的按钮时,会在右侧创建一个新 fragment 。同时,当我单击右侧 fragment 中的按钮时(即,我下面代码中的 DetialsFragment 应该被另一个 fragment 替换。main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment class="com.fragment.example.Titles"
android:id="@+id/titles" android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<FrameLayout android:id="@+id/details" android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />

</LinearLayout>

FragmentExample.java

public class FragmentExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Titles.java

public class Titles extends Fragment {
public FragmentTransaction ft;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main1, null);
Button button1 = (Button)v.findViewById(R.id.button1);
button1.setText("santhosh");
button1.setOnClickListener(new OnClickListener() {



@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DetailsFragment details = (DetailsFragment)
getFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != 1) {
// Make new fragment to show this selection.
details = DetailsFragment.newInstance(1);

// Execute a transaction, replacing any existing
// fragment with this one inside the frame.
ft
= getFragmentManager().beginTransaction();
ft.add(R.id.details, details, "detail");
ft.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}

});
return v;
}
}

DetailsFragment.java

public class DetailsFragment extends Fragment {
/**
* Create a new instance of DetailsFragment, initialized to
* show the text at 'index'.
*/
Titles title = new Titles();
String[] titles = {"Title1", "Title2", "Title3", "Title4"};
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();

// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);

return f;
}

public int getShownIndex() {
return getArguments().getInt("index", 0);
}

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
// Currently in a layout without a container, so no
// reason to create our view.
return null;
}
Button button = new Button(getActivity());
button.setText("Next");
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return button;
}
}

最佳答案

然后,如果您的按钮正在显示并且点击事件被触发,您可以在您的点击事件中调用以下内容:

final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();

如果您想在单击返回时返回 DetailsFragment,请确保将上述事务添加到后台堆栈,即

ft.addToBackStack(null);

或者我错过了什么?或者,有些人建议您的 Activity 获取按钮的单击事件,并负责替换详细信息 Pane 中的 fragment 。

关于Android用另一个 fragment 替换当前 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8163104/

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