Android Fragment實現按鈕間的切換
原文地址:http://blog.csdn.net/a123demi/article/details/32693037
Fragment要點
Fragment是activity的介面中的一部分或一種行為。你可以把多個Fragment們組合到一個activity中來建立一個多面介面並且你可以在多個activity中重用一個Fragment。你可以把Fragment認為模組化的一段activity,它具有自己的生命週期,接收它自己的事件,並可以在activity執行時被新增或刪除。
Fragment不能獨立存在,它必須嵌入到activity中,而且Fragment的生命週期直接受所在的activity的影響。例如:當activity暫停時,它擁有的所有的Fragment們都暫停了,當activity銷燬時,它擁有的所有Fragment們都被銷燬。然而,當activity執行時(在onResume()之後,onPause()之前),你可以單獨地操作每個Fragment,比如新增或刪除或替代(add(),remove(),replace())它們。當你在執行上述針對Fragment的事務時,你可以將事務新增到一個棧中,這個棧被activity管理,棧中的每一條都是一個Fragment的一次事務。有了這個棧,就可以反向執行Fragment的事務,這樣就可以在Fragment級支援“返回”鍵(向後導航)。
而本文簡單介紹主要通過點選不同按鈕實現切換對應的fragment的效果,類似用Tab的切換:
主要程式碼如下:
1.工程原始碼顯示:
2.編譯後效果圖
3.切換按鈕佈局:activity_bottom_bts.xml切換的按鈕顯示在底部
[html] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;"><?xmlversionxmlversion="1.0" encoding="utf-8"?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- <Button
- android:id="@+id/movie_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:gravity="center"
- android:text="@string/movie"/>
- <Button
- android:id="@+id/tv_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:gravity="center"
- android:text="@string/tv"/>
- <Button
- android:id="@+id/anime_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:gravity="center"
- android:text="@string/anime"/>
- <Button
- android:id="@+id/variety_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:gravity="center"
- android:text="@string/variety" />
- </LinearLayout></span></span>
4.主介面activity_main.xml
[html] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;"><RelativeLayoutxmlns:androidRelativeLayoutxmlns: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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.example.switchfragmentdemo.MainActivity$PlaceholderFragment">
- <LinearLayout
- android:id="@+id/button_view_include"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- >
- <includelayoutincludelayout="@layout/activity_bottom_btns" />
- </LinearLayout>
- <FrameLayout
- android:id="@+id/fragment_content"
- android:layout_width="match_parent"
- android:layout_height="fill_parent"
- android:layout_alignParentTop="true"
- android:layout_marginBottom="50dp"
- android:layout_below="@id/button_view_include"
- >
- </FrameLayout>
- </RelativeLayout></span></span>
5.strings.xml
[html] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;"><?xmlversionxmlversion="1.0" encoding="utf-8"?>
- <resources>
- <stringnamestringname="app_name">SwitchFragmentDemo</string>
- <stringnamestringname="hello_world">Hello world!</string>
- <stringnamestringname="action_settings">Settings</string>
- <string name="movie">電影</string>
- <string name="tv">電視劇</string>
- <string name="anime">動漫</string>
- <string name="variety">綜藝</string>
- <stringnamestringname="movie_view">這是一個電影介面</string>
- <string name="tv_view">這是一個電視劇介面</string>
- <stringnamestringname="anime_view">這是一個動漫介面</string>
- <stringnamestringname="variety_view">這是一個綜藝介面</string>
- </resources></span></span>
6.主介面實現程式碼:MainActivity.java
[java] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;">packagecom.example.myandroidswitchbtndemo;
- importjava.util.ArrayList;
- importjava.util.List;
- importandroid.annotation.SuppressLint;
- importandroid.app.Activity;
- importandroid.app.FragmentManager;
- importandroid.app.FragmentTransaction;
- importandroid.graphics.Color;
- importandroid.os.Bundle;
- importandroid.view.Menu;
- importandroid.view.MenuItem;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- @SuppressLint("NewApi")
- public classMainActivity extends Activity implements OnClickListener {
- private Button movieBtn, tvBtn,animeBtn, varietyBtn;
- private List<Button> btnList = newArrayList<Button>();
- private FragmentManager fm;
- private FragmentTransaction ft;
- @Override
- protected void onCreate(BundlesavedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findById();
- // 進入系統預設為movie
- fm = getFragmentManager();
- ft = fm.beginTransaction();
- setBackgroundColorById(R.id.movie_btn);
- ft.replace(R.id.fragment_content,new MovieFragment());
- ft.commit();
- }
- private void findById() {
- movieBtn = (Button)this.findViewById(R.id.movie_btn);
- tvBtn = (Button)this.findViewById(R.id.tv_btn);
- animeBtn = (Button) this.findViewById(R.id.anime_btn);
- varietyBtn = (Button)this.findViewById(R.id.variety_btn);
- movieBtn.setOnClickListener(this);
- tvBtn.setOnClickListener(this);
- animeBtn.setOnClickListener(this);
- varietyBtn.setOnClickListener(this);
- btnList.add(movieBtn);
- btnList.add(tvBtn);
- btnList.add(animeBtn);
- btnList.add(varietyBtn);
- }
- private void setBackgroundColorById(intbtnId) {
- for (Button btn : btnList) {
- if (btn.getId() == btnId){
- btn.setBackgroundColor(Color.GREEN);
- }else {
- btn.setBackgroundColor(Color.BLUE);
- }
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menumenu) {
- // Inflate the menu; this addsitems to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main,menu);
- return true;
- }
- @Override
- public booleanonOptionsItemSelected(MenuItem item) {
- // Handle action bar item clickshere. The action bar will
- // automatically handle clicks onthe Home/Up button, so long
- // as you specify a parentactivity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- returnsuper.onOptionsItemSelected(item);
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated methodstub
- fm = getFragmentManager();
- ft = fm.beginTransaction();
- switch (v.getId()) {
- case R.id.movie_btn:
- setBackgroundColorById(R.id.movie_btn);
- ft.replace(R.id.fragment_content,new MovieFragment());
- break;
- case R.id.tv_btn:
- setBackgroundColorById(R.id.tv_btn);
- ft.replace(R.id.fragment_content,new TVFragment());
- break;
- case R.id.anime_btn:
- setBackgroundColorById(R.id.anime_btn);
- ft.replace(R.id.fragment_content,new AnimeFragment());
- break;
- case R.id.variety_btn:
- setBackgroundColorById(R.id.variety_btn);
- ft.replace(R.id.fragment_content,new VarietyFragment());
- break;
- default:
- break;
- }
- // 不要忘記提交
- ft.commit();
- }
- }</span></span>
7.電影介面:fragment_movie.xml和MovieFragment.java
[html] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;"><?xml version="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#FF00FF"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/movie_tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/movie_view" />
- </LinearLayout></span></span>
[java] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;">packagecom.example.myandroidswitchbtndemo;
- import android.app.Fragment;
- importandroid.os.Bundle;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- public classMovieFragment extends Fragment {
- <spanstyle="white-space:pre"> </span>@Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_movie, null);
- }
- }</span></span>
8.電視劇介面:fragment_tv.xml和TVFragment.java
[html] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;"><?xmlversionxmlversion="1.0" encoding="utf-8"?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#00FFFF"
- android:orientation="vertical">
- <TextView
- android:id="@+id/tv_tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/tv_view"
- />
- </LinearLayout></span></span>
[java] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;">packagecom.example.myandroidswitchbtndemo;
- importandroid.os.Bundle;
- importandroid.app.Fragment;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- public classTVFragment extends Fragment {
- <spanstyle="white-space:pre"> </span>@Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_tv, null);
- }
- }</span></span>
9.動漫介面:fragment_anime和AnimeFragment.java
[html] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;"><?xmlversionxmlversion="1.0" encoding="utf-8"?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#00FF00"
- android:orientation="vertical">
- <TextView
- android:id="@+id/anime_tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/anime_view"
- />
- </LinearLayout></span></span>
[java] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;">packagecom.example.myandroidswitchbtndemo;
- importandroid.os.Bundle;
- importandroid.annotation.SuppressLint;
- importandroid.app.Fragment;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- @SuppressLint("NewApi")
- public classAnimeFragment extends Fragment {
- <spanstyle="white-space:pre"> </span>@Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_anime, null);
- }
- }</span></span>
10.綜藝介面:fragment_variety和VarietyFragment
[html] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;"><?xmlversionxmlversion="1.0" encoding="utf-8"?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#FFFF00"
- android:orientation="vertical">
- <TextView
- android:id="@+id/variety_tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/variety_view"/>
- </LinearLayout></span></span>
[java] view plain copy print?
- <span style="font-family:SimSun;"><span style="font-size:18px;">packagecom.example.myandroidswitchbtndemo;
- importandroid.os.Bundle;
- importandroid.app.Fragment;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- public classVarietyFragment extends Fragment {
- <spanstyle="white-space:pre"> </span>@Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_variety, null);
- }
- }
- </span></span>
上面為程式碼的具體實現。
相關文章
- 點選按鈕實現狀態切換效果
- 點選按鈕實現圖片切換效果
- 點選按鈕實現切換頁面背景顏色效果
- javascript閉包的使用–按鈕切換JavaScript
- 點選按鈕實現隱藏和顯示的切換程式碼
- 利用ViewPager和Fragment實現頁卡切換ViewpagerFragment
- jQuery點選按鈕實現div的隱藏和顯示切換效果jQuery
- Qt 模擬滑鼠事件-在兩個按鈕之間切換QT事件
- Android使用(TabLayout+ViewPager+fragment)與(FragmentTabHost+ViewPager+Fragment)實現底部狀態列切換AndroidTabLayoutViewpagerFragment
- 請問各位大佬,vue如何實現點選按鈕切換圖片的效果?Vue
- 點選同一按鈕實現div的隱藏與顯示切換
- 安卓開發:viewpager + fragment 實現滑動切換安卓ViewpagerFragment
- JavaScript點選按鈕切換背景顏色JavaScript
- 純CSS3實現超炫酷的3D開關切換按鈕CSSS33D
- Qt中按鈕背景圖片的切換設定QT
- vue-video-player,通過自定義按鈕元件實現全屏切換效果VueIDE元件
- javascript實現的按鈕間隔指定時間再能點選JavaScript
- Android 自定義實現switch開關按鈕Android
- css滑鼠浮劃過切換按鈕背景圖片CSS
- 核取方塊和切換按鈕的7個使用案例
- Android中Fragment巢狀Fragment,切換Fragment時不顯示檢視的原因及解決方法AndroidFragment巢狀
- Android使用Fragment打造萬能頁面切換框架AndroidFragment框架
- 點選同一按鈕顯示隱藏切換
- 38 首頁切換研究深度按鈕加陰影效果
- 自定義Navigator切換fragmentFragment
- 使用SVG實現的一個Android播放/暫停按鈕SVGAndroid
- Androidx為Fragment中的按鈕設定監聽AndroidFragment
- ViewPager、Fragment和TabLayout實現切頁效果ViewpagerFragmentTabLayout
- TabLayout+ViewPager+Fragment實現切頁展示TabLayoutViewpagerFragment
- JavaFx 實現按鈕防抖Java
- 實現浮動按鈕 (轉)
- Android——Activity切換炫酷動畫實現Android動畫
- Simple WPF: WPF 實現按鈕的長按,短按功能
- 【Flutter 專題】129 圖解 ToggleButtons 按鈕切換容器組Flutter圖解
- Android 實現APP可切換多語言AndroidAPP
- CSS實現帶箭頭按鈕CSS
- css實現圓角按鈕效果CSS
- android實現多圖片放大縮小的切換Android