App主界面布局的實(shí)現(xiàn)方式(一)

RadioGroup + Fragment實(shí)現(xiàn)

  • 01 效果圖
    該實(shí)現(xiàn)方式只能通過(guò)切換RadioButton來(lái)切換頁(yè)面,并不能通過(guò)滑動(dòng)來(lái)實(shí)現(xiàn)。
MainInterface.png
  • 02 layout
    4個(gè)fragment的相似布局文件
contact_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"
    android:gravity="center">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Contact fragment"
        android:textSize="30sp"/>
</LinearLayout>

主布局文件

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!-- 將該FrameLayout作為Fragment的容器 -->
    <FrameLayout 
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <RadioGroup 
        android:id="@+id/rg_control"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="bottom"
        android:gravity="center">
        <RadioButton
            android:id="@+id/rb_talk"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="30dp"
            android:button="@drawable/radio_talk_selector"
            android:checked="true" />
        <RadioButton 
            android:id="@+id/rb_contact"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@drawable/radio_contact_selector"/>
        <RadioButton 
            android:id="@+id/rb_discovery"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@drawable/radio_discovery_selector"/>
        <RadioButton 
            android:id="@+id/rb_personal"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@drawable/radio_personal_selector"/>
    </RadioGroup>
</LinearLayout>
  • 03 Activity
    4個(gè)Fragment的相似Java代碼
public class ContactFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // 這里注意inflate的第三個(gè)參數(shù)要設(shè)置為false
        View view = inflater.inflate(R.layout.contact_fragment, container, false);
        return view;
    }
}

MainActivity.java

public class MainActivity extends Activity implements OnCheckedChangeListener {
    // 用于對(duì)Fragment進(jìn)行管理
    private FragmentManager fragmentManager;
    private TalkFragment talkFragment;
    private ContactFragment contactFragment;
    private DiscoveryFragment discoveryFragment;
    private PersonalFragment personalFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        fragmentManager = getFragmentManager();
        initUI();
    }

    private void initUI() {
        RadioGroup rg_control = (RadioGroup) findViewById(R.id.rg_control);
        rg_control.setOnCheckedChangeListener(this);
        // 設(shè)置默認(rèn)的Fragment
        setDefaultFragment();
    }

    private void setDefaultFragment() {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        talkFragment = new TalkFragment();
        transaction.add(R.id.fl_content, talkFragment);
        transaction.commit();
    }

    // Radio切換的事件處理
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
        case R.id.rb_talk:
            setTabSelection(0);
            break;
        case R.id.rb_contact:
            setTabSelection(1);
            break;
        case R.id.rb_discovery:
            setTabSelection(2);
            break;
        case R.id.rb_personal:
            setTabSelection(3);
            break;
        }
    }

    private void setTabSelection(int index) {
        // 開(kāi)啟一個(gè)Fragment事務(wù)
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隱藏掉所有的Fragment
        hideFragments(transaction);
        switch (index) {
        case 0:
            if (talkFragment == null) {
                // 如果TalkFragment為空,則創(chuàng)建一個(gè)并添加到界面上
                talkFragment = new TalkFragment();
                transaction.add(R.id.fl_content, talkFragment);
            } else {
                // 如果TalkFragment不為空,則直接將它顯示出來(lái)
                transaction.show(talkFragment);
            }
            break;
        case 1:
            if (contactFragment == null) {
                contactFragment = new ContactFragment();
                transaction.add(R.id.fl_content, contactFragment);
            } else {
                transaction.show(contactFragment);
            }
            break;
        case 2:
            if (discoveryFragment == null) {
                discoveryFragment = new DiscoveryFragment();
                transaction.add(R.id.fl_content, discoveryFragment);
            } else {
                transaction.show(discoveryFragment);
            }
            break;
        case 3:
            if (personalFragment == null) {
                personalFragment = new PersonalFragment();
                transaction.add(R.id.fl_content, personalFragment);
            } else {
                transaction.show(personalFragment);
            }
            break;
        }
        transaction.commit();
    }

    /**
     * 將所有的Fragment都置為隱藏狀態(tài)。
     * 
     * @param transaction
     *            用于對(duì)Fragment執(zhí)行操作的事務(wù)
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (talkFragment != null) {
            transaction.hide(talkFragment);
        }
        if (contactFragment != null) {
            transaction.hide(contactFragment);
        }
        if (discoveryFragment != null) {
            transaction.hide(discoveryFragment);
        }
        if (personalFragment != null) {
            transaction.hide(personalFragment);
        }
    }
}
  • 04 橫豎屏切換
    如果參考這個(gè)Demo后,在橫豎屏切換時(shí)發(fā)現(xiàn)重疊問(wèn)題,那么在AndroidManifest.xml文件中做如下設(shè)置。
    橫豎屏切換時(shí),activity不重新創(chuàng)建。
<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|keyboardHidden|screenSize">
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容