简体   繁体   English

我怎样才能将片段称为活动?

[英]How can i call fragment to activity?

I created a progress dialog into a fragment by using this effect . 通过使用此效果,我将进度对话框创建为一个片段。 Now i must call it in different activities. 现在我必须在不同的活动中称呼它。 When I call the effect the buttons at the background must not work. 当我调用效果器时,后台的按钮必须不起作用。 How can I do that? 我怎样才能做到这一点?

First i need to learn how to call the fragment in a different activity. 首先,我需要学习如何在另一个活动中调用该片段。 Then i must make the buttons at the background unclickable but there are many of them so 'setclickabla(false)' would be a tiring choice. 然后,我必须使背景上的按钮不可单击,但其中有许多按钮,因此“ setclickabla(false)”将是一个很累人的选择。

Progress Dialog Fragment XML: 进度对话框片段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 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;
}

For the buttons at the background to respond to clicks, you have to implement the onClick listener of the various buttons. 为了使后台的按钮能够响应单击,您必须实现各种按钮的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" />

In your layout above, you are making use of ImageView which I changed to centerButton and you can make it respond to clicks this way: 在上面的布局中,您使用的是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;
}

Call your fragment from your activity: 从活动中调用片段:

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