美文网首页
Android Fragment

Android Fragment

作者: 百里漫步 | 来源:发表于2017-10-21 14:29 被阅读0次

    首先有一个底部导航,用RadioGroup和RadioButton实现
    这里主要写静态和动态加载的fragment
    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    
    
    
        <RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >
    
            <RadioButton
                android:id="@+id/first"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/radio_pressed"
                android:button="@null"
                android:drawableTop="@drawable/ic_launcher"
                android:gravity="center_horizontal"
                android:text="静态加载" />
    
            <RadioButton
                android:id="@+id/second"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/radio_pressed"
                android:button="@null"
                android:drawableTop="@drawable/ic_launcher"
                android:gravity="center_horizontal"
                android:text="动态加载" />
    
            <RadioButton
                android:id="@+id/thrid"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/radio_pressed"
                android:button="@null"
                android:drawableTop="@drawable/ic_launcher"
                android:gravity="center_horizontal"
                android:text="生命周期" />
    
            <RadioButton
                android:id="@+id/fourth"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/radio_pressed"
                android:button="@null"
                android:drawableTop="@drawable/ic_launcher"
                android:gravity="center_horizontal"
                android:text="传值通信" />
        </RadioGroup>
    
    </RelativeLayout>
    

    MainActivity.java

    package com.example.android_fragment;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    
    public class MainActivity extends Activity implements OnCheckedChangeListener
     {
    
        private RadioGroup group;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            group = (RadioGroup) findViewById(R.id.radiogroup);
            group.setOnCheckedChangeListener(this);
    
        }
    
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
    
            switch (checkedId) {
            case R.id.first: {
                //静态添加fragment
                Intent intent=new Intent(this,MainActivity2.class);
                startActivity(intent);
                break;
    
            }
            case R.id.second: {
                //动态添加fragment
                MyFragment2 fragment2=new MyFragment2();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                beginTransaction.add(R.id.frame, fragment2);
                beginTransaction.addToBackStack(null);
                beginTransaction.commit();
                break;
            }
            case R.id.thrid: {
                Intent intent=new Intent(MainActivity.this,MainActivity3.class);
                startActivity(intent);
    
                break;
            }
            case R.id.fourth: {
                Intent intent=new Intent(MainActivity.this,MainActivity4.class);
                startActivity(intent);
    
                break;
             }
            }
        }
    }
    
    

    MyFragment.java

    package com.example.android_fragment;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MyFragment extends Fragment{
        
        private String aaa;
        
        
        public String getAaa() {
            return aaa;
        }
    
    
        public void setAaa(String aaa) {
            this.aaa = aaa;
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            //layout布局文件转换成View对象
            /**
             * resource:Fragment需要加载的布局文件
             * root:加载layout的父ViewGroup
             * attactToRoot:false,不返回父ViewGroup
             */
            View view = inflater.inflate(R.layout.fragment, container, false);
            TextView text=(TextView) view.findViewById(R.id.text);
            Button button=(Button) view.findViewById(R.id.button);
            text.setText("静态加载Fragment");
            button.setText("获取内容");
            button.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String value = getAaa();
                    Toast.makeText(getActivity(), "value="+value, Toast.LENGTH_SHORT).show();
                }
            });
            return view;
        }
    
    }
    
    

    MyFragment2.java

    package com.example.android_fragment;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    public class MyFragment2 extends Fragment{
        
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            //layout布局文件转换成View对象
            
            /**
             * resource:Fragment需要加载的布局文件
             * root:加载layout的父ViewGroup
             * attactToRoot:false,不返回父ViewGroup
             */
            View view = inflater.inflate(R.layout.fragment, container, false);
            TextView text=(TextView) view.findViewById(R.id.text);
            text.setText("动态加载Fragment");
            return view;
        
        }
    
    }
    
    

    main2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <fragment
            android:id="@+id/fragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:name="com.example.android_fragment.MyFragment"
            />
    
    </LinearLayout>
    
    

    MainActivity2.java

    package com.example.android_fragment;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity2 extends Activity{
    
        
        private TextView tv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main2);
            Button button=(Button) findViewById(R.id.button);
            tv=(TextView) findViewById(R.id.text);
            button.setText("改变");
            button.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    tv.setText("TextView改变了");
                }
            });
            
        }
    }
    

    radio_press.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@color/gray" android:state_checked="true"></item>
        <item android:drawable="@color/white" android:state_pressed="true"></item>
        <item android:drawable="@color/white"></item>
    
    </selector>
    

    下面是页面传值
    main4.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送" />
        
        <fragment 
            android:id="@+id/frag"
            android:name="com.example.android_fragment.MyFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    
    </LinearLayout>
    

    MainActivity4.java

    package com.example.android_fragment;
    
    import com.example.android_fragment.MyFragment5.MyListener;
    
    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity4 extends Activity implements MyListener {
    
        private EditText editext;
        private Button send;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main4);
            editext = (EditText) findViewById(R.id.editText);
            send = (Button) findViewById(R.id.send);
            
            
            send.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String text = editext.getText().toString();
                    MyFragment5 fragment5 = new MyFragment5();
                    Bundle bundle = new Bundle();
                    bundle.putString("name", text);
                    fragment5.setArguments(bundle);
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction beginTransaction = fragmentManager
                            .beginTransaction();
                    beginTransaction.add(R.id.layout, fragment5, "fragment5");
                    beginTransaction.commit();
                    Toast.makeText(MainActivity4.this, "向Fragment发送数据" + text,
                            Toast.LENGTH_SHORT).show();
                }
            }); 
            
            FragmentManager fragmentManager = getFragmentManager();
            Fragment findFragmentById = fragmentManager.findFragmentById(R.id.frag);
            MyFragment frag=(MyFragment) findFragmentById;
            frag.setAaa("fragment静态传值");
        }
    
        @Override
        public void thank(String code) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity4.this, "已成功接收到" + code + ",客气了!",
                    Toast.LENGTH_SHORT).show();
        }
    
    }
    
    

    MyFragment5.java

    package com.example.android_fragment;
    
    import android.app.Activity;
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MyFragment5 extends Fragment{
    
        private String code="Thank you,Activity!";
        
        public MyListener listener;
        public interface MyListener
        {
            public void thank(String code);
        }
        
        @Override
        public void onAttach(Activity activity) {
            // TODO Auto-generated method stub  
            listener=(MyListener) activity;
            super.onAttach(activity);
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View view = inflater.inflate(R.layout.fragment2, container, false);
            TextView tv = (TextView) view.findViewById(R.id.text);
            String text=getArguments().get("name")+"";
            tv.setText(text);
            Toast.makeText(getActivity(), "已成功接收到"+text, Toast.LENGTH_SHORT).show();
            Toast.makeText(getActivity(), "向Activity发送"+code, Toast.LENGTH_SHORT).show();
            listener.thank(code);
            return view;
        }
    }
    
    

    效果演示:


    fragment.gif

    相关文章

      网友评论

          本文标题:Android Fragment

          本文链接:https://www.haomeiwen.com/subject/ldliuxtx.html