Android的另一个片段取代目前的片段 [英] Android replace the current fragment with another fragment
问题描述
我刚开始的片段设计的蜂窝。我创建了两个片段。当我点击左侧片段的按钮,一个新的片段在右侧创建。同时,当我单击右边的片段(即DetialsFragment在我下面的code按钮应该由另一个片段所取代。 main.xml中
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向>
<片段类=com.fragment.example.Titles
机器人:ID =@ + ID /标题机器人:layout_weight =1
机器人:layout_width =0px
机器人:layout_height =match_parent/>
<的FrameLayout机器人:ID =@ + ID /详细信息的android:layout_weight =1
机器人:layout_width =0px
机器人:layout_height =match_parent/>
< / LinearLayout中>
FragmentExample.java
公共类为FragmentExample延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
}
}
Titles.java
公共类标题扩展片段{
公共FragmentTransaction英尺;
@覆盖
公共查看onCreateView(LayoutInflater充气,
ViewGroup中的容器,包savedInstanceState){
视图V = inflater.inflate(R.layout.main1,NULL);
按钮按钮1 =(按钮)v.findViewById(R.id.button1);
button1.setText(santhosh);
button1.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
DetailsFragment细节=(DetailsFragment)
。getFragmentManager()findFragmentById(R.id.details);
如果(详细信息== NULL || details.getShownIndex()!= 1){
//使新的片段,显示此选项。
细节= DetailsFragment.newInstance(1);
//执行事务,替换现有
//片段,这个框架里面。
金融时报
= getFragmentManager()的BeginTransaction();
ft.add(R.id.details,细节,细节);
ft.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
});
返回伏;
}
}
DetailsFragment.java
公共类DetailsFragment扩展片段{
/ **
*创建DetailsFragment的新实例,初始化为
*显示在索引的文字。
* /
标题标题=新标题();
的String []标题= {标题1,标题2,TITLE3,TITLE4};
公共静态DetailsFragment的newInstance(INT指数){
DetailsFragment F =新DetailsFragment();
//供应指标输入作为参数。
捆绑的args =新包();
args.putInt(指数,指数);
f.setArguments(参数);
返回F;
}
公众诠释getShownIndex(){
返回getArguments()调用getInt(索引,0);
}
@覆盖
公共查看onCreateView(LayoutInflater充气,
ViewGroup中的容器,包savedInstanceState){
如果(集装箱== NULL){
//当前在无容器的布局,所以没有
//必要创建我们的观点。
返回null;
}
Button按钮=新按钮(getActivity());
button.setText(下一步);
button.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
}
});
返回按钮;
}
}
然后提供您的按钮显示和点击事件被解雇,你可以叫你的单击事件如下:
最后FragmentTransaction英尺= getFragmentManager()的BeginTransaction()。
ft.replace(R.id.details,新NewFragmentToReplace(),NewFragmentTag);
ft.commit();
如果你想回到DetailsFragment上单击后退确保您添加上述交易向后退堆栈,即
ft.addToBackStack(空);
还是我失去了一些东西?另外一些人认为,你的活动获得的单击事件的按钮,它有更换片段在详细信息窗格中的责任。
I just started with fragment design for HoneyComb. I created two fragments. When i click a button in the left side fragment, a new fragment is created in right side. Meanwhile when i click a button in the right fragment(ie. DetialsFragment in my code below should be replaced by another 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;
}
}
Then provided your button is showing and the click event is being fired you can call the following in your click event:
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();
and if you want to go back to the DetailsFragment on clicking back ensure you add the above transaction to the back stack, i.e.
ft.addToBackStack(null);
Or am I missing something? Alternatively some people suggest that your activity gets the click event for the button and it has responsibility for replacing the fragments in your details pane.
这篇关于Android的另一个片段取代目前的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!