鱼C论坛

 找回密码
 立即注册
查看: 2741|回复: 0

[学习笔记] android编程4.2.2

[复制链接]
发表于 2017-10-3 09:32:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 alltolove 于 2017-10-3 09:34 编辑

我们既要碎片化学习也要碎片化编程,碎片化编程手机软件是今年来非常流行的,比如制作页签换页效果等。今天的动态添加碎片是以后比较常用的方法。我们继续昨天的项目,先在layout目录下新建another_right_fragment.xml文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent">
  5. <TextView
  6.     android:layout_width="wrap_content"
  7.     android:layout_height="wrap_content"
  8.     android:id="@+id/another"
  9.     android:textSize="20sp"
  10.     android:layout_gravity="center_horizontal"
  11.     android:text="This is another fragment"/>
  12. </LinearLayout>
复制代码

然后把这个布局文件制作成碎片,在mainactivity.java旁边新建一个AnotherRightFragment.java类
  1. package com.example.xinwei.fragmentactivity;

  2. import android.os.Bundle;
  3. import android.support.annotation.Nullable;
  4. import android.support.v4.app.Fragment;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;

  8. /**
  9. * Created by xinwei on 2017/10/3.
  10. */

  11. public class AnotherRightFragment extends Fragment {
  12.     @Nullable
  13.     @Override
  14.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  15.         View view = inflater.inflate(R.layout.another_right_fragment,container,false);
  16.         return view;
  17.     }
  18. }
复制代码

然后,换一个可以被替换的控件,修改activity_main.xml文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="horizontal" android:layout_width="match_parent"
  4.     android:layout_height="match_parent">

  5.     <fragment
  6.         android:layout_width="0dp"
  7.         android:layout_height="match_parent"
  8.         android:layout_weight="1"
  9.         android:id="@+id/left_fragment"
  10.         android:name="com.example.xinwei.fragmentactivity.LeftFragment" />

  11.     <FrameLayout
  12.         android:layout_width="0dp"
  13.         android:layout_height="match_parent"
  14.         android:id="@+id/right_layout"
  15.         android:layout_weight="1" />
  16. </LinearLayout>
复制代码

修改mainactivity.java
  1. package com.example.xinwei.fragmentactivity;

  2. import android.support.v4.app.Fragment;
  3. import android.support.v4.app.FragmentManager;
  4. import android.support.v4.app.FragmentTransaction;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;

  9. public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_main);
  14.         Button button = (Button)findViewById(R.id.button);
  15.         button.setOnClickListener(this);
  16.         replaceFragment(new RightFragment());
  17.     }

  18.     private void replaceFragment(Fragment fragment) {
  19.         FragmentManager fragmentManager=getSupportFragmentManager();
  20.         FragmentTransaction transition=fragmentManager.beginTransaction();
  21.         transition.replace(R.id.right_layout,fragment);
  22.         transition.commit();
  23.     }

  24.     @Override
  25.     public void onClick(View view) {
  26.         replaceFragment(new AnotherRightFragment());
  27.     }



  28. }
复制代码

效果图:
jdfw.gif
saassdas.png

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 16:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表