簡體   English   中英

如何顯示來自Android自定義組件的片段?

[英]How do I show a fragment from an Android custom component?

我有一個使用合並標記聲明的自定義組件,該標記在我的應用程序中的多個位置使用。 是否可以從此類調用getFragmentManager()getSupportFragmentManager() 此類擴展了LinearLayout而不是活動。 我想要做的是顯示來自此自定義組件的新片段,但是這些功能都無法訪問。 如何顯示來自此自定義組件的新片段?

這是我的代碼的基礎:

public class SpecialTextBox extends LinearLayout implements View.OnClickListener {

    //......constructors

    public void initialize(final Context context, AttributeSet attrs) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.special_box, this);
    }

    @Override
    public void onClick(View view) {
        super.callOnClick();

        switch (view.getId()) {
            case R.id.searchButton:
                SearchFragment searchFragment = new SearchFragment(new DialogFragmentDismissHandler(), specialBox.getEditText().getText().toString());
                searchFragment.show(dashboardActivity.getFragmentManager(), SearchFragment.class.toString());
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:customAttribute="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:layout_gravity="left">

            <com.mobile.layout.SpecialEditText
                android:id="@+id/specialBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text=""
                style="@style/value"
                android:layout_gravity="left"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="0dp"
                android:layout_marginRight="0dp"
                android:layout_marginBottom="3dp"
                android:layout_gravity="bottom">

                <ImageButton
                    android:id="@+id/searchButton"
                    style="@style/searchImageButton"
                    android:clickable="true"
                    android:longClickable="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <ImageButton
                    android:id="@+id/myButton"
                    style="@style/reportButton"
                    android:clickable="true"
                    android:longClickable="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:layout_gravity="left">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/nameTextView"
                android:text=""
                android:textColor="@color/black"
                android:layout_marginLeft="1dp"
                style="@style/value"
                android:layout_gravity="bottom|left" />

        </LinearLayout>

    </LinearLayout>

</merge>

您只能從FragmentActivity調用getFragmentManager()getSupportFragmentManager() 如果您的自定義視圖在FragmentActivity內部,則可以執行此操作

FragmentActivity a = (FragmentActivity) getContext();
a.getFragmentManager()

我可以通過以下方式訪問getFragmentManager():

Activity a = (Activity)(((ContextWrapper) getContext()).getBaseContext());
a.getFragmentManager();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM