Android Fragment使用

1.activity_main_fragment.xml

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="mydemo.mycom.demo2.FragmentMain">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tab1"
            android:text="社会新闻"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tab2"
            android:text="生活新闻"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tab3"
            android:text="军事新闻"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tab4"
            android:text="娱乐新闻"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_height="wrap_content" />
        </LinearLayout>

    <LinearLayout
        android:id="@+id/lv"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        </LinearLayout>



</LinearLayout>
复制代码

2.fragment1.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">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="社会新闻"/>
</LinearLayout>

3.fragment2.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">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="生活新闻"/>
</LinearLayout>

4.fragment3.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">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="军事新闻"/>
</LinearLayout>

5.fragment4.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">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="娱乐新闻"/>
</LinearLayout>

6.FragmentMain.java

复制代码
package mydemo.mycom.demo2;



import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class FragmentMain extends FragmentActivity implements View.OnClickListener {

    private TextView tab1;
    private TextView tab2;
    private TextView tab3;
    private TextView tab4;
    private FragmentManager fm;
    private FragmentTransaction ft;
    private List<Fragment> list = new ArrayList<Fragment>();
    private List<TextView> tvList = new ArrayList<TextView>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_main);

        tab1 = (TextView)findViewById(R.id.tab1);
        tab2 = (TextView)findViewById(R.id.tab2);
        tab3 = (TextView)findViewById(R.id.tab3);
        tab4 = (TextView)findViewById(R.id.tab4);
        tvList.add(tab1);
        tvList.add(tab2);
        tvList.add(tab3);
        tvList.add(tab4);

        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        tab4.setOnClickListener(this);

        list.add(new Fragment1());
        list.add(new Fragment2());
        list.add(new Fragment3());
        list.add(new Fragment4());


        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();


        tvList.get(0).setTextColor(getResources().getColor(R.color.detail_background));
        tvList.get(0).setBackgroundColor(getResources().getColor(R.color.default_background));

        ft.replace(R.id.lv,new Fragment1());

        ft.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_fragment_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        ft = fm.beginTransaction();
        switch (view.getId())
        {
            case R.id.tab1:
                //ft.hide(list.get(0)).hide(list.get(1)).hide(list.get(2)).show(list.get(0));
                ft.replace(R.id.lv,new Fragment1());
                setStyle(0);
                break;
            case R.id.tab2:
                //ft.hide(list.get(0)).hide(list.get(1)).hide(list.get(2)).show(list.get(1));
                ft.replace(R.id.lv,new Fragment2());
                setStyle(1);
                break;
            case R.id.tab3:
                //ft.hide(list.get(0)).hide(list.get(1)).hide(list.get(2)).show(list.get(3));
                ft.replace(R.id.lv,new Fragment3());
                setStyle(2);
                break;
            case R.id.tab4:
                //ft.hide(list.get(0)).hide(list.get(1)).hide(list.get(2)).show(list.get(4));
                ft.replace(R.id.lv,new Fragment4());
                setStyle(3);
                break;
        }
        ft.commit();
    }

    private void setStyle(int i)
    {
        for (TextView tv:tvList)
        {
            tv.setBackgroundColor(getResources().getColor(R.color.default_color));
            tv.setTextColor(getResources().getColor(R.color.black));
        }


        tvList.get(i).setTextColor(getResources().getColor(R.color.detail_background));
        tvList.get(i).setBackgroundColor(getResources().getColor(R.color.default_background));
    }
}
复制代码

7.Fragment1.java

复制代码
package mydemo.mycom.demo2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2015/5/25.
 */

public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1,null);
    }

}
复制代码

8.Fragment2.java

复制代码
package mydemo.mycom.demo2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2015/5/25.
 */

public class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment2,null);
    }
}
复制代码

9.Fragment3.java

复制代码
package mydemo.mycom.demo2;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2015/5/25.
 */

public class Fragment3 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment3,null);
    }

}
复制代码

10.Fragment4.java

复制代码
package mydemo.mycom.demo2;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2015/5/25.
 */

public class Fragment4 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment4,null);
    }
}
复制代码

 

posted @   大空白纸  阅读(220)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示