简体   繁体   English

如何从其他片段更新XML TextView

[英]How to update an XML TextView from other fragment

I have two fragments and I navigate by swiping between them . 我有两个片段,在两个片段之间滑动即可浏览。 I want to update second fragment TextView from first fragment. 我想从第一个片段更新第二个片段TextView Is it possibleto do that? 有可能这样做吗? Here's what I try to do but this doesn't worked to me. 这是我尝试执行的操作,但这对我不起作用。

public void updateOtherFragment(){
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View vi = inflater.inflate(R.layout.fragment_log, null); //my second fragment xml file.
        TextView tv = (TextView)vi.findViewById(R.id.textLog);

        tv.setText("Updated from first fragment " + info.getName());
}

The default Google way for communication between fragments is to do that through the activity that hosts them. Google在片段之间进行通信的默认方式是通过托管它们的活动来实现。

The FirstFragment defines a callbacks interface that the activity must implement. FirstFragment定义了活动必须实现的回调接口。 When the activity gets a callback it can send the information through to the SecondFragment. 当活动获得回调时,它可以将信息发送到SecondFragment。 Just read the example code below to make this more clear: 只需阅读下面的示例代码即可使其更加清晰:

FirstFragment.java : FirstFragment.java

This fragment has a button, which when clicked sends a callback to its activity. 该片段具有一个按钮,单击该按钮会将回调发送到其活动。

public class FirstFragment extends Fragment implements View.OnClickListener {

    public FirstFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View result = inflater.inflate(R.layout.fragment_first, container, false);
        result.findViewById(R.id.updateButton).setOnClickListener(this);
        return result;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.updateButton:
                ((Callbacks) getActivity()).onUpdateLogtext("This is an update from FirstFragment");
                break;
            default:
                throw new UnsupportedOperationException();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof Callbacks))
            throw new UnsupportedOperationException("Hosting activity must implement Callbacks interface");
    }

    public interface Callbacks {
        void onUpdateLogtext(String text);
    }
}

MainActivity.java : MainActivity.java

This activity implements the FirstFragment.Callbacks interface in order to receive callbacks from FirstFragment. 此活动实现FirstFragment.Callbacks接口,以便从FirstFragment接收回调。 When it receives an onUpdateLogtext it just passes the data on to SecondFragment. 当它收到一个onUpdateLogtext它只是将数据传递给SecondFragment。

public class MainActivity extends AppCompatActivity implements FirstFragment.Callbacks {

    private SecondFragment secondFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        secondFragment = (SecondFragment) getFragmentManager().findFragmentById(R.id.secondFragment);
    }

    @Override
    public void onUpdateLogtext(String text) {
        secondFragment.updateLogtext(text);
    }
}

SecondFragment.java : SecondFragment.java

This just provides a public method that sets the textview with new data. 这只是提供了一个公共方法,该方法将textview设置为新数据。 And this method is used by MainActivity when it gets a onUpdateLogtext callback from FirstFragment . MainActivityFirstFragment获得onUpdateLogtext回调时,将使用此方法。

public class SecondFragment extends Fragment {

    private TextView tv;

    public SecondFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View result = inflater.inflate(R.layout.fragment_second, container, false);
        tv = (TextView) result.findViewById(R.id.textlog);
        return result;
    }

    public void updateLogtext(String text) {
        tv.setText(text);
    }
}

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

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