更换片段:没有工作 [英] Replacing Fragment : Didn't work
问题描述
我在片段
A 按钮
。如果按钮
点击它与其他片段$ C $来替换
片段
C>。在片段
activity_main.xml中是静态创建。
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#EFEFEF
机器人:方向=垂直
工具:上下文=MainActivity。><的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
机器人:weightSum =3> <按钮
机器人:ID =@ + ID / b民政
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =50dp
机器人:layout_weight =1
机器人:背景=#000000
机器人:填充=3DP
机器人:文字=家
机器人:文字颜色=#FFFFFF/> <按钮
机器人:ID =@ + ID / bEvents
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =50dp
机器人:layout_weight =1
机器人:背景=#f8bd0e
机器人:填充=3DP
机器人:文本=活动/> <按钮
机器人:ID =@ + ID / bNearby
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =50dp
机器人:layout_weight =1
机器人:背景=#f8bd0e
机器人:填充=3DP
机器人:文字=周边/>
< / LinearLayout中><的LinearLayout
机器人:ID =@ + ID / llFragment
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:方向=横向> <片段
机器人:ID =@ + ID / fHome
机器人:名字=com.example.fragmentstack.Home
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_margin =10dp/> <片段
机器人:ID =@ + ID / fEvents
机器人:名字=com.example.fragmentstack.Events
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_margin =10dp/> <片段
机器人:ID =@ + ID / fNear
机器人:名字=com.example.fragmentstack.NearBy
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_margin =10dp/> <的FrameLayout
机器人:ID =@ + ID / fragment_container
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
< /&的FrameLayout GT;
< / LinearLayout中>
activity_main.xml中的输出是
主要活动:
公共类MainActivity扩展FragmentActivity实现OnClickListener {
按钮家庭,事件,附近;
片段F1,F2,F3,F4;
FragmentManager经理;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
家=(按钮)findViewById(R.id.bHome);
事件=(按钮)findViewById(R.id.bEvents);
近=(按钮)findViewById(R.id.bNearby); home.setOnClickListener(本);
events.setOnClickListener(本);
near.setOnClickListener(本); 经理= getSupportFragmentManager();
F1 = manager.findFragmentById(R.id.fHome);
F2 = manager.findFragmentById(R.id.fEvents);
F3 = manager.findFragmentById(R.id.fNear);
}@覆盖
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.bHome:
如果(home.is pressed()){
home.setBackgroundColor(Color.BLACK);
home.setTextColor(Color.WHITE);
events.setBackgroundResource(R.color.Yellow);
near.setBackgroundResource(R.color.Yellow);
events.setTextColor(Color.BLACK);
near.setTextColor(Color.BLACK);
FragmentTransaction交易= getSupportFragmentManager()
.beginTransaction(); transaction.hide(F2);
transaction.hide(F3);
transaction.show(F1);
器transaction.commit();
}
打破;
案例R.id.bEvents:
如果(events.is pressed()){
events.setBackgroundColor(Color.BLACK);
events.setTextColor(Color.WHITE);
home.setBackgroundResource(R.color.Yellow);
near.setBackgroundResource(R.color.Yellow);
home.setTextColor(Color.BLACK);
near.setTextColor(Color.BLACK);
FragmentTransaction为Transaction1 = getSupportFragmentManager()
.beginTransaction();
transaction1.hide(F1);
transaction1.hide(F3);
transaction1.show(F2);
transaction1.commit();
}
打破;
案例R.id.bNearby:
如果(near.is pressed()){
near.setBackgroundColor(Color.BLACK);
near.setTextColor(Color.WHITE);
home.setBackgroundResource(R.color.Yellow);
events.setBackgroundResource(R.color.Yellow);
home.setTextColor(Color.BLACK);
events.setTextColor(Color.BLACK);
FragmentTransaction transaction2 = getSupportFragmentManager()
.beginTransaction();
transaction2.hide(F1);
transaction2.hide(F2);
transaction2.show(F3);
transaction2.commit();
}
打破;
}
} }
现在首页
扩展片段
。家里的XML布局有一个按钮
和一个的TextView
。在这里onClicking扣我更换首页片段
更新配置文件片段
。
公共类家庭扩展片段{
按钮BTN;
查看视图。
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根
鉴于= inflater.inflate(R.layout.home,集装箱,FALSE);
在里面(); 返回视图。
}私人无效的init(){
BTN =(按钮)view.findViewById(R.id.bUpdate);
btn.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
片段FG =新UpdateProfile();
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,FG);
fragmentTransaction.addToBackStack(NULL);
fragmentTransaction.commit();
}
});} }
R.id.fragment_container
是activity_main.xml中present。现在我面临的问题是点击它不与新片段替换片段按钮时。
现在我面临的问题是点击按钮,它是不是当
用新片段替换片段。
块引用>本次交易不会发生,但你的片段添加到将被排挤出屏幕的容器是不可见的(所有布局的片段都设置宽度为
match_parent
所以他们将填补的LinearLayout
的宽度和的FrameLayout
将被推出屏幕)。
还你的,不持有previous碎片的容器上替代交易,这意味着旧的碎片仍然会在屏幕上看到(加上新增加的片段)。系统设置,你是不是来处理您的方案的正确方法。从图片看来你有一个标签一样的布局,在这种情况下,你只能有一个容器中,所有的碎片将驻留(避免显示/隐藏适当的片段的额外工作,并提供一个体面的返回按钮体验)。
其次,如果你去上班(做交易)与那些片段,你真的应该添加它们作为编程更换不带静电的碎片顺利。有很多的教程在那里就如何使一个标签像碎片布局。
I have a
Button
inFragment
. IfButton
is clicked it has to replace theFragment
with anotherFragment
. TheFragments
in activity_main.xml is created statically.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#efefef" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="3" > <Button android:id="@+id/bHome" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1" android:background="#000000" android:padding="3dp" android:text="Home" android:textColor="#ffffff" /> <Button android:id="@+id/bEvents" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1" android:background="#f8bd0e" android:padding="3dp" android:text="Events" /> <Button android:id="@+id/bNearby" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1" android:background="#f8bd0e" android:padding="3dp" android:text="Nearby" /> </LinearLayout> <LinearLayout android:id="@+id/llFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" > <fragment android:id="@+id/fHome" android:name="com.example.fragmentstack.Home" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" /> <fragment android:id="@+id/fEvents" android:name="com.example.fragmentstack.Events" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" /> <fragment android:id="@+id/fNear" android:name="com.example.fragmentstack.NearBy" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" /> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="wrap_content" > </FrameLayout> </LinearLayout>
The output of activity_main.xml is
Main Activity:
public class MainActivity extends FragmentActivity implements OnClickListener { Button home, events, near; Fragment f1, f2, f3, f4; FragmentManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); home = (Button) findViewById(R.id.bHome); events = (Button) findViewById(R.id.bEvents); near = (Button) findViewById(R.id.bNearby); home.setOnClickListener(this); events.setOnClickListener(this); near.setOnClickListener(this); manager = getSupportFragmentManager(); f1 = manager.findFragmentById(R.id.fHome); f2 = manager.findFragmentById(R.id.fEvents); f3 = manager.findFragmentById(R.id.fNear); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bHome: if (home.isPressed()) { home.setBackgroundColor(Color.BLACK); home.setTextColor(Color.WHITE); events.setBackgroundResource(R.color.Yellow); near.setBackgroundResource(R.color.Yellow); events.setTextColor(Color.BLACK); near.setTextColor(Color.BLACK); FragmentTransaction transaction = getSupportFragmentManager() .beginTransaction(); transaction.hide(f2); transaction.hide(f3); transaction.show(f1); transaction.commit(); } break; case R.id.bEvents: if (events.isPressed()) { events.setBackgroundColor(Color.BLACK); events.setTextColor(Color.WHITE); home.setBackgroundResource(R.color.Yellow); near.setBackgroundResource(R.color.Yellow); home.setTextColor(Color.BLACK); near.setTextColor(Color.BLACK); FragmentTransaction transaction1 = getSupportFragmentManager() .beginTransaction(); transaction1.hide(f1); transaction1.hide(f3); transaction1.show(f2); transaction1.commit(); } break; case R.id.bNearby: if (near.isPressed()) { near.setBackgroundColor(Color.BLACK); near.setTextColor(Color.WHITE); home.setBackgroundResource(R.color.Yellow); events.setBackgroundResource(R.color.Yellow); home.setTextColor(Color.BLACK); events.setTextColor(Color.BLACK); FragmentTransaction transaction2 = getSupportFragmentManager() .beginTransaction(); transaction2.hide(f1); transaction2.hide(f2); transaction2.show(f3); transaction2.commit(); } break; } } }
Now
Home
extendsFragment
. the xml layout of home has oneButton
and oneTextView
.Here onClicking Button I'm replacing theHome Fragment
with newUpdate Profile Fragment
.public class Home extends Fragment { Button btn; View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.home, container, false); init(); return view; } private void init() { btn = (Button) view.findViewById(R.id.bUpdate); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment fg = new UpdateProfile(); FragmentTransaction fragmentTransaction = getFragmentManager() .beginTransaction(); fragmentTransaction.replace(R.id.fragment_container, fg); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); } }
R.id.fragment_container
is present in activity_main.xml. Now the problem I'm facing is when clicking the button it is not replacing the fragment with new Fragment.解决方案Now the problem I'm facing is when clicking the button it is not replacing the fragment with new Fragment.
The transaction does occur but it's not visible as you add that fragment to a container that will be pushed out of the screen(all of your layout's fragments have width set to
match_parent
so they will fill theLinearLayout
's width and theFrameLayout
will be pushed out of the screen). Also you do the replace transaction on a container that doesn't hold the previous fragments, this means that the old fragments will still be visible on the screen(plus the new added fragment).The system you setup is not the proper way to handle your scenario. From the images it seems you have a tab like layout and in this case you should have only one container in which all of your fragments will reside(to avoid the extra work of showing/hiding the proper fragment and also provide a decent BACK button experience).
Secondly if you're going to work(do transactions) with those fragments you should really add them programmatically as replace will not go well with a static fragments. There are a lot of tutorials out there on how to make a tab like layout with fragments.
这篇关于更换片段:没有工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!