本篇文章給大家分享的是有關(guān)使用fragment怎么實(shí)現(xiàn)界面切換效果,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

創(chuàng)新互聯(lián)于2013年成立,公司以網(wǎng)站制作、成都做網(wǎng)站、系統(tǒng)開(kāi)發(fā)、網(wǎng)絡(luò)推廣、文化傳媒、企業(yè)宣傳、平面廣告設(shè)計(jì)等為主要業(yè)務(wù),適用行業(yè)近百種。服務(wù)企業(yè)客戶數(shù)千家,涉及國(guó)內(nèi)多個(gè)省份客戶。擁有多年網(wǎng)站建設(shè)開(kāi)發(fā)經(jīng)驗(yàn)。為企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、創(chuàng)意設(shè)計(jì)、宣傳推廣等服務(wù)。 通過(guò)專業(yè)的設(shè)計(jì)、獨(dú)特的風(fēng)格,為不同客戶提供各種風(fēng)格的特色服務(wù)。
MainActivity.java :
package com.crazy.gemi;
import android.app.SearchManager;
import android.content.Intent;
import android.graphics.Color;
import android.provider.SearchRecentSuggestions;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;
import com.crazy.gemi.ui.cheaper.CheaperFragment;
import com.crazy.gemi.ui.cheaper.SearchSuggestionSampleProvider;
import com.crazy.gemi.ui.favor.FavorFragment;
import com.crazy.gemi.ui.more.MoreFragment;
import com.crazy.gemi.ui.near.NearFragment;
import com.crazy.gemi.ui.pocket.PocketFragment;
public class MainActivity extends FragmentActivity
implements View.OnClickListener, CheaperFragment.SearchResult{
private TextView[] textView = new TextView[5];
private View[] views = new View[5];
// 其中的 firstFragment 相當(dāng)于是個(gè)中間變量
private Fragment firstFragment, nearFragment, cheaperFragment, favorFragment, pocketFragmnet, moreFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initFragment();
}
private void init() {
textView[0] = (TextView)findViewById(R.id.near);
textView[1] = (TextView)findViewById(R.id.search_cheaper);
textView[2] = (TextView)findViewById(R.id.favor);
textView[3] = (TextView)findViewById(R.id.pocket);
textView[4] = (TextView)findViewById(R.id.more);
views[0] = findViewById(R.id.near_top_line);
views[1] = findViewById(R.id.cheaper_top_line);
views[2] = findViewById(R.id.favor_top_line);
views[3] = findViewById(R.id.pocket_top_line);
views[4] = findViewById(R.id.more_top_line);
textView[0].setOnClickListener(this);
textView[1].setOnClickListener(this);
textView[2].setOnClickListener(this);
textView[3].setOnClickListener(this);
textView[4].setOnClickListener(this);
}
private void initFragment() {
firstFragment = FavorFragment.newInstance();
favorFragment = firstFragment;
// 最先加載的 fragment
getSupportFragmentManager().beginTransaction().
add(R.id.frame_layout, favorFragment).commit();
textView[2].setTextColor(Color.BLACK);
views[2].setBackgroundColor(Color.parseColor("#FF6600"));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.near:
// getSupportFragmentManager().beginTransaction().
// replace(R.id.frame_layout, NearFragment.newInstance()).commit();
if(nearFragment==null){
nearFragment= NearFragment.newInstance();
}
switchContent(firstFragment, nearFragment, getSupportFragmentManager().beginTransaction());
firstFragment = nearFragment;
selectStringAndBackgroundColor(0);
break;
case R.id.search_cheaper:
if(cheaperFragment==null){
cheaperFragment= CheaperFragment.newInstance();
}
switchContent(firstFragment, cheaperFragment, getSupportFragmentManager().beginTransaction());
firstFragment = cheaperFragment;
selectStringAndBackgroundColor(1);
break;
case R.id.favor:
if(favorFragment==null){
favorFragment= FavorFragment.newInstance();
}
switchContent(firstFragment, favorFragment, getSupportFragmentManager().beginTransaction());
firstFragment = favorFragment;
selectStringAndBackgroundColor(2);
break;
case R.id.pocket:
if(pocketFragmnet==null){
pocketFragmnet= PocketFragment.newInstance();
}
switchContent(firstFragment, pocketFragmnet, getSupportFragmentManager().beginTransaction());
firstFragment = pocketFragmnet;
selectStringAndBackgroundColor(3);
break;
case R.id.more:
if(moreFragment==null){
moreFragment= MoreFragment.newInstance();
}
switchContent(firstFragment, moreFragment, getSupportFragmentManager().beginTransaction());
firstFragment = moreFragment;
selectStringAndBackgroundColor(4);
break;
}
}
/**
* 通過(guò) position 的位置改變文字和 View 的顏色
* @param position
*/
private void selectStringAndBackgroundColor(int position){
int sum = textView.length;
for (int i = 0; i < sum; i++) {
if (position == i) {
textView[i].setTextColor(Color.BLACK);
views[i].setBackgroundColor(Color.parseColor("#FF6600"));
} else {
textView[i].setTextColor(Color.GRAY);
views[i].setBackgroundColor(Color.parseColor("#f0f0f0"));
}
}
}
/**
* 判斷是否添加了界面,以保存當(dāng)前狀態(tài)
*/
public void switchContent(Fragment from, Fragment to,
FragmentTransaction transaction) {
if (!to.isAdded()) { // 先判斷是否被add過(guò)
transaction.hide(from).add(R.id.frame_layout, to)
.commit(); // 隱藏當(dāng)前的fragment,add下一個(gè)到Activity中
} else {
transaction.hide(from).show(to).commit(); // 隱藏當(dāng)前的fragment,顯示下一個(gè)
}
}
}以上就是使用fragment怎么實(shí)現(xiàn)界面切換效果,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章名稱:使用fragment怎么實(shí)現(xiàn)界面切換效果
當(dāng)前鏈接:http://www.chinadenli.net/article38/igpisp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、外貿(mào)建站、建站公司、App設(shè)計(jì)、微信小程序、全網(wǎng)營(yíng)銷(xiāo)推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)