• CriminalIntent项目开发笔记(一)----动态添加Fragment


    1.创建Crime实体类

    package com.criminalintent;
    
    import java.util.UUID;
    
    /**
     * Created by yeeson on 2015/11/24.
     */
    public class Crime {
        private UUID uuid;
        private String title;
        public Crime(){
            uuid=UUID.randomUUID();
        }
    
        public UUID getUuid() {
            return uuid;
        }
    
        public String getTitle() {
    
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    }
    Crime

    2.定义容器视图activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    </FrameLayout>
    activity_main

    3.定义CrimeFragment的布局视图

    <?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">
        <EditText
            android:id="@+id/crime_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/crime_title_hint"/>
    </LinearLayout>
    fragment_crime

    4.创建CrimeFragment类

    package com.criminalintent;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    /**
     * Created by yeeson on 2015/11/24.
     */
    public class CrimeFragment extends Fragment {
        private Crime crime;
        TextView titleFiled;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            crime=new Crime();
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment_crime,container,false);
            titleFiled= (TextView) view.findViewById(R.id.crime_title);
            titleFiled.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    crime.setTitle(charSequence.toString());
    
                }
    
                @Override
                public void afterTextChanged(Editable editable) {
    
                }
            });
            return view;
        }
    }
    CrimeFragment

    5.在MainActivity 中动态添加CrimeFragment

    package com.criminalintent;
    
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            FragmentManager fm=getSupportFragmentManager();
            Fragment fragment=fm.findFragmentById(R.id.fragmentContainer);
            if(fragment==null){
                fragment=new CrimeFragment();
                fm.beginTransaction()
                        .add(R.id.fragmentContainer, fragment)
                        .commit();
            }
    
        }
    }
    MainActivity

     最后在模拟其中运行效果如下:

  • 相关阅读:
    Jmeter断言设置
    jmeter设置自动启动时间
    jmeter用命令行运行jmx脚本
    jmeter多用户并发压力测试(导入文件)
    Fiddler导出接口抓包数据
    postman和postwoman
    UVa10561
    UVa11859
    Ferguson游戏
    UVa11916
  • 原文地址:https://www.cnblogs.com/liujufu/p/4991979.html
Copyright © 2020-2023  润新知