gpt4 book ai didi

java - 我如何将 fragment 调用到 Activity ?

转载 作者:行者123 更新时间:2023-12-02 01:46:06 26 4
gpt4 key购买 nike

我使用 this effect 在 fragment 中创建了一个进度对话框。现在我必须在不同的 Activity 中调用它。当我调用效果时,后台的按钮不能工作。我怎样才能做到这一点?

首先我需要学习如何在不同的 Activity 中调用 fragment 。然后我必须使后台的按钮不可点击,但它们有很多,所以“setclickabla(false)”将是一个累人的选择。

进度对话框 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;
}

最佳答案

为了让后台按钮响应点击,你必须实现各个按钮的 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;
}

从您的 Activity 中调用您的 fragment :

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

}
}

关于java - 我如何将 fragment 调用到 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57453429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com