当前位置: 动力学知识库 > 问答 > 编程问答 >

android - 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. 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.

The FirstFragment defines a callbacks interface that the activity must implement. When the activity gets a callback it can send the information through to the SecondFragment. Just read the example code below to make this more clear:

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:

This activity implements the FirstFragment.Callbacks interface in order to receive callbacks from FirstFragment. When it receives an onUpdateLogtext it just passes the data on to 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:

This just provides a public method that sets the textview with new data. And this method is used by MainActivity when it gets a onUpdateLogtext callback from 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);
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: