簡體   English   中英

我怎樣才能將片段稱為活動?

[英]How can i call fragment to activity?

通過使用此效果,我將進度對話框創建為一個片段。 現在我必須在不同的活動中稱呼它。 當我調用效果器時,后台的按鈕必須不起作用。 我怎樣才能做到這一點?

首先,我需要學習如何在另一個活動中調用該片段。 然后,我必須使背景上的按鈕不可單擊,但其中有許多按鈕,因此“ setclickabla(false)”將是一個很累人的選擇。

進度對話框片段XML:

<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ProgressDialogFragment">

<com.skyfishjy.library.RippleBackground
    android:id="@+id/ProgressDialogs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CB000000"
    app:rb_color="#80FFFFFF"
    app:rb_duration="2500"
    app:rb_radius="32dp"
    app:rb_rippleAmount="4"
    app:rb_scale="6">

    <ImageView
        android:id="@+id/centerImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/logo" />
</com.skyfishjy.library.RippleBackground>

ProgressDialogFragment.java

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_progress_dialog, container, false);
    RippleBackground rippleBackground = (RippleBackground)view.findViewById(R.id.ProgressDialogs);
    rippleBackground.startRippleAnimation();
    return view;
}

為了使后台的按鈕能夠響應單擊,您必須實現各種按鈕的onClick偵聽器。

<com.skyfishjy.library.RippleBackground
android:id="@+id/ProgressDialogs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CB000000"
app:rb_color="#80FFFFFF"
app:rb_duration="2500"
app:rb_radius="32dp"
app:rb_rippleAmount="4"
app:rb_scale="6">

<Button
    android:id="@+id/centerButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/logo" />

在上面的布局中,您使用的是ImageView(我將其更改為centerButton),並且可以通過以下方式使其對點擊做出響應:

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_progress_dialog, container, false);
    RippleBackground rippleBackground = (RippleBackground)view.findViewById(R.id.ProgressDialogs);
    rippleBackground.startRippleAnimation();

Button button=(Button)view.findViewById(R.id.centerButton);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // your respond to clicks
        }
    });
    return view;
}

從活動中調用片段:

public class MainActivity extends AppCompatActivity  {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Fragment fragment = new MainFragment(); // your fragment
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();

        }
    }

暫無
暫無

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

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