Dynamic addition of Fragment
Show results
The dynamic loading of fragments is mainly divided into 5 steps
1. Create a shard instance.
2. Get FragmentManager.
3. Start the transaction.
4. Add fragments to the container.
5. Commit the transaction.
code demo
In this example, the left side is static loading, and the right side is dynamic loading
Show static loading on the left
left_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent"> <Button android:onClick="anotherRight" android:id="@+id/button" android:layout_gravity="center_horizontal" android:text="This is button in left_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
LeftFragment.class
public class LeftFragment extends Fragment { // Rewrite onCreateView method @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // The data type here is View , so just return inflater to return inflater.inflate(R.layout.left_fragment , container, false ); } }
Dynamic addition on the right
another_right_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary"> < TextView android :id= "@+id/textview" android :layout_gravity= "center_horizontal" android :text= " This is a dynamically added fragment " android :layout_width= "wrap_content" android :layout_height= "wrap_content" /> </LinearLayout>
AnotherRightFragment.class
public class AnotherRightFragment extends Fragment { // Override the onCreateView method @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // The data type here is View , so return the inflater directly inflater.inflate (R.layout.another_right_fragment,container, false ); } }also need to be rewritten
just changed in the main activity
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <!--注意,fragment必须要有id,否则无法编译--> <fragment android:id="@+id/left_fragment" android:name="com.loser.fragmenttext.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <!--注意,这里不是fragment,是用来填充的一个布局--> <FrameLayout android:id="@+id/right_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0" /> </LinearLayout>
最主要的代码,在主活动中实现碎片的动态加载
MainActivity.class
public class MainActivity extends AppCompatActivity { FragmentManager fragmentManager; FragmentTransaction fragmentTransaction; FrameLayout frameLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // 左边碎片中的按钮绑定的事件 public void anotherRight(View v){ // FragmentManager fragmentManager = getSupportFragmentManager(); // 通过begin开启事务 fragmentTransaction = fragmentManager.beginTransaction(); // 使用replace向容器内添加碎片 fragmentTransaction.replace(R.id.right_layout,new AnotherRightFragment()); // 将事务添加到返回栈中 fragmentTransaction.addToBackStack(null); // 拿到FrameLayout以便在设置其大小 frameLayout = (FrameLayout)findViewById(R.id.right_layout); // 重新设置右碎片的布局 frameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,MATCH_PARENT, 2.0f)); // 提交事务 fragmentTransaction.commit(); // 土司一下,证明你点击有效 Toast.makeText(MainActivity.this,"你点击了按钮",Toast.LENGTH_SHORT).show(); } }
此处
// 将事务添加到返回栈中 fragmentTransaction.addToBackStack(null);
是当用户按下Back后就直接退出活动了,而如上设置为null则返回到上一个碎片。
至此,动态添加碎片的基本方法已经结束。