|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 alltolove 于 2017-10-3 09:34 编辑
我们既要碎片化学习也要碎片化编程,碎片化编程手机软件是今年来非常流行的,比如制作页签换页效果等。今天的动态添加碎片是以后比较常用的方法。我们继续昨天的项目,先在layout目录下新建another_right_fragment.xml文件:- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/another"
- android:textSize="20sp"
- android:layout_gravity="center_horizontal"
- android:text="This is another fragment"/>
- </LinearLayout>
复制代码
然后把这个布局文件制作成碎片,在mainactivity.java旁边新建一个AnotherRightFragment.java类- package com.example.xinwei.fragmentactivity;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * Created by xinwei on 2017/10/3.
- */
- public class AnotherRightFragment extends Fragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.another_right_fragment,container,false);
- return view;
- }
- }
复制代码
然后,换一个可以被替换的控件,修改activity_main.xml文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <fragment
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:id="@+id/left_fragment"
- android:name="com.example.xinwei.fragmentactivity.LeftFragment" />
- <FrameLayout
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:id="@+id/right_layout"
- android:layout_weight="1" />
- </LinearLayout>
复制代码
修改mainactivity.java- package com.example.xinwei.fragmentactivity;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button button = (Button)findViewById(R.id.button);
- button.setOnClickListener(this);
- replaceFragment(new RightFragment());
- }
- private void replaceFragment(Fragment fragment) {
- FragmentManager fragmentManager=getSupportFragmentManager();
- FragmentTransaction transition=fragmentManager.beginTransaction();
- transition.replace(R.id.right_layout,fragment);
- transition.commit();
- }
- @Override
- public void onClick(View view) {
- replaceFragment(new AnotherRightFragment());
- }
- }
复制代码
效果图:
|
-
|