简体   繁体   English

如何从弹出窗口 window class 更改片段中的 ImageView?

[英]How to change ImageView in fragment from pop up window class?

I want to change the ImageView in my fragment's xml when I click on an Image from a pop up window class.当我单击弹出窗口 window ZA2F635D0E0F3874FFF8B581C132E6C7A7Z 中的图像时,我想更改 ImageView

Pop Up class弹出 class

public class MoodPopUp extends Activity {

ImageView a,b,c,d,e,f,g,h,i;
ImageView main;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mood_picker_popup);

    DisplayMetrics  displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;

    //Pop Up Window Size
    getWindow().setLayout((int) (width*.8),(int)(height*.6));

    //Set emoji images on mood imageview
    //Main Mood Image View
    main = (ImageView) findViewById(R.id.MainMoodimageview);

    //Pop Up Picker on Click
    a = (ImageView) findViewById(R.id.Aemoji);
    a.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            main.setImageResource(R.drawable.smiling);
        }
    });
}

This is the imageview from another fragment I want to change upon click.这是来自另一个片段的 imageview,我想在点击时更改。

   <ImageView
    android:id="@+id/MainMoodimageview"
    android:layout_width="187dp"
    android:layout_height="163dp"
    android:layout_marginTop="19dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/divider3"
    android:src="@drawable/ic_baseline_emoji_emotions_24" />

Welcome to Stack Overflow, @Enzo.欢迎来到 Stack Overflow,@Enzo。

There are a few ways to accomplish what you want.有几种方法可以实现您想要的。

A traditional way to accomplish this is to use an interface in a dialog fragment.实现此目的的传统方法是在对话框片段中使用接口。

Some ideas are in this question and set of answers这个问题和一组答案中有一些想法

Callback to a Fragment from a DialogFragment 从 DialogFragment 回调片段

Another solution would be to use a shared ViewModel.另一种解决方案是使用共享的 ViewModel。 Your picker can set the Drawable and you can use it in your other activity.您的选择器可以设置 Drawable,您可以在其他活动中使用它。

https://developer.android.com/topic/libraries/architecture/viewmodel https://developer.android.com/topic/libraries/architecture/viewmodel

https://developer.android.com/codelabs/kotlin-android-training-view-model#0 https://developer.android.com/codelabs/kotlin-android-training-view-model#0

The important part if you go the ViewModel route is to make sure that your two activities or fragments, etc, share the same scope.如果您使用 go,重要的部分是 ViewModel 路线是确保您的两个活动或片段等共享相同的 scope。

Another easy way to accomplish this is to use a Singleton or a Kotlin object.另一种简单的方法是使用 Singleton 或 Kotlin object。 This works well if you only every want one instance that all will read and write to.如果您只需要一个所有人都可以读取和写入的实例,则此方法效果很好。

https://kotlinlang.org/docs/object-declarations.html#object-declarations https://kotlinlang.org/docs/object-declarations.html#object-declarations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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