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 指定加載Fragmenthtml

              2.若是Activity顯示的畫面,會切換,就在Activity-Layout-幀佈局佔位(把位置佔住),而後在Activity代碼中去替換fragment(fragment去替換幀佈局)android

 


 

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長按無反應的問題:app

 

 效果:

相關文章
相關標籤/搜索