简体   繁体   中英

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.

Progress Dialog Fragment 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;
}

For the buttons at the background to respond to clicks, you have to implement the onClick listener of the various buttons.

<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:

  @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();

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM