Android用Fragment創(chuàng)建選項(xiàng)卡
本文結(jié)合之前的動(dòng)態(tài)創(chuàng)建fragment來進(jìn)行一個(gè)實(shí)踐,來實(shí)現(xiàn)用Fragment創(chuàng)建一個(gè)選項(xiàng)卡
項(xiàng)目布局
<LinearLayout xmlns: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:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tab1" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="社會(huì)新聞" /> <TextView android:id="@+id/tab2" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="生活新聞" /> <TextView android:id="@+id/tab3" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="軍事新聞" /> <TextView android:id="@+id/tab4" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="娛樂新聞" /> </LinearLayout> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout> </LinearLayout>
新建Fragment1.java~Fragment4.java,其中Fragment1.java中的代碼如下:
public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, null); } }
其他幾個(gè)文件的代碼類似
新建fragment1.xml~fragment4.xml,其中fragment1.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:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="社會(huì)新聞" android:textAppearance="?android:attr/textAppearanceLarge"/> </LinearLayout>
其他幾個(gè)文件的代碼類似
MainActivity.java中的代碼如下:
public class MainActivity extends Activity implements OnClickListener { private LinearLayout content; private TextView tv1, tv2, tv3, tv4; private FragmentManager fm; private FragmentTransaction ft; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); content = (LinearLayout) findViewById(R.id.content); tv1 = (TextView) findViewById(R.id.tab1); tv2 = (TextView) findViewById(R.id.tab2); tv3 = (TextView) findViewById(R.id.tab3); tv4 = (TextView) findViewById(R.id.tab4); tv1.setOnClickListener(this); tv2.setOnClickListener(this); tv3.setOnClickListener(this); tv4.setOnClickListener(this); fm = getFragmentManager(); ft = fm.beginTransaction(); ft.replace(R.id.content, new Fragment1()); // 默認(rèn)情況下Fragment1 } @Override public void onClick(View v) { ft = fm.beginTransaction(); switch (v.getId()) { case R.id.tab1: ft.replace(R.id.content, new Fragment1()); break; case R.id.tab2: ft.replace(R.id.content, new Fragment2()); break; case R.id.tab3: ft.replace(R.id.content, new Fragment3()); break; case R.id.tab4: ft.replace(R.id.content, new Fragment4()); break; default: break; } ft.commit(); } }
運(yùn)行項(xiàng)目后如下效果:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
/** * Created by gerry.zhong on 2016/10/11. */ var gerry = (function(){ //創(chuàng)建一個(gè)獨(dú)立的對(duì)象,注入所有的方法,包括你想拋出去和不想拋出去的 var tool = { AAAA:function(){}, BBBB:function(){ console.log("我只想內(nèi)部使用,不想給別人用"); } }; /* * 該對(duì)象承載所有需要拋出去的對(duì)象 * 1.該對(duì)象中的方法可以自己寫 * 2.該對(duì)象中的方法可以注入(例子中的tempObj.tool.AA) * 3.該對(duì)象也可以選擇性拋出給使用者需要的方法,也可以隱藏(tool.BBBB) * */ var tempObj ={ //reader為一些初始化需要的操作,有時(shí)候會(huì)有注冊(cè)事件等,或者一些預(yù)操作 reader:function(){ }, //注入所有的選擇器,方便選擇器變化,直接修改該對(duì)象中的選擇器,而不需要全局去更改 selector:{ mySelector:"#mySelector", //原密碼 }, //注入所有的接口地址,方便接口變化可以進(jìn)行,快速變更,不需要全局找引用的對(duì)象 interface:{ loginUrl:"", }, //注入page中所有的事件,統(tǒng)一管理,建議命名規(guī)范:事件_命名,例 click_login registerEle:{ click_login:function(){ //注冊(cè)單擊事件 } }, //注入所有ajax請(qǐng)求,頁面所有請(qǐng)求,將在這里統(tǒng)一管理,建議命名規(guī)范:ajax_命名,例 ajax_login /* * 該請(qǐng)求中有2種方案,看需求使用 * 1.不公用一個(gè)請(qǐng)求方案 * 2.公用一個(gè)請(qǐng)求,但是回調(diào)處理不一樣 * */ ajaxRequest:{ //不公用一個(gè)請(qǐng)求方案 ajax_login:function(){ $.post("","",function(data){ tempObj.callback.call_login(data); }); }, //會(huì)有多個(gè)業(yè)務(wù)公用這個(gè)請(qǐng)求 ajax_login_T:function(callback){ //所有接口地址從interface中獲取,callback中tempObj.callback中處理 $.post("","",callback); }, }, //處理所有回調(diào)函數(shù),針對(duì)一個(gè)請(qǐng)求,處理一個(gè)回調(diào) callback:{ //不共用請(qǐng)求處理回調(diào) call_login:function(data){ //處理回調(diào) }, //公用請(qǐng)求處理回調(diào) call_login_T:function(){ var temp = function(){ }; tempObj.ajaxRequest.ajax_login_T(temp); } }, //所有使用的工具類,如果每個(gè)項(xiàng)目都單獨(dú)的unit.js或者common.js等存放一些公共方法的,這里可以不使用 // PS:這里存放的只是僅針對(duì)于這個(gè)頁面處理的一些tool,一般沒必要拋出去,不過看業(yè)務(wù)而定 tool:{ A:function(){ console.log("我是自己寫的方法"); }, AA:tool.AAAA, //這是我想拋出去給別人用的東西 }, //臨時(shí)緩存存放區(qū)域,僅針對(duì)本頁面,如果跨頁面請(qǐng)存放cookie或者localstorage等 //主要解決有時(shí)候會(huì)使用頁面控件display來緩存當(dāng)前頁面的一些數(shù)據(jù) temp:{ }, /* * 業(yè)務(wù)使用區(qū)域,針對(duì)每個(gè)特別的業(yè)務(wù)去串上面所有的一個(gè)個(gè)原子 * 因?yàn)樯厦嫠械姆椒?,只是做一件事,這邊可以根據(jù)業(yè)務(wù)進(jìn)行串服務(wù),很簡單的 * */ firm:{ } }; /* * 閉包拋出去的方法 * */ var outputObj =function(){ //首先執(zhí)行reader方法,初始化一些操作,比如注冊(cè)事件啥啥啥的 tempObj.reader(); /* * 拋出給別人使用的對(duì)象 * 想給別人看和使用的東西,可以注入tempObj對(duì)象,就像tool中的AA的方式 * 不想給別人看和使用的東西,就像內(nèi)部tool對(duì)象中的BBBB方法,你內(nèi)部可以使用,外部是無法引用的 * */ return tempObj; } //拋出你希望拋出去的對(duì)象,因?yàn)槟阏瓶亓怂?,哈哈? return new outputObj(); })();
- android TabHost(選項(xiàng)卡)的使用方法
- android 選項(xiàng)卡(TabHost)如何放置在屏幕的底部
- Android實(shí)現(xiàn)底部導(dǎo)航欄功能(選項(xiàng)卡)
- Android TabLayout(選項(xiàng)卡布局)簡單用法實(shí)例分析
- Android仿微信底部實(shí)現(xiàn)Tab選項(xiàng)卡切換效果
- Android多個(gè)TAB選項(xiàng)卡切換效果
- Android利用Fragment實(shí)現(xiàn)Tab選項(xiàng)卡效果
- Android組件TabHost實(shí)現(xiàn)頁面中多個(gè)選項(xiàng)卡切換效果
- Android編程之TabWidget選項(xiàng)卡用法實(shí)例分析
- Android ViewPager實(shí)現(xiàn)選項(xiàng)卡切換
- Android編程實(shí)現(xiàn)自定義Tab選項(xiàng)卡功能示例
- Android開發(fā)之選項(xiàng)卡功能的實(shí)現(xiàn)方法示例
相關(guān)文章
Android網(wǎng)絡(luò)數(shù)據(jù)開關(guān)用法簡單示例
這篇文章主要介紹了Android網(wǎng)絡(luò)數(shù)據(jù)開關(guān)用法,通過自定義函數(shù)調(diào)用系統(tǒng)服務(wù)實(shí)現(xiàn)開關(guān)功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06詳解Android中的ActivityThread和APP啟動(dòng)過程
ActivityThread就是我們常說的主線程或UI線程,ActivityThread的main方法是整個(gè)APP的入口,本篇深入學(xué)習(xí)下ActivityThread,順便了解下APP和Activity的啟動(dòng)過程。2021-06-06Kotlin實(shí)現(xiàn)圖片選擇器的關(guān)鍵技術(shù)點(diǎn)總結(jié)
這篇文章主要給大家介紹了關(guān)于Kotlin實(shí)現(xiàn)圖片選擇器的一些關(guān)鍵技術(shù)點(diǎn),這是一個(gè)我在學(xué)習(xí)Kotlin過程中的一個(gè)練手項(xiàng)目,非常適合學(xué)習(xí)Kotlin的時(shí)候參考,需要的朋友可以參考下2021-09-09Android 實(shí)現(xiàn)自定義圓形進(jìn)度條的實(shí)例代碼
進(jìn)度條在Android中教程使用到,本文章向大家介紹一下Android自定義圓形進(jìn)度條實(shí)現(xiàn)代碼,需要的朋友可以參考一下。2016-11-11Qt qml中l(wèi)istview 列表視圖控件(下拉刷新、上拉分頁、滾動(dòng)軸)
這篇文章主要介紹了Qt qml中l(wèi)istview 列表視圖控件(下拉刷新、上拉分頁、滾動(dòng)軸) 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Flutter禁止手機(jī)橫屏的簡單實(shí)現(xiàn)方法
app默認(rèn)是可以橫屏的,如果需要禁止橫屏話可以參考這篇文章,本文主要給大家介紹了關(guān)于Flutter禁止手機(jī)橫屏的簡單實(shí)現(xiàn)方法,需要的朋友可以參考下2021-07-07Android 驗(yàn)證碼功能實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 驗(yàn)證碼功能實(shí)現(xiàn)代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08