简体   繁体   English

修改其他片段内的片段内的视图不起作用

[英]Modifying view inside a fragment inside other fragment doesn't works

I am trying to modify views inside a fragment1 from other fragment2, parent of fragment1, calling a public method of fragment1, but doesn't works. 我试图修改来自fragment1的父级的另一个fragment2的fragment1内的视图,调用fragment1的公共方法,但是不起作用。

In the container fragment a do this: 在容器片段a中执行以下操作:

AddressEditSubviewFragment profesionalEditFragment = new AddressEditSubviewFragment();
        notificationsEditFragment = new AddressEditSubviewFragment();
        fragmentTransaction.add(R.id.addresses_edit_fragment_notifications_edit_fl, notificationsEditFragment);
fragmentTransaction.commit();

CheckBox notCB = (CheckBox) view.findViewById(R.id.addresses_edit_fragment_notifications_same_cb);
        notCB.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                notificationsEditFragment.disableEnableEdit(true);

            }
        });

The method in the fragment to change the state of the views is this: 片段中用于更改视图状态的方法是这样的:

public void disableEnableEdit(boolean disable) {

        streetET.setKeyListener(null);
        streetET.setCursorVisible(false);
        streetET.setPressed(false);
        streetET.setFocusable(false);
        numberET.setFocusable(!disable);
        numberET.setEnabled(!disable);
        numberET.setText("pruebas");
        floorET.setVisibility(View.GONE);
        buildingET.setFocusable(!disable);
        buildingET.setEnabled(!disable);
        pcET.setFocusable(!disable);
        pcET.setEnabled(!disable);



    }

When I call the method from the container fragment it enters in the method, but not change nothing. 当我从容器片段中调用该方法时,它会进入该方法,但不会进行任何更改。 Why is this thing happens? 为什么会发生这种事情?

you can't and shouldn't have fragments communicate directly with one another. 您不能也不应让片段彼此直接通信。 all communication between them must go through the activity or the parent fragment (if it's hosting both of them). 它们之间的所有通信都必须经过活动或父片段(如果同时托管它们)。

There are a few ways to do this. 有几种方法可以做到这一点。 here's one method . 这是一种方法

I personally prefer working with broadcastreceivers . 我个人更喜欢与广播接收器一起工作。 The method is basically: 该方法基本上是:

  1. Have fragment1 call some method in the activity. 在活动中让fragment1调用某些方法。
  2. Have that method sent a broadcast on some intent filter when its done. 完成后,让该方法在某些意图过滤器上发送广播。
  3. Implement a broadcast receiver on fragment2, that does stuff when receiving a broadcast. 在fragment2上实现广播接收器,该接收器在接收广播时会进行填充。
  4. Have fragment2 register and unregister the receiver on said intent filter as you please (usually done on the onResume and onPause respectfully). 根据需要,让fragment2在所述意图过滤器上注册和注销接收器(通常分别在onResume和onPause上完成)。

Tell me if my explanation wasn't clear enough, I'll provide a working code example. 告诉我我的解释不够清楚,我将提供一个工作代码示例。

Good luck! 祝好运! :) :)

--- EDIT --- -编辑-

Ok, after understanding you're trying to call a method in a child fragment from its parent fragment, here's a method with broadcast receivers that should work: 好的,理解之后,您将尝试从其父片段的一个子片段中调用一个方法,这是一个具有广播接收器的方法,该方法应该可以正常工作:

parent fragment sends a broadcast on some intent filter whenever you need to call the method in the child fragment. 当您需要在子片段中调用方法时,父片段会在一些意图过滤器上发送广播。

Intent intent = new Intent();
intent.setAction(some_string);
getActivity().sendBroadcast(intent);

implement a broadcast receiver in child fragment that calls that method in its onReceive() . 在子片段中实现广播接收器,该子片段在其onReceive()中调用该方法。

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        doStuff();
    }
};

register the receiver to the intent filter in the child fragment's onResume() , and unregister it in the child's onPause() . 将接收方注册到子片段的onResume()的intent过滤器,然后将其注销到子片段的onPause()

getActivity().registerReceiver(receiver, new IntentFilter(some_string));
getActivity().unregisterReceiver(receiver);

Finally, I found the problem. 最后,我发现了问题。 The problem was that I had two cheboxes whit the same id in the parent fragment. 问题是我在父片段中有两个同名的chebox。 And when I check a check box doesn't work well. 而且当我选中一个复选框时,它不能正常工作。 Sorry for the question. 对不起,这个问题。

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

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