Aks*_*ood 1 android interface android-fragments
我想使用接口无捆绑包将数据从活动传递到捆绑包。请看下面的代码:
接口:
public interface FragmentCommunicator {
public void passData(String name);
}
Run Code Online (Sandbox Code Playgroud)
主要活动
public class MainActivity extends AppCompatActivity{
FragmentCommunicator fragmentCommunicator;
private Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragment= new Fragment();
fragmentCommunicator = (FragmentCommunicator) getApplication();
fragmentCommunicator.passData("hello");
getSupportFragmentManager().beginTransaction().replace(R.id.container ,fragment).commit();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
分段
public class Fragment extends android.support.v4.app.Fragment implements FragmentCommunicator {
FragmentCommunicator communicator;
Context c;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, null);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.c = context;
}
@Override
public void passData(String name) {
Toast.makeText(c, name, Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
我只想传递一些字符串,当我单击按钮以启动片段时,片段启动时应显示包含该字符串的吐司。.请帮助我们,将不胜感激。
在onCreate方法之后编写此行代码。
public void passVal(FragmentCommunicator fragmentCommunicator) {
this.fragmentCommunicator = fragmentCommunicator;
}
Run Code Online (Sandbox Code Playgroud)
像这样
public class MainActivity extends AppCompatActivity{
FragmentCommunicator fragmentCommunicator;
private Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button);
fragment= new Fragment();
//App is crasing for this line. Working fine by removing it
//fragmentCommunicator = (FragmentCommunicator) getApplication();
//fragmentCommunicator.passData("hello");
getSupportFragmentManager().beginTransaction().replace(R.id.container ,fragment).commit();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragmentCommunicator.passData("Hello");
}
});
}
//Here is new method
public void passVal(FragmentCommunicator fragmentCommunicator) {
this.fragmentCommunicator = fragmentCommunicator;
}
}
Run Code Online (Sandbox Code Playgroud)
然后将这行代码写入片段的onCreateView()中。像这样
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, null);
((MainActivity) getActivity()).passVal(new FragmentCommunicator() {
@Override
public void passData(String name) {
Toast.makeText(c, name, Toast.LENGTH_SHORT).show();
}
});
return view;
}
Run Code Online (Sandbox Code Playgroud)
注意:无需在片段中实现FragmentCommunicator接口。希望它能工作。这个对我有用。我测试过