简体   繁体   English

从 Fragment 到 MainActivity 的按钮 OnClick

[英]Button OnClick from Fragment to MainActivity

I have 3 Fragments inside my MainActivity and in one of my Fragments I have 3 ImageView and each of image view performs same action based on which ImageView was selected, now What I want to achieve is that instead of using OnClickListener for each of Views separately, call same method with switch-case or if-else inside.我的MainActivity中有 3 个片段,在我的一个片段中有 3 个ImageView并且每个图像视图都根据选择的ImageView执行相同的操作,现在我想要实现的是,而不是分别为每个视图使用OnClickListener ,使用 switch-case 或 if-else 调用相同的方法。 But the problem is those ImageViews inside fragment cannot call Functions implemented inside MainActivity.但是问题是fragment里面的那些ImageViews不能调用MainActivity里面实现的函数。 for instance.例如。 here's the code to understand what I want to say:这是理解我想说的话的代码:

MAIN ACTIVITY主要活动

public void imageClicked(View v){
    if(v.getID() == findViewById(R.id.id_of_imageView_1).getID()){
        myTextView.setText("ImageView 1 is Clicked")
    }
    
    else if(v.getID() == findViewById(R.id.id_of_imageView_2).getID()){
        myTextView.setText("ImageView 2 is Clicked")
    }

    else if(v.getID() == findViewById(R.id.id_of_imageView_3).getID()){
        myTextView.setText("ImageView 3 is Clicked")
    }
}

XML (fragment_images.xml) WHERE IMAGES EXIST XML (fragment_images.xml) 图像存在的地方

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/id_of_imageView_1"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:src="@drawable/laptop" />

            <ImageView
                android:id="@+id/id_of_imageView_2"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:src="@drawable/memory" />

            <ImageView
                android:id="@+id/id_of_imageView_3"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:src="@drawable/screen" />

        </LinearLayout>
    </ScrollView>

XML (activity_main.xml) HERE I Link my Fragment and TextView XML (activity_main.xml) 这里我链接我的片段和 TextView

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

     //CHANGE THIS TEXT VIEW WHEN IMAGE IS CLICKED
     <TextView
        android:id="@+id/myTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center_horizontal"
        android:text="Select Product"
        android:textSize="20sp"
        android:textStyle="bold" />

        <fragment
            android:id="@+id/fragment_1"
            android:name="com.salmanibrahim.calssifiedelectronics.ListFrag"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            tools:layout="@layout/fragment_list" />

        <fragment
            android:id="@+id/fragment_2"
            android:name="com.salmanibrahim.calssifiedelectronics.DetailFrag"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            tools:layout="@layout/fragment_detail" />

        //THIS IS THE FRAGMENT
        <fragment
            android:id="@+id/fragment_images"
            android:name="com.salmanibrahim.calssifiedelectronics.AddProduct"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            tools:layout="@layout/fragment_images" />
    </LinearLayout>

How do I call imageClicked() defined inside MainActivity using onClick on ImageView如何在 ImageView 上使用onClick调用MainActivity中定义的ImageView imageClicked()

In your fragment you can call imageClicked() in the MainActivity by using requireActivity() or getActivity() and do the necessary cast在您的片段中,您可以使用requireActivity()getActivity() ) 在MainActivity中调用imageClicked() ) 并执行必要的转换

In fragment:在片段中:

public View onCreateView(...) {

    View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
    MainActivity mActivity = (MainActivity) requireActivity();

    imageView1 = view.findViewById(...);
    imageView1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mActivity.imageClicked(imageView1);
        }
    });

    
...}

repeat the same for imageView2, 3对 imageView2、3 重复相同的操作

Also the condition in imageClicked() seems not right as you findViewById which will point to your MainActivity layout, not the fragment.此外, imageClicked()中的条件似乎不正确,因为您findViewById将指向您的 MainActivity 布局,而不是片段。

So you need to change this所以你需要改变这个

if (v.getID() == findViewById(R.id.id_of_imageView_1).getID())

To

if (v.getID() == R.id.id_of_imageView_1)

And do the same for other images.并为其他图像做同样的事情。

I understand what you trying to do but android:onClick parameter only links for the context declared in tools:context field on the parent tag of your layout file.我了解您要做什么,但android:onClick参数仅链接tools:context字段。

So it is not possible to reference your imageClicked() function found in your MainActivity from your fragment's ImageView because they are in different context.因此,无法从片段的ImageView中引用您在 MainActivity 中找到的imageClicked() function,因为它们处于不同的上下文中。 Check this for more info.检查以获取更多信息。

You are doing it in wrong way.你做错了。 You can do this instead.您可以改为这样做。

imageView_1.setOnClickListener(this);

then implement activity from View.onClickListener然后从View.onClickListener实现活动

then get view by id on the method that's been implemented.然后通过 id 查看已实现的方法。

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.id_of_imageView_1:
            // do your things here
            break;
        }
}

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

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