gpt4 book ai didi

android - fragment 之间如何通信?

转载 作者:行者123 更新时间:2023-11-29 17:32:23 25 4
gpt4 key购买 nike

我正在开发 Android 应用程序。我有一个要求,比如 fragment 1 中有一个按钮,当用户单击该按钮时,结果应显示在 fragment 2 中。加载 Activity 时,两个 fragment 都已附加。这是我的尝试:

在主要 Activity 中:

    public void dsp(String str) {
secondfragment f2=new secondfragment();
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");

f2.setArguments(bundle);
}

在第一个 fragment 中:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragone, container,false);
Button btn = (Button) v.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
m.dsp("clicked");
}
});
return v;
}

在第二个 fragment 中:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragtwo, container,false);
tv= (TextView) v.findViewById(R.id.textView1);

tv.setText(this.getArguments().getString("name"));

return v;
}

最佳答案

当 Fragment 与 Fragment 通信时,您使用一个接口(interface)将数据传递给 Activity,Activity 反过来更新您要更改的 fragment 。

例如:

在 fragment 1 中:

public class FragmentOne extends Fragment{

public Callback mCallback;

public interface Callback{
void onUpdateFragmentTwo(String message);
}


@Override
public void onAttach(Activity activity){
super.onAttach(activity);
mCallback = (Callback) activity;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragone, container,false);
Button btn = (Button) v.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCallback.onUpdateFragmentTwo("clicked");
}
});
return v;
}
}

然后在主Activity中实现接口(interface):

public class MainActivity extends AppCompatActivity implements Callback{

FragmentTwo fragmentTwo;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// ... Load views or perform logic

// ... Load Fragment Two into your container
if(savedInstanceState == null){
fragmentTwo = FragmentTwo.newInstance(new Bundle()); // use real bundle here
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_holder, fragmentTwo, "Frag2").commit();
}
}


// Interface method
@Override
public void onUpdateFragmentTwo(String message){
// Call activity method with the argument
if(fragmentTwo != null){
fragmentTwo.updateFragmentTwo(message);
}
}

更新

在您的第二个 fragment 中,我通常使用静态 newInstance(Bundle args) 方法进行初始化,然后使用公共(public)方法从 Activity 到 Fragment 进行通信,例如:

 public class FragmentTwo extends Fragment{

public static FragmentTwo newInstance(Bundle args){
FragmentTwo fragment = new FragmentTwo();
fragment.setArguments(args);
return fragment;
}

//... Class overrides here onCreateView etc..

// declare this method
public void updateFragmentTwo(String updateText){
// .. do something with update text
}

}

就是这样,祝您编码愉快!

关于android - fragment 之间如何通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32346704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com