android基礎---->Fragment的使用

來源:互聯網
上載者:User

標籤:lda   keep   srp   etc   usb   fragment   oda   dex   sse   

  片段(Fragment)是一種可以嵌入在活動當中的UI 片段,它能讓程式更加合理和充分地利用大螢幕的空間,因而在平板上應用的非常廣泛。

Fragment的基礎例子

一、增加Fragment,another_right_fragment.xml檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="#ffff00"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="This is another right fragment"        android:textSize="20sp" /></LinearLayout>

二、AnotherRightFragment類:

package com.example.fragmenttest2;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Linux on 2016/8/9. */public class AnotherRightFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.another_right_fragment, container, false);        return view;    }}

三、在mainActivity中使用

public void addFragment(View view) {    AnotherRightFragment anotherRightFragment = new AnotherRightFragment();    getFragmentManager().beginTransaction().add(R.id.right_layout, anotherRightFragment, FRAGMENT_TAG).commit();}

四、運行效果如下:

 

Fragment的一些介紹

一、調用FragmentManager的findFragmentById()方法,可以在活動中得到相應片段的執行個體,然後就能輕鬆地調用片段裡的方法了。

RightFragment rightFragment = (RightFragment) getFragmentManager().findFragmentById(R.id.right_fragment);

二、在每個片段中都可以通過調用getActivity()方法來得到和當前片段相關聯的活動執行個體

MainActivity activity = (MainActivity) getActivity();

三、替換Fragment,

public void replaceFragment(View view) {    AnotherRightFragment fragment = new AnotherRightFragment();    FragmentManager fragmentManager = getFragmentManager();    FragmentTransaction transaction = fragmentManager.beginTransaction();    transaction.replace(R.id.right_layout, fragment);    // 這沒有加的話,back直接退出    transaction.addToBackStack(null);    transaction.commit();}

四、刪除Fragment:

public void deleteFragment(View view) {    getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentByTag(FRAGMENT_TAG)).commit();}

 五、運行效果:

Fragment使用Activity中的方法

一、在Fragment中定義介面,並在onAttach方法中轉換,之後可以在Fragment其他方法中調用mCallback.onArticleSelected()的方法。

OnHeadlineSelectedListener mCallback;public interface OnHeadlineSelectedListener {    public void onArticleSelected();}@Overridepublic void onAttach(Context context) {    Log.i(TAG, "on attach");    super.onAttach(context);    try {        mCallback = (OnHeadlineSelectedListener) context;    } catch (ClassCastException e) {        throw new ClassCastException(context.toString() + " must implement OnHeadlineSelectedListener");    }}

二、在MainActivity中繼承OnHeadlineSelectedListener介面。

public class MainActivity extends AppCompatActivity implements HeadlinesFragment.OnHeadlineSelectedListener {    @Override    public void onArticleSelected() {        Toast.makeText(MainActivity.this, "hello world", Toast.LENGTH_SHORT).show();    }}

 這個方法由於是在MainActivity,也就是可以實現Fragment與Fragment之間互動通訊,然而事實也確實如此。

 

Fragment的生命週期

 

 

友情連結

 

android基礎---->Fragment的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.