fragment小結,fragment

來源:互聯網
上載者:User

fragment小結,fragment

最近新get了fragment,用起來實在方便,Android3.0以上就能使用,比tab整齊方便簡潔。

具體用法不在贅述,先記下其跳轉之類的實現:

1.替換fragment

R.id.button:  

<span style="font-size:18px;">setContentFragment(想要替換的Fragment.class,<span style="font-family: Arial, Helvetica, sans-serif;">R.id.fragment_contiainer);</span></span>
break;

2.從一個fragment(A)跳到另一個fragment(B)Fragment只能是activity裡的一部分,不能單獨跳轉運行,跳轉的時候只能是activity的跳轉。

在主檔案裡添加相應方法:


public void switchToXueche(){

radio_group_main.check(R.id.rb_leaning);

}


在A fragment中調用這個方法:


case R.id.button: {

 

((MainActivity) getActivity()).switchToXueche();

 

}

3.從一個activity(A)中跳到一個fragment同2,fragment不能單獨跳轉運行,跳轉的時候只能跳轉相關activity,只要對這個activity傳相應的參數即滿足條件主檔案中定義activity相關fragment參數:

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

int index = intent.getIntExtra("index", 1);

if(index == 2){

radio_group_main.check(R.id.rb_leaning);

setContentFragment(XuecheFragment.class,

R.id.fragment_contiainer);

}

}


在activity A跳轉的時候加上相應參數:


case R.id.button:

 

Intent intent = new Intent(HuodongxiangqingActivity.this,

MainActivity.class);

intent.putExtra("index", 2);

 

baseStartActivity(intent);

 

break;



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.