简单的Fragment切换

来源:互联网 发布:ccdd心电图数据库 编辑:程序博客网 时间:2024/04/29 16:56
package com.example.hongyan;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.LinearLayout;/** * @author HD * @date 2015-12-7 * @package_name com.example.hongyan * @file_name MainActivity.java */public class MainActivity extends Activity implements OnClickListener {    private LinearLayout mTabWeixin;    private LinearLayout mTabContact;    private LinearLayout mTabtongxun;    private LinearLayout mTabSet;    private ContentFrament mWeixin;    private FriendFragment mFriend;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);        mTabWeixin = (LinearLayout) findViewById(R.id.mTabWeixin);        mTabContact = (LinearLayout) findViewById(R.id.mTabContact);        mTabtongxun = (LinearLayout) findViewById(R.id.mTab_tongxun);        setDefaultFragment();        mTabSet = (LinearLayout) findViewById(R.id.mTab_set);        mTabWeixin.setOnClickListener(this);        mTabContact.setOnClickListener(this);    }    private void setDefaultFragment() {        FragmentManager fm = getFragmentManager();        FragmentTransaction transaction = fm.beginTransaction();        mWeixin = new ContentFrament();        transaction.replace(R.id.framLayout, mWeixin);        transaction.commit();    }    @Override    public void onClick(View v) {        // TODO 自动生成的方法存根        FragmentManager fm = getFragmentManager();        FragmentTransaction transaction = fm.beginTransaction();        switch (v.getId()) {        case R.id.mTabWeixin:            if(mWeixin == null){                mWeixin = new ContentFrament();            }            transaction.replace(R.id.framLayout, mWeixin);            break;        case R.id.mTabContact:            if( mFriend == null){                mFriend = new FriendFragment();            }            transaction.replace(R.id.framLayout, mFriend);            break;        }        transaction.commit();    }}
package com.example.hongyan;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class ContentFrament extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        // TODO 自动生成的方法存根        View view = inflater.inflate(R.layout.content, container, false);        return view;    }}
package com.example.hongyan;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FriendFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        // TODO 自动生成的方法存根        View view = inflater.inflate(R.layout.friend_fragment, container, false);        return view;    }}
package com.example.hongyan;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.webkit.WebView.FindListener;import android.widget.Button;import android.widget.Toast;public class TitileFragment extends Fragment {    private Button mButton;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        // TODO 自动生成的方法存根        View view = inflater.inflate(R.layout.title, container);        mButton = (Button) view.findViewById(R.id.button);        mButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // TODO 自动生成的方法存根                Toast.makeText(getActivity(), "title", Toast.LENGTH_SHORT).show();            }        });        return view;    }}

layout_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <fragment        android:id="@+id/titlt_fragment"        android:name="com.example.hongyan.TitileFragment"        android:layout_width="match_parent"        android:layout_height="45dp"        android:layout_alignParentTop="true" />    <include        android:id="@+id/bottomBar"        android:layout_width="match_parent"        android:layout_height="55dp"        android:layout_alignParentBottom="true"        layout="@layout/bottombar" />    <FrameLayout        android:id="@+id/framLayout"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@id/bottomBar"        android:layout_below="@id/titlt_fragment" >    </FrameLayout></RelativeLayout>

bottombar.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="horizontal"     android:background="#FF0000">    <LinearLayout         android:id="@+id/mTabWeixin"        android:layout_width="0dp"        android:layout_height="45dp"        android:layout_weight="1"        >        <Button             android:id="@+id/btn_weixing"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:text="微信"            android:visibility="gone"/>    </LinearLayout>    <LinearLayout         android:id="@+id/mTabContact"        android:layout_width="0dp"        android:layout_height="45dp"        android:layout_weight="1"        >        <Button             android:id="@+id/btn_Contact"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:text="联系人"            android:visibility="gone"/>    </LinearLayout>    <LinearLayout         android:id="@+id/mTab_tongxun"        android:layout_width="0dp"        android:layout_height="45dp"        android:layout_weight="1"        >        <Button             android:id="@+id/btn_tongxun"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:text="通讯录"            android:visibility="gone"/>    </LinearLayout>    <LinearLayout         android:id="@+id/mTab_set"        android:layout_width="0dp"        android:layout_height="45dp"        android:layout_weight="1"        >        <Button             android:id="@+id/btn_set"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:text="设置"            android:visibility="gone"/>    </LinearLayout></LinearLayout>

content.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" >    <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:textSize="25dp"        android:text="content" /></RelativeLayout>

friend_fragment.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" >    <TextView        android:id="@+id/content_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="我是微信"        android:textSize="65dp" /></LinearLayout>

title.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="wrap_content"    android:orientation="horizontal"     android:background="#00ff00">    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="left"        android:text="button" />    <TextView        android:id="@+id/textView"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.hongyan"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
0 0
原创粉丝点击