Android-fragment的替换

fragment的替换:是指一个Activity加载多个Fragment,当某些动作的时候在Activity替换Fragment显示;

 

昨天写的这几篇博客Android-fragment简介-fragment的简单使用Activity-fragment-ListView展示Android-fragment生命周期,都是讲解了在Activity的Layout里面(使用fragment-class去指定加载Fragment)

注意:⚠️ 本篇博客,和昨天写的这几篇博客都是使用 android.app.Fragment 自身的Fragment,不是v4包的,两者是有不同之处的;

 

在使用Fragment开发过程中,有一个开发技巧:

              1.如果Activity显示的画面,不会切换画面,就直接在Activity-Layout-<fragment 指定加载Fragment

              2.如果Activity显示的画面,会切换,就在Activity-Layout-帧布局占位(把位置占住),然后在Activity代码中去替换fragment(fragment去替换帧布局)


Activity

package liudeli.activity.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import liudeli.activity.R;

public class MyTestFragmentActivity4 extends Activity implements View.OnClickListener {

    private Button msg;
    private Button persons;
    private Button my;

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

        initView();
        initChangeFragment();
        initListener();
    }

    private void initView() {
        msg = findViewById(R.id.bt_msg);
        persons = findViewById(R.id.bt_persons);
        my = findViewById(R.id.bt_my);
    }

    /**
     * 初始化默认切换到 消息Fragment
     */
    private void initChangeFragment() {
        // 得到FragmentManager
        android.app.FragmentManager manager = getFragmentManager();
        // 开始事务 得到事务
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        // 替换操作
        fragmentTransaction.replace(R.id.frame_layout, new MsgFragment());
        // 提交
        fragmentTransaction.commit();

        setButton(0);
    }

    private void initListener() {
        msg.setOnClickListener(this);
        persons.setOnClickListener(this);
        my.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // 得到FragmentManager
        android.app.FragmentManager manager = getFragmentManager();
        // 开始事务 得到事务
        FragmentTransaction fragmentTransaction = manager.beginTransaction();

        Fragment fragment = null;

        switch (v.getId()) {
            case R.id.bt_msg:
                fragment = new MsgFragment();
                setButton(0);
                break;
            case R.id.bt_persons:
                setButton(1);
                fragment = new PersonsFragment();
                break;
            case R.id.bt_my:
                setButton(2);
                fragment = new MyWoFragment();
                break;
        }
        // 替换操作
        fragmentTransaction.replace(R.id.frame_layout, fragment);
        // 提交
        fragmentTransaction.commit();
    }

    /**
     * 设置三个按钮的颜色
     * @param value
     */
    private void setButton(int value) {
        switch (value) {
            case 0:
                msg.setTextColor(Color.RED);
                persons.setTextColor(Color.BLACK);
                my.setTextColor(Color.BLACK);
                break;
            case 1:
                msg.setTextColor(Color.BLACK);
                persons.setTextColor(Color.RED);
                my.setTextColor(Color.BLACK);
                break;
            case 2:
                msg.setTextColor(Color.BLACK);
                persons.setTextColor(Color.BLACK);
                my.setTextColor(Color.RED);
                break;
        }

    }
}

Activity的布局文件

<?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">

    <!-- 帧布局 下面的LinearLayout已经先填充了,剩下的控件我全部来填充  -->
    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

    </FrameLayout>

    <!-- 我的layout_weight默认为0,我先填充我的控件 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_below="@id/frame_layout">

        <Button
            android:id="@+id/bt_msg"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="消息"
            android:gravity="center"
            android:textColor="@android:color/black"
            />

        <Button
            android:id="@+id/bt_persons"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="好友"
            />

        <Button
            android:id="@+id/bt_my"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="我的"
            />

    </LinearLayout>

</LinearLayout>

消息的Fragment,MsgFragment

package liudeli.activity.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MsgFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return new ListView(getActivity()); // Fragment不能使用this
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final String[] data = new String[]{
                "你有一条消息1",
                "你有一条消息2",
                "你有一条消息3",
                "你有一条消息4",
                "你有一条消息5",
                "你有一条消息6",
                "你有一条未读消息6",
                "你有一条未读消息7",
                "你有一条未读消息8",
        };

        ListView listView = (ListView)view;
        ListAdapter listAdapter = new ArrayAdapter(getActivity(),
                                                           android.R.layout.simple_list_item_1,
                                                           android.R.id.text1,
                                                           data);
        listView.setAdapter(listAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), data[position], Toast.LENGTH_SHORT).show();
            }
        });
    }
}

好友的Fragment,PersonsFragment

package liudeli.activity.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class PersonsFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return new ListView(getActivity()); // Fragment不能使用this
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final String[] data = new String[]{
                "张三",
                "李四",
                "王五",
                "赵六",
                "王八",
                "朱九",
                "厨十",
                "阿名",
                "雄霸",
        };

        ListView listView = (ListView)view;
        ListAdapter listAdapter = new ArrayAdapter(getActivity(),
                                                           android.R.layout.simple_list_item_1,
                                                           android.R.id.text1,
                                                           data);
        listView.setAdapter(listAdapter);

        // ListVIew 设置可以解决,Item长按无反应的问题: android:descendantFocusability="blocksDescendants"
        // listView.setDescendantFocusability(2);

        /*listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), data[position], Toast.LENGTH_SHORT).show();
                return true;
            }
        });*/

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), data[position], Toast.LENGTH_SHORT).show();
            }
        });
    }
}

我的Fragment,MyWoFragment

package liudeli.activity.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MyWoFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return new GridView(getActivity()); // Fragment不能使用this
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        String[] data = new String[]{
                "我的账号",
                "我的社交",
                "我的简洁",
                "我的钱包",
                "我的设置",
                "退出账号",
                "重置账号"
        };

        GridView gridView = (GridView)view;

        // 设置三列
        gridView.setNumColumns(3);

        ListAdapter listAdapter = new ArrayAdapter(getActivity(),
                                                           android.R.layout.simple_list_item_1,
                                                           android.R.id.text1,
                                                           data);
        gridView.setAdapter(listAdapter);
    }
}

ListVIew 设置可以解决,Item长按无反应的问题:

 效果:

猜你喜欢

转载自www.cnblogs.com/android-deli/p/10183500.html