(仿頭條APP項目)2.主界面按鈕切換Fragment頁面

主界面按鈕切換Fragment頁面

效果展示

在這裏插入圖片描述

框架結構

在這裏插入圖片描述

xml視圖

主視圖

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#ffffff"></LinearLayout>
    <FrameLayout
        android:id="@+id/fl_content"

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
       ></FrameLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        >
        <RadioGroup
            android:id="@+id/rg_option"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:checkedButton="@id/rb_home">

            <RadioButton
                android:id="@+id/rb_home"
                style="@style/my_radiobutton"
                android:drawableTop="@drawable/selctor_home"
                android:text="首頁"/>
            <RadioButton
                android:id="@+id/rb_video"
                style="@style/my_radiobutton"
                android:text="西瓜視頻"
                android:drawableTop="@drawable/selctor_video"/>
            <RadioButton
                style="@style/my_radiobutton"
                android:background="@mipmap/asq"
                />
            <RadioButton
                android:id="@+id/rb_talk"
                style="@style/my_radiobutton"
                android:text="微頭條"
                android:drawableTop="@drawable/selctor_talk"
                />
            <RadioButton
                android:id="@+id/rb_user"
                style="@style/my_radiobutton"
                android:drawableTop="@drawable/selctor_user"
                android:text="我的"/>
        </RadioGroup>

    </LinearLayout>
</LinearLayout>

選擇器和style

準備被選擇時的按鈕圖片和正常未選中時的按鈕圖片,將其在選擇器中設置
在這裏插入圖片描述

<!--文字選擇器-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorRed" android:state_checked="true"></item>
    <item android:color="@color/colorBlack" android:state_checked="false"></item>
</selector>

<!--圖片選擇器-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/user_select" android:state_checked="true"></item>
    <item android:drawable="@mipmap/user_normal" android:state_checked="false"></item>
</selector>

java代碼模塊

創建4個Fragment

在這裏插入圖片描述

public class HomeFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = getMyView();
        return view;
    }

    private View getMyView() {
        TextView textView= new TextView(getActivity());
        textView.setBackgroundColor(Color.BLUE);
        textView.setText("首頁");
        textView.setTextSize(30);
        textView.setGravity(Gravity.CENTER);
        return textView;
    }

1.綁定按鈕選擇事件

private void listenRadioButton() {
        //初始化RadioGroup
        RadioGroup radioGroup = findViewById(R.id.rg_option);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                setContentShowFragment(checkedId);
            }
        });
    }

2.初始化和用HashMap管理fragment對象

 //創建成員HashMap變量
    HashMap<Integer,Fragment> map ;
    private void initPages() {
        //初始化
        HomeFragment homeFragment = new HomeFragment();
        TalkFragment talkFragment = new TalkFragment();
        UserFragment userFragment = new UserFragment();
        VideoFragment videoFragment = new VideoFragment();
        //管理四個頁面
        //初始化HashMap
        map = new HashMap<>();
        map.put(R.id.rb_home,homeFragment);
        map.put(R.id.rb_talk,talkFragment);
        map.put(R.id.rb_user,userFragment);
        map.put(R.id.rb_video,videoFragment);
    }

3.創建切換Fragment功能的方法

 private void setContentShowFragment(int checkedId) {
        Fragment fragment = map.get(checkedId);
        //通過fragment 的管理者將fragment顯示在指定的佈局
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //根據id來切換fragment界面
        fragmentTransaction.replace(R.id.fl_content,fragment);
        //必須提交,頁面纔會更新
        fragmentTransaction.commit();
    }

總代碼

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.administrator.zhjrtt.R;
import com.xzit.fragment.HomeFragment;
import com.xzit.fragment.TalkFragment;
import com.xzit.fragment.UserFragment;
import com.xzit.fragment.VideoFragment;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.創建監聽器
        listenRadioButton();
        //2.初始化和用HashMap管理fragment對象
        initPages();
        //3.創建切換Fragment功能的方法setOnContentShowFragmet(int checkedId)
        //4.設置HomeFragment爲默認界面
        setContentShowFragment(R.id.rb_home);



    }
    private void setContentShowFragment(int checkedId) {
        Fragment fragment = map.get(checkedId);
        //通過fragment 的管理者將fragment顯示在指定的佈局
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //根據id來切換fragment界面
        fragmentTransaction.replace(R.id.fl_content,fragment);
        //必須提交,頁面纔會更新
        fragmentTransaction.commit();
    }
    //創建成員HashMap變量
    HashMap<Integer,Fragment> map ;
    private void initPages() {
        //初始化
        HomeFragment homeFragment = new HomeFragment();
        TalkFragment talkFragment = new TalkFragment();
        UserFragment userFragment = new UserFragment();
        VideoFragment videoFragment = new VideoFragment();
        //管理四個頁面
        //初始化HashMap
        map = new HashMap<>();
        map.put(R.id.rb_home,homeFragment);
        map.put(R.id.rb_talk,talkFragment);
        map.put(R.id.rb_user,userFragment);
        map.put(R.id.rb_video,videoFragment);
    }

    private void listenRadioButton() {
        //初始化RadioGroup
        RadioGroup radioGroup = findViewById(R.id.rg_option);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                setContentShowFragment(checkedId);
            }
        });
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章