如何从其他片段更新XML TextView

时间:2015-09-06 15:17:25

标签: android android-fragments textview

我有两个片段,我通过在它们之间滑动来导航。我想从第一个片段更新第二个片段TextView。有可能这样做吗?这是我尝试做的事情,但这对我没有用。

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

1 个答案:

答案 0 :(得分:2)

片段之间进行通信的默认Google方式是通过托管它们的活动来实现。

FirstFragment定义了活动必须实现的回调接口。当活动获得回调时,它可以将信息发送到SecondFragment。请阅读下面的示例代码,以便更清楚:

<强> FirstFragment.java

此片段有一个按钮,单击该按钮会向其活动发送回调。

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

此活动实现FirstFragment.Callbacks接口,以便从FirstFragment接收回调。当它收到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

这只是提供了一个使用新数据设置textview的公共方法。当MainActivityonUpdateLogtext获得FirstFragment回调时,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); } } 使用此方法。

self.deleteSelected = function() {

    var emails = self.emails(),
        emailsLength = emails.length,
        emailsToKeep = [];

    for (var i = 0; i < emailsLength; i++) {
        if (emails[i].selected() !== true) {
            emailsToKeep.push(emails[i]);
        }
    }

    self.emails(emailsToKeep);
}